简体   繁体   中英

angular2-template-loader: Error: Can't resolve './' in \@angular\compiler\src

Trying to bundle my angular2 app with webpack2 and angular2-template-loader, but getting this error:

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

Without the angular2-template-loader it bunldes just fine.

src/login/index.js:

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

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

src/login/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'
            }
        ]
    }
};

You should exclude the node_modules folder:

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

Otherwise webpack tries to pipe also .js files that are located somewhere in the node_modules folder through your pipeline, which is something you don't want and breaks things in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM