angular2-template-loader:错误:无法解析\ @angular \ compiler \ src中的'./'

时间:2016-12-01 16:37:53

标签: javascript angular webpack webpack-2 angular2-template-loader

尝试将我的angular2应用与webpack2和angular2-template-loader捆绑在一起,但收到此错误:

ERROR in ./~/@angular/compiler/src/compile_metadata.js
Module not found: Error: Can't resolve './' in 'C:\Users\eagor\Documents\work\node_modules\@angular\compiler\src'
 @ ./~/@angular/compiler/src/compile_metadata.js 393:22-35
 @ ./~/@angular/compiler/index.js
 @ ./~/@angular/platform-browser-dynamic/src/platform-browser-dynamic.js
 @ ./~/@angular/platform-browser-dynamic/index.js
 @ ./src/login/index.js

如果没有angular2-template-loader它就可以了。

的src /登录/ index.js:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { LoginModule } from './src/login.module';

document.addEventListener('DOMContentLoaded', function() {
    platformBrowserDynamic()
        .bootstrapModule(LoginModule);
});

的src /登录/ SRC / login.module.js

export class LoginModule {
    // make empty for example.
}

webpack.config.js:

var path = require("path");

module.exports = {
    entry: {
        login: "./src/login/index.js"
    },
    output: {
        path: path.resolve(__dirname, "wwwroot/bundles"),
        filename: "[name].min.js"
    },
    module: {
        loaders: [
            {
                test: /\.js$/, exclude: /node_modules/,
                loader: "babel-loader"
            },

            // adding this loader breaks
            {
                test: /\.js$/,
                loaders: ['angular2-template-loader'],
                exclude: [/\.(spec)\.js$/]
            },

            {
                test: /\.(html|css)$/,
                loader: 'raw-loader'
            }
        ]
    }
};

1 个答案:

答案 0 :(得分:2)

您应该排除node_modules文件夹:

{
  test: /\.js$/,
  loaders: ['angular2-template-loader'],
  exclude: [/node_modules/, /\.(spec)\.js$/]
}

否则webpack会尝试通过您的管道管道位于.js文件夹中某处的node_modules个文件,这是您不想要的,并且会破坏您的情况。