Unexpected value 'MyCustomModule' imported by the module 'AppModule'

风流意气都作罢 提交于 2019-12-03 10:45:52

问题


I am trying to migrate one of my angular2 custom library to RC.6 + Webpack. My directory structure is:

- src - source TS files
- lib - transpiled JS files + definition files
- dev - development app to test if it works / looks ok.
- myCustomLib.js - barrel
- myCustomLib.d.ts

Within dev folder try to run an app. I bootstrap my module:

app.module.ts

import { BrowserModule }                from "@angular/platform-browser";
import { NgModule }                     from "@angular/core";
import { AppComponent }                 from "./app.component";
import { MyCustomModule }               from "../../myCustomLib";

@NgModule({
    imports: [
        BrowserModule,
        MyCustomModule
    ],
    declarations: [ AppComponent ],
    bootstrap: [ AppComponent ]
})
export class AppModule {
}

Now using the webpack I bundle my dev app.

webpack.config.js

module.exports = {
    entry: "./app/boot",
    output: {
        path: __dirname,
        filename: "./bundle.js",
    },
    resolve: {
        extensions: ['', '.js', '.ts'],
        modules: [
            'node_modules'
        ]
    },
    devtool: 'source-map',
    module: {
        loaders: [{
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: /node_modules/
        }, {
            test: /\.ts$/,
            loader: 'awesome-typescript-loader',
            exclude: /node_modules/
        }]
    },
    watch: true
};

But when I try to load the app I get a message:

metadata_resolver.js:230
Uncaught Error: Unexpected value 'MyCustomModule' imported by the module 'AppModule'

My barrel file I import looks like:

myCustomLib.js

export * from './lib/myCustomLib.module';

I found also hint on similar topic on github, but changing it to:

export { MyCustomModule } from './lib/myCustomLib.module';

did not help. I have also tried to import the module from src directory - same error. MyCustomModule should be ok as It was working fine with systemJS before.

myCustomLib.module.ts:

import { NgModule }                     from '@angular/core';
import { BrowserModule }                from '@angular/platform-browser';

@NgModule({
    imports: [
        BrowserModule
    ]
})
export class MyCustomModule {}

Any idea what can be the reason of this error? I have seen similar topics here but no answer or hint helped.

Edit: To make the example even simpler I have removed all from MyCustomModule - same problem...


回答1:


Add the lib folder to your webpack configuration for resolving modules.The path should be relative to the location of the webpack config file.

resolve: {
   modules: ['node_modules','lib']



回答2:


in myCustomLib.js, try to import before exporting

import { MyCustomModule } from './lib/myCustomLib.module;

export MyCustomModule;



回答3:


your MyCustomModule should be inside the src/app folder as it is not recognised by compiler because compiler, compiles all the files in the src folder ,thats why it is not recognising your module



来源:https://stackoverflow.com/questions/39385191/unexpected-value-mycustommodule-imported-by-the-module-appmodule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!