Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 555
Baumi Avatar asked Sep 08 '16 07:09

Baumi


2 Answers

in myCustomLib.js, try to import before exporting

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

export MyCustomModule;
like image 124
Ionut Achim Avatar answered Nov 08 '22 20:11

Ionut Achim


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']
like image 1
LT56 Avatar answered Nov 08 '22 19:11

LT56