Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@ngtools/webpack complainning about not an NgModule #3157

Closed
princemaple opened this issue Nov 16, 2016 · 71 comments
Closed

@ngtools/webpack complainning about not an NgModule #3157

princemaple opened this issue Nov 16, 2016 · 71 comments

Comments

@princemaple
Copy link

princemaple commented Nov 16, 2016

using @ngtools/webpack 1.1.7
tried both @angular/compiler-cli 2.1.x and 2.2.0

import { NgModule, Component } from '@angular/core';

@Component({
  selector: 'app',
  template: '<h1>hello world</h1>',
  styles: []
})
class AppComponent {}

@NgModule({
  declarations: [AppComponent],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}
Error: AppModule is not an NgModule
    at AotPlugin.getNgModuleMetadata (/var/build/node_modules/@ngtools/webpack/src/plugin.js:297:19)
    at AotPlugin._processNgModule (/var/build/node_modules/@ngtools/webpack/src/plugin.js:241:42)
    at /var/build/node_modules/@ngtools/webpack/src/plugin.js:217:39
    at process._tickCallback (internal/process/next_tick.js:103:7)
    at Module.runMain (module.js:606:11)
    at run (bootstrap_node.js:394:7)
Done in 7.29s.
@marinho
Copy link

marinho commented Nov 17, 2016

@princemaple can you paste your AotPlugin configuration, please?

@marinho
Copy link

marinho commented Nov 17, 2016

oops, I missed this is an angular-cli related.

Without it (i.e. just using @ngtools/webpack), that error message would appear if entryModule points to the wrong path (i.e. ' ./src/app/app.module#AppModule' must be a valid path)

@princemaple
Copy link
Author

I had a very simple minimal AotPlugin config, something like

new ngtools.AotPlugin({
  tsConfigPath: '/var/build/tsconfig.json',
  entryModule: '/var/build/src/app.module#AppModule'
})

@princemaple
Copy link
Author

Note I used absolute paths, because I saw some commits in this repo saying something about relative paths not working. I am pretty sure my path was valid.

@marinho
Copy link

marinho commented Nov 17, 2016

I see. In that case, I have no more to add.

@janwo
Copy link

janwo commented Nov 17, 2016

I have the same problem here. I also configured it like @princemaple. BTW: I have not defined any entry point.

@princemaple
Copy link
Author

@marinho thanks for taking a look still!

@janwo
Copy link

janwo commented Nov 17, 2016

AotPlugin.prototype.getNgModuleMetadata = function (staticSymbol) {
    var ngModules = this._reflector.annotations(staticSymbol).filter(function (s) { 
        return s instanceof core_1.NgModule; 
    });
    if (ngModules.length === 0) {
        throw new Error(staticSymbol.name + " is not an NgModule");
    }
    return ngModules[0];
};

Whereas staticSymbol is...

{
  filePath: '/Users/***skipped***/client/app/app.module.ts',
  name: 'AppModule',
  members: undefined 
}

...and this._reflector.annotations(staticSymbol) is [].

Seems like the annotations are not set correctly? The path is valid.

@marinho
Copy link

marinho commented Nov 18, 2016

@janwo did you try without ".ts" in filePath?

When enabling AoT, it didn't pass because I had some imports with .ts in the end. I just removed in all lines it found and it didn't complain anymore. Maybe it's the same case.

BTW, I still don't have my app successfully in AoT. I'm also learning in this topic at this very moment.

@janwo
Copy link

janwo commented Nov 18, 2016

@marinho Thanks for your response. Yes, its all without a suffix. Somehow it works now, I deleted my node_modules folder and installed all dependencies again. It's weird as I updated them before as well.

@dhardtke
Copy link

I have the same problem, see #2667

@mashhoodr
Copy link

Im also getting the same. If I build a new project using ng new and run -aot on that, it works fine - but on my own project im getting Error: AppModule is not an NgModule. I have removed everything from my AppModule and AppComponent.

@dhardtke
Copy link

I have the problem with the AotPlugin for Webpack...

@jessezhang91
Copy link

It seems like [email protected] is causing the issue. Downgrading to 2.0.10 seems to make it pick up the metadata correctly.

@zakdances
Copy link

Strangely, this error shows up on 2.2.4 but not 2.2.3.

@dhardtke
Copy link

dhardtke commented Dec 2, 2016

For me downgrading to [email protected] worked, just like jessezhang91 suggested.

@e-oz
Copy link

e-oz commented Dec 2, 2016

@zakdances thanks for heads up. Will skip this update :) Previous dancing with this error is hard to forget.

@mashhoodr
Copy link

Downgrading to 2.0.12 didnt work for me. :( When I run ng server --aot I still get this error. (double checked the installed version as well)

@dhardtke
Copy link

dhardtke commented Dec 2, 2016

2.0.12 != 2.0.10

@mashhoodr
Copy link

Sorry, I meant to write 2.0.10.

@DutchKevv
Copy link

@zakdances downgrading all packages to 2.2.3 didn't work for me..:( Which packages did you downgraded to 2.2.3 exactly?? Thanks

@jessezhang91
Copy link

@DutchKevv make sure you downgrade router to 3.2.3 or so as well.

@DutchKevv
Copy link

Thanks Jess, but didn't get it to work..

Using these packages ->

"@angular/common": "2.2.3",
"@angular/compiler": "2.2.3",
"@angular/core": "2.2.3",
"@angular/forms": "2.2.3",
"@angular/http": "2.2.3",
"@angular/platform-browser": "2.2.3",
"@angular/platform-browser-dynamic": "2.2.3",
"@angular/router": "3.2.3",
"@ngrx/core": "^1.2.0",
"@ngrx/effects": "^2.0.0",
"@ngrx/store": "2.2.1",
"@ngrx/store-devtools": "^3.2.1",
"@ngrx/store-log-monitor": "^3.0.2",

Probably just wait until Typescript and Angular are friends again on final releases.. But can't build production now :(

@e-oz
Copy link

e-oz commented Dec 4, 2016

@DutchKevv try to rm -rf npm_modules && npm cache clean before installation.

@Quramy
Copy link
Contributor

Quramy commented Dec 4, 2016

I think this issue is related to [email protected] and tsc-wrapped as dhardtke and jessezhang91 said.

In https://github.com/angular/angular/blob/2.2.x/tools/@angular/tsc-wrapped/src/collector.ts#L254 , which collects metadata from app.module.ts ,

        case ts.SyntaxKind.ClassDeclaration:
          const classDeclaration = <ts.ClassDeclaration>node;
          if (classDeclaration.name) {
            const className = classDeclaration.name.text;
            if (node.flags & ts.NodeFlags.Export) { // this is false always! 
              if (classDeclaration.decorators) {
                if (!metadata) metadata = {};
                metadata[className] = classMetadataOf(classDeclaration);
              }
            }
          }

this code works only if node.flags & ts.NodeFlags.Export g.t. 0. But in https://github.com/Microsoft/TypeScript/blob/v2.1.1/src/compiler/types.ts#L400 , there is no export enum named Export. (In [email protected] ts.NodeFlags has the Export value. see https://github.com/Microsoft/TypeScript/blob/v2.0.10/src/compiler/types.ts#L395 )

@beeman
Copy link
Contributor

beeman commented Dec 5, 2016

I'm getting the same error using TypeScript 2.0.10, trying to link modules from 1 app into another. Both are generated using angular-cli version 1.0.0-beta.22.

@tarlepp
Copy link

tarlepp commented Dec 6, 2016

Yep same problem here with "typescript": "2.0.10" and "angular-cli": "1.0.0-beta.22-1" any solutions ?

@antonybudianto
Copy link
Contributor

antonybudianto commented Dec 6, 2016

not working even with typescript 2.0.10, using @ngtools/webpack 1.1.9

0% compilingError: AppModule is not an NgModule
    at AotPlugin.getNgModuleMetadata (/Users/antony/Documents/Code/Web/angular-webpack-starter/node_modules/@ngtools/webpack/src/plugin.js:332:19)
    at AotPlugin._processNgModule (/Users/antony/Documents/Code/Web/angular-webpack-starter/node_modules/@ngtools/webpack/src/plugin.js:271:42)
    at /Users/antony/Documents/Code/Web/angular-webpack-starter/node_modules/@ngtools/webpack/src/plugin.js:242:39

reproduction repo, just update the package.json for ngtools and angular packages


fixed, following updates from 45d5154

@ghost
Copy link

ghost commented Dec 6, 2016

@tarlepp @antonybudianto Workaround: Specify the precise dependency versions in the package.json.

@antonybudianto
Copy link
Contributor

antonybudianto commented Dec 6, 2016

@timosalm already did, it's breaking after @ngtools 1.1.6

@rgiosa
Copy link

rgiosa commented Dec 22, 2016

My external modules only builds when:

Angular: 2.2.3
Router: 3.2.3
Typescript: 2.0.10
Angular-Cli: 1.0.0-beta.15

I've tried several combinations.

npm uninstall angular-cli -g
npm cache clean
npm install [email protected] -g

@misterwise
Copy link

misterwise commented Dec 25, 2016

@hansl this is a legitimate issue and should be reopened, I also recently upgraded to Angular-Cli:1.0.0-beta.24 and encountered this breaking change, preventing work on a production site. Downgrading to beta.22 and related packages resolved the issue. It's possible that you mean this has been fixed post beta.24, if possible is there a workaround until the fix is released?

@anderflash
Copy link

It builds (dev and prod) even though the error is shown. I use latest versions:

  • angular 2.4.1
  • typescript 2.1.4
  • angular-cli 1.0.0-beta.24

@ichent
Copy link

ichent commented Dec 26, 2016

Hello.
I have this error today, after I updated angular to 2.4.1.
Today I saw many errors, and "AppModule is not an NgModule", and "cannot read property codegen" and so on. Then I changed my package.json to previous state (worked state), and now I'm getinng an error "AppModule is not an NgModule". A current package.json is
{
angular: 2.2.3,
router: 3.2.3,
"@ngtools/webpack": "1.1.9",
"typescript": "2.0.10",
"webpack": "2.1.0-beta.25"
}

and this is not working. Changing from 2.2.3 to 2.2.4 not solve this problem.

@greenginger
Copy link

greenginger commented Jan 5, 2017

I had the "AppModule is not an NgModule" error plus various others. I've somehow now got Angular 2.4.1 working with Angular CLI 1.0.0-beta.24 and Typescript 2.0.10 !! :o)

(But not with 2.1.4, that produced the AppModule is not an NgModule". Given this I suspect I need to wait a while for Angular to work with Typescript 2.1.4.)

I followed the steps here for updating Angular-CLI

And here are my dependencies from package.json:

dependencies": {
"@angular/common": "2.4.1",
"@angular/compiler": "2.4.1",
"@angular/core": "2.4.1",
"@angular/forms": "2.4.1",
"@angular/http": "2.4.1",
"@angular/platform-browser": "2.4.1",
"@angular/platform-browser-dynamic": "2.4.1",
"@angular/platform-server": "2.4.1",
"@angular/router": "3.4.1",
"@angularclass/conventions-loader": "^1.0.2",
"@angularclass/hmr": "~1.2.2",
"@angularclass/hmr-loader": "~3.0.2",
"core-js": "^2.4.1",
"http-server": "^0.9.0",
"ie-shim": "^0.1.0",
"jasmine-core": "^2.5.2",
"reflect-metadata": "^0.1.9",
"rxjs": "~5.0.2",
"zone.js": "~0.7.4",
"ts-helpers": "^1.1.1"
},
"devDependencies": {
"@angular/compiler-cli": "2.4.1",
"@types/jasmine": "2.5.38",
"@types/node": "^6.0.42",
"angular-cli": "1.0.0-beta.24",
"codelyzer": "~2.0.0-beta.1",
"jasmine-core": "2.5.2",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "~4.0.13",
"ts-node": "1.2.1",
"tslint": "^4.0.2",
"typescript": "2.0.10"
}

@e-oz
Copy link

e-oz commented Jan 5, 2017

@hansl I guess you closed this issue just because you assumed it should be somehow fixed, without checking it and asking affected users, right? Please reopen it and please let's find solution for this issue - it's pretty critical thing. Angular CLI is not usable at this moment because of this issue.

@JesterXL
Copy link

JesterXL commented Jan 6, 2017

I need(ed?) TypeScript 2.1.x for Generator support. Typescript 2.2.3, 2.1.4, and 2.1.3-insiders.20161130 didn't work (I couldn't find 2.1.3). Dropping to 2.0.10 like suggested fix it.

I should note an older project had "angular-cli": "^1.0.0-beta.19-3", and "typescript": "v2.1.1" and that worked for both ng serve and ng test. Whatever, it works and only took 1 🍺.

@ichent
Copy link

ichent commented Jan 9, 2017

Hello.
Today I removed @ngtools/webpack from my project and now I'm using webpack for AoT. I'm happy.

@princemaple
Copy link
Author

princemaple commented Jan 9, 2017 via email

@arciisine
Copy link

Angular 2.4.2 just landed which fixed TS 2.1 support. I've successfully built 2.4.2 with typescript 2.1.4 and beta 24 of the cli.

@sbley
Copy link

sbley commented Jan 9, 2017

@arciisine Which Angular dependency has to be updated to 2.4.2? common, core?

@jkuri
Copy link
Contributor

jkuri commented Jan 9, 2017

@sbley compiler-cli at least, preferable all.

@sbley
Copy link

sbley commented Jan 9, 2017

Yeah, in the end these are peer dependencies. If you update one you gotta update all...

@ichent
Copy link

ichent commented Jan 9, 2017

@princemaple What do you mean?
Yesterday I founded an example here https://github.com/blacksonic/angular2-aot-webpack (big thanks to the author). But my roadmap is a little bit different
And now, I'm using webpack. Here is a part of my webpack.config.
.....
{
test: /.ts$/,
loaders: env.aot ?
['awesome-typescript-loader?tsconfig=./client-code/src/tsconfig.json', 'angular2-template-loader', 'angular2-router-loader?genDir=./client-code/src/aot-compiled/app&aot=true'] :
['awesome-typescript-loader?tsconfig=./client-code/src/tsconfig.json', 'angular2-template-loader', 'angular2-router-loader']
}
...

With 'angular2-router-loader' I can split my bundles.
Here are my "scripts" from packages.json

"scripts": {
"prepublish": "yarn run build",
"build": "yarn run clean && yarn webpack",
"build:jit": "yarn run clean && yarn webpack -- --env.production",
"build:production": "yarn run clean && yarn run compiler:aot && yarn webpack -- --env.aot --env.production",
"compiler:aot": "ngc -p client-code/src/tsconfig-aot.json",
"clean": "rimraf client-code/dist/* client-code/src/aot-compiled"
},

@rgiosa
Copy link

rgiosa commented Jan 9, 2017

Yeap, this is now working with Typescript 2.1.4!

angular-cli: 1.0.0-beta.24
node: 4.4.7
os: win32 x64
@angular/common: 2.4.2
@angular/compiler: 2.4.2
@angular/core: 2.4.2
@angular/forms: 2.4.2
@angular/http: 2.4.2
@angular/platform-browser: 2.4.2
@angular/platform-browser-dynamic: 2.4.2
@angular/router: 3.4.2
@angular/compiler-cli: 2.4.2

@princemaple
Copy link
Author

@ichent thanks for the information about the alternative!

@ichent
Copy link

ichent commented Jan 10, 2017

@princemaple You are welcome!

@MortimerCat
Copy link

I can also confirm that this is now working on the latest release versions.....

Angular 2.4.2
Router on 3.4.2
Typescript 2.1.4

@larssin
Copy link

larssin commented Jan 14, 2017

Look for unwanted node_modules folders...

I experienced the problem with "AppModule is not an NgModule" after trying to run "./node_modules/.bin/ngc" to build a translated version (the build failed and the app wasn't translated).
I tried to reinstall, change versions etc. but nothing worked.
I finally realized that something had created an extra node_modules folder under the src folder. It only contains "@angular" and "rxjs" folders. When I delete this extra node_modules I can build again.
However I still can't figure out how to use i18n - but that is another problem.

@AckerApple
Copy link

I had one time forgot to add "typings"="index.d.ts" key to my package.json file. Once I did, I stopped getting Please add @NgModule error

@Maloric
Copy link

Maloric commented May 22, 2017

In case anyone stumbles across this, in my case it was misconfiguration. I was getting a null is not an NgModule error, which was because my entryModule missed of the name of the module:

    new ngtools.AotPlugin({
        tsConfigPath: './tsconfig.json',
        entryModule: root('src/app/pager') 
    })

became

    new ngtools.AotPlugin({
        tsConfigPath: './tsconfig.json',
        entryModule: root('src/app/pager') + '#PagerComponentModule'
    })

@pratiksha-chandel
Copy link

ng build and ng serve is not working for me .gives error

ERROR in /home/pratiksha/migration//app/shared/user.service.spec.ts (7,3): Cannot find name 'beforeEach'.

ERROR in /home/pratiksha/migration/app/shared/user.service.spec.ts (13,3): Cannot find name 'it'.

ERROR in /home/pratiksha/migration/app/shared/user.service.spec.ts (14,5): Cannot find name 'expect'.

ERROR in PopoverModule is not an NgModule

ERROR in multi ./src/styles.css ./src/css/main.css ./src/css/font-awesome.min.css ./src/css/daterangepicker.css ./src/css/style.css ./src/css/style-bootstrap.css ./src/css/style-default.css ./src/css/style-material.css
Module not found: Error: Can't resolve '/home/pratiksha/migration/src/css/main.css' in '/home/pratiksha/migration/node_modules/@angular/cli/models/webpack-configs'
@ multi ./src/styles.css ./src/css/main.css ./src/css/font-awesome.min.css ./src/css/daterangepicker.css ./src/css/style.css ./src/css/style-bootstrap.css ./src/css/style-default.css ./src/css/style-material.css

try all but not working for me .
angular cli is not working after upgrade -this is not supporting to .spec.ts file .
why this is happing ?

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests