angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Uncaught Unexpected value 'undefined' declared by the module 'AppModule'

Open caroso1222 opened this issue 10 years ago • 38 comments

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

caroso1222 avatar Aug 25 '16 07:08 caroso1222

I am facing same issue. "Unexpected value 'undefined' declared by the module 'AppModule' at SyntaxError.ZoneAwareError"

Can anyone please help out to find what's wrong looking at http://stackoverflow.com/questions/43794233/unexpected-value-undefined-declared-by-the-module-appmodule-at-syntaxerror-z?noredirect=1#comment74668883_43794233

ChandniGondhiya avatar May 09 '17 22:05 ChandniGondhiya

@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

abhinavsingi avatar May 11 '17 12:05 abhinavsingi

The cyclic dep checker is a great idea. But I hate to recommend ejecting a CLI app to get access to add such a webpack plugin. Ideally CLI would integrate it.

kylecordes avatar May 11 '17 14:05 kylecordes

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

ChandniGondhiya avatar May 24 '17 04:05 ChandniGondhiya

use: "export default RouterModule.forRoot(routes);" on the routing file instead

leolanese avatar May 26 '17 15:05 leolanese

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';

irontoby avatar Jun 29 '17 19:06 irontoby

Ran into this issue. It was caused by renaming a bunch of classes (trying to ensure consistency), all of the imports were correct in the AppModule, something just wasn't working right. My solution was to log all of the imports and see which ones were undefined. This limited it to two and I bug fixed from there. I ended up just renaming the class again, compiling, and setting it back to the new name.

SimplyComplexable avatar Jul 31 '17 21:07 SimplyComplexable

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


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

titusfx avatar Aug 16 '17 10:08 titusfx

Thanks @abhinavsingi for providing a link to cyclic-dependency-checker plugin. Had already gone through all of the other tips provided here but the issue wasn't due to my import/export syntax or using barrel files, it was due to a handful of circular dependencies in the app.

Super useful plugin and helped me finally resolve my issue after spending a couple hours trying to figure it out.

davidalee avatar Sep 04 '17 01:09 davidalee

Had the same problem. For me it was because I had 2 components with the exact same selector.

PhilHuot avatar Jan 11 '18 16:01 PhilHuot

In my case.. I made a really stupid mistake. in the project New created component Inner Scss file no extensions ..

I want you to check it out. Good luck

HolyMarsia avatar Feb 04 '18 13:02 HolyMarsia

@psamsotha Thanks so much.

hbsoftco avatar Feb 10 '18 20:02 hbsoftco

I was getting this error because of the barrels of my App I'm not sure yet which tsconfig setting causes it, but I have to use:

import { ... } from './subfolder/index`

instead just

import { ... } from './subfolder`

matheo avatar Feb 12 '18 16:02 matheo

In case it helps somebody. I fixed my problem with https://github.com/aackerman/circular-dependency-plugin

ralzinov avatar Jun 20 '18 16:06 ralzinov

I have a circular dependency which i cannot prevent. Is this issue going to be fixed or do i need to remove my circular dependency?

Linksonder avatar Jun 25 '18 14:06 Linksonder

@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'

ralzinov avatar Jun 25 '18 14:06 ralzinov

The above solutions of importing from the "index" file rather than the folder works well but has the downright that everyone needs to know and understand the problem and be vigilant of the imports, which is impossible in a big team working on IDEs with imports autogeneration (IntelliJ will always try to import from ".." when possible for example).

So in the enterprise library I am working on I renamed all "index" files "public_api", and the root public_api is re-exporting from all public_api files.

I also added a tslint rule to prevent imports from public_api (disabled in the root file of course).

This way the IDE always imports each class from its own file, not from the barrel but we have a nice "public_api tree" in the library.

Hope this helps.

mistic100 avatar May 01 '19 08:05 mistic100

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.