angular-cli: Uncaught Unexpected value 'undefined' declared by the module 'AppModule'

  • OS? Windows 7, 8 or 10. Linux (which distribution). Mac OSX (Yosemite? El Capitan?) Mac OSX El Capitan
  • Versions.
angular-cli: 1.0.0-beta.11-webpack.2
node: 6.3.0
os: darwin x64
  • Repro steps.

Migrate from @1.0.0-beta.10 to @webpack

tl;dr: Create two components. One (Child) inside the other (Parent). Aggregate all the exports into the parent’s barrel. Import the components into app.module.ts using the parent barrel and add them to the declarations array. App crashes.

Detailed steps

ng g component Parent
cd src/app/parent/
ng g component Child

Aggregate all exports within parent barrel so that parent/index.ts looks like this:

export * from './parent.component';
export * from './child'; //Should re export everything from child/index.ts

parent/child/index.ts looks like this:

export * from './child.component';

Now import the components using the parent barrel in app.module.ts and add it to the declarations array:

...
import { ParentComponent, ChildComponent  } from './parent';
...
@NgModule({
  declarations: [
    AppComponent,
    ParentComponent,
    ChildComponent
  ],
...
  • The log given by the failure.

Open up the app in the browser, console shows:

Uncaught Unexpected value 'undefined' declared by the module 'AppModule'
(anonymous function) @ metadata_resolver.js:287
CompileMetadataResolver.getNgModuleMetadata @ metadata_resolver.js:285
RuntimeCompiler._compileComponents @ runtime_compiler.js:150
RuntimeCompiler._compileModuleAndComponents @ runtime_compiler.js:72
RuntimeCompiler.compileModuleAsync @ runtime_compiler.js:49
PlatformRef_._bootstrapModuleWithZone @ application_ref.js:368
PlatformRef_.bootstrapModule @ application_ref.js:361
(anonymous function) @ main.ts:9
__webpack_require__ @ bootstrap d67ffcb…:52
(anonymous function) @ environment.dev.ts:4
__webpack_require__ @ bootstrap d67ffcb…:52
webpackJsonpCallback @ bootstrap d67ffcb…:23
(anonymous function) @ main.bundle.js:1
  • Mention any other details that might be useful.

When the component is generated via cli, it’s imported in app.module.ts using the path to the component itself. So it’s no natively failing when using the cli ng generate. But according to the Angular Style Guide a barrel should recursively re-export all its “sub-barrels”.

Workaround: Just import all the components from their respective folders as stated in this OS post.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 16
  • Comments: 38 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Not sure if my situation is related. But I have a module

/sidebar
   index.ts
   sidebar.component.ts
   sidebar.module.ts

index.ts

export * from './sidebar.module.ts';
export * from './sidebar.component.ts';

sidebar.module.ts

import { SidebarComponent } from './index';

@NgModule({
  declarations: [ SidebarComponent ],
  exports: [ SidebarComponent ]
})
export class SidebarModule {}

And I was getting the same error

Failed: Unexpected value 'undefined' exported by the module 'SidebarModule'

I had a bunch of other declarations and exports, but I narrowed down the problem to the SidebarComponent. It seems the problem is in the index.ts where exporting from './sidebar.module' before './sidebar.component' was the problem. Maybe because the module is dependent on the component, I don’t know. So to fix it, I just changed the order of exports in the index.ts

export * from './sidebar.component';
export * from './sidebar.module';

Not sure is this is expected behavior, but this is what solved my specific problem.

One thing to note is that I am not using CLI, but the angular2-webpack-starter. Not sure if that makes any difference.

replace export * from './myComponent' by export {MyComponent} from './myComponent' everywere the component that is undefined is exported. this does the job.

Oh yes, it is very easy:

import { Something } from './some-module';

console.assert(Something, "Uhoh, Something was not defined, likely part of a circular reference loop");

Do that for each thing where you are suspicious about the import not working.

As far as I can tell, the issue here is related to circular dependencies. These worked fine in SystemJS, but Webpack2 seems to have some trouble in it, see https://github.com/webpack/webpack/issues/1788. It manifests as Unexpected value 'undefined' in weird places.

It’s not something we can solve on the CLI itself other than wait for it to be working on a future Webpack2 version.

/cc @robwormald @TheLarkInn

Just in case anyone happens across this and makes the same stupid mistake I did and don’t see the answer right in front of you… it wasn’t even a circular dependency, I just had the component export duplicated in my index.ts:

export * from './my.component';
// lots more stuff...
export { MyComponent } from './my.component';

I’ve the exact problem with Karma and its DynamicModule, my problem was an extra COMMA ( AFTER httpModule )


...
TestBed.configureTestingModule({
      imports:[
        HttpModule,,
      ],
...

Before

import { abcComponent ,defComponent, xyzComponent,} from “./reports”;

After

import { abcComponent } from “./reports/abc-details/abc-details-component”; import { defComponent } from “./reports/def-details/def-detai‌​ls-component”; import { xyzComponent } from “./reports/xyz-details/xyz-details-component”;

I need to make above changes in app.module.ts files in order to resolve mentioned error

same issue.

I fix it by delete all index export file, then all file import path is specific path.

besides, don’t export default class.

@Linksonder I don’t think this issue is related to this project. It seems to it’s just how webpack works. Try to remove everything from barrels except re-exports

- src
  | - fileA.ts
  | - index.ts

./fileA.ts

import {RGB_CONSTANT} from './index'
...

./index.ts

// bad
export * from './fileA.ts'
export const RGB_CONSTANT = {
	red: '#FF0000',
	green: '#00FF00',
	blue: '#0000FF',
}

// good
export * from './fileA.ts'
export {RGB_CONSTANT} from './constants'

@kylecordes @Choleriker There’s a plugin out there which in a very good way detects all the cyclic dependencies in a project: https://github.com/DelvarWorld/webpack-cyclic-dependency-checker

barrels can be tricky. all I know is that exporting using a wildcard sometimes causes issues. ¯_(ツ)_/¯