"No NgModule metadata found for 'AppModule'" after Upgrade to Angular 5.1.0 and AngularCli 1.6.0
Versions
Angular CLI: 1.6.0
Node: 8.9.1
OS: win32 x64
Angular: 5.1.0
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
@angular/cdk: 5.0.0
@angular/cli: 1.6.0
@angular/flex-layout: 2.0.0-beta.10-4905443
@angular/material: 5.0.0
@ngtools/json-schema: 1.1.0
@schematics/schematics: 0.0.10
typescript: 2.5.0
webpack: 3.10.0
Repro steps
- Upgrade
@angularfrom5.0.2to5.1.0 - Upgrade
@angular/clifrom1.5.2to1.6.0 - Run
ng serve
Observed behavior
After doing the upgrade, the following error arises. I was able to resolve the issue as described here.
ERROR in Error: No NgModule metadata found for 'AppModule'.
at NgModuleResolver.resolve (C:\Users\volkende\WebstormProjects\project-name\node_modules\@angular\compiler\bundles\compiler.umd.js:20249:23)
at CompileMetadataResolver.getNgModuleMetadata (C:\Users\volkende\WebstormProjects\project-name\node_modules\@angular\compiler\bundles\compiler.umd.js:15218:60)
at visitLazyRoute (C:\Users\volkende\WebstormProjects\project-name\node_modules\@angular\compiler\bundles\compiler.umd.js:31063:104)
at AotCompiler.listLazyRoutes (C:\Users\volkende\WebstormProjects\project-name\node_modules\@angular\compiler\bundles\compiler.umd.js:31031:20)
at AngularCompilerProgram.listLazyRoutes (C:\Users\volkende\WebstormProjects\project-name\node_modules\@angular\compiler-cli\src\transformers\program.js:156:30)
at Function.NgTools_InternalApi_NG_2.listLazyRoutes (C:\Users\volkende\WebstormProjects\project-name\node_modules\@angular\compiler-cli\src\ngtools_api.js:44:36)
at AngularCompilerPlugin._getLazyRoutesFromNgtools (C:\Users\volkende\WebstormProjects\project-name\node_modules\@angular\cli\node_modules\@ngtools\webpack\src\angular_compiler_plug
in.js:247:66)
at Promise.resolve.then.then (C:\Users\volkende\WebstormProjects\project-name\node_modules\@angular\cli\node_modules\@ngtools\webpack\src\angular_compiler_plugin.js:561:50)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
Desired behavior
- Since no real changes have been made in
app.module.ts, as a developer I don't want to have to take this step
Mention any other details that might be useful (optional)
- Before this issue occured, I had to explicitly
npm i --save-dev webpackto be able to runng serveat all (#7334)
The original problem in this issue is definitely due to having webpack in package.json. There might be other problems that exhibit similar behaviour though, but without a repro I cannot investigate.
Good news! Based on the recent conversations on here it prompted me to try and reproduce this issue with a new CLI project, and once I was able to reproduce the issue the solution became obvious. It has to do with how you bootstrap the main Angular module for your application. Here's an abridged version of the contents of my main.ts file:
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { environment } from 'environments/environment';
import 'whatwg-fetch';
import { AppModule } from 'app/app.module';
const platform = platformBrowserDynamic();
fetch('url to get some configuration setting', { method: 'get', credentials: 'include' }).then((response) => {
response.json().then((settings: any) => {
if (environment.production) {
enableProdMode();
}
platform.bootstrapModule(AppModule).then((moduleInjector: any) => {
// code to do stuff with some application specific services using the configuration
//that was pulled down from the 'fetch' operation
});
});
});
The main thing to take away from this is how it's setting the constant 'platform' to the result of executing the 'platformBrowserDynamic()' function and later using that 'platform' variable to call 'bootstrapModule()'. Apparently, in order for AOT to work, the call to bootstrapModule HAS to happen as follows:
platformBrowserDynamic().bootstrapModule(AppModule);
I tried switching the 'const' in 'const platform = platformBrowserDynamic()' to a 'let' and a 'var' and neither one worked. This issue can be reproduced by simply changing your main.ts to capture the result of the platformBrowserDynamic() function call to a variable, and using that variable to call 'bootstrapModule()' function.
Edit: Code sample formatting.
@willisd2 that is unfortunately a limitation (https://github.com/angular/angular-cli/issues/8880) of our current code generation setup 😞
So I'm having the issue only the first time I fire npm start and webpack throws the metadata error on AppModule. If I save a project file, it kicks it off again and works.
Running a 960 EVO 3000MB/s SSD .. any chance there is some speed/access issue?
I've used Git Bash (mingw) and babun (cygwin) on Win10.
Update I installed a newer version of typescript and now its working. @angular/[email protected] requires typescript@'>=2.4.2 <2.6.0' but 2.6.2 was found instead. But it builds. I was on 2.5.3 previously.
the answer is
npm install --save-dev --save-exact @angular/cli@latest
or remove "^" in package.json
change from "@angular/cli": "^1.6.4" to "@angular/cli": "1.6.4"
For me this issue still exists, after upgrading to 1.6.4, i tried removing node modules, clearing cache, and save reinstall for angular cli..
ERROR in Error: No NgModule metadata found for 'AppModule'.
at NgModuleResolver.resolve (/home/hnr/web/gera-srh/node_modules/@angular/compiler/bundles/compiler.umd.js:20276:23)
at CompileMetadataResolver.getNgModuleMetadata (/home/hnr/web/gera-srh/node_modules/@angular/compiler/bundles/compiler.umd.js:15229:60)
at visitLazyRoute (/home/hnr/web/gera-srh/node_modules/@angular/compiler/bundles/compiler.umd.js:31090:104)
at AotCompiler.listLazyRoutes (/home/hnr/web/gera-srh/node_modules/@angular/compiler/bundles/compiler.umd.js:31058:20)
at AngularCompilerProgram.listLazyRoutes (/home/hnr/web/gera-srh/node_modules/@angular/compiler-cli/src/transformers/program.js:156:30)
at Function.NgTools_InternalApi_NG_2.listLazyRoutes (/home/hnr/web/gera-srh/node_modules/@angular/compiler-cli/src/ngtools_api.js:44:36)
at AngularCompilerPlugin._getLazyRoutesFromNgtools (/home/hnr/web/gera-srh/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:248:66)
at Promise.resolve.then.then (/home/hnr/web/gera-srh/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:565:50)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:160:7)
EDIT: I get this for all my angular projects since i updated..
@elhenro Hi. I was getting the same error, and had tried the same steps you mention: removing node modules, clearing cache, reinstall
There were a couple of extra things I did that made it work, maybe it will help:
npm cache clean --force(instead ofnpm cache verify)npm update- In package.json chagned TypeScript to 2.6.2
- deleted the
package.lock.json, then rannpm install
I can reproduce the "No NgModule metadata found for 'AppModule'." issue:
Angular: 5.2.0
Angular CLI: 1.6.4
TypeScript: 2.5.3
node: 8.9.3
npm: 5.5.1
As described above, making any tiniest change to app.module.ts makes it disappear. I don't have webpack listed as a dependency in my package.json.
It would be nice to reopen the issue.
@KNeela, "OpaqueToken" was removed in Angular 5.0.0 as it was deprecated in Angular 4.0.0. You should use "InjectionToken" instead.
I had to remove my existing typescript version 2.5.3 and install [email protected] after hours of trying to figure it out .
Same error. Nothing helped really except saving the file and CLI kicks it off for the second time and then it works.
Angular: 5.2.0 Angular CLI: 1.6.5 TypeScript: 2.5.3 / 2.6.2 node: 9.4.0 npm: 5.6.0
Same problem here! I manage to make it work first time but if I stop and start the project again then it fails again! Super annoying trying a full day to figure this out.
Angular CLI: 1.6.5 Node: 9.4.0 OS: darwin x64 Angular: 5.1.2 ... animations, common, compiler, compiler-cli, core, forms ... http, language-service, platform-browser ... platform-browser-dynamic, router @angular/cdk: 5.0.2 @angular/cli: 1.6.5 @angular/material-moment-adapter: 5.0.2 @angular/material: 5.0.2 @angular-devkit/build-optimizer: 0.0.41 @angular-devkit/core: 0.0.28 @ngtools/json-schema: 1.1.0 typescript: 2.4.2 webpack-dev-middleware: 2.0.4 webpack: 3.10.0
And the error:
ERROR in Error: No NgModule metadata found for 'AppModule'. at NgModuleResolver.resolve (/Users/vassilis/Projects/Lexicon-NG/node_modules/@angular/compiler/bundles/compiler.umd.js:20276:23) at CompileMetadataResolver.getNgModuleMetadata (/Users/vassilis/Projects/Lexicon-NG/node_modules/@angular/compiler/bundles/compiler.umd.js:15229:60) at visitLazyRoute (/Users/vassilis/Projects/Lexicon-NG/node_modules/@angular/compiler/bundles/compiler.umd.js:31090:104) at AotCompiler.listLazyRoutes (/Users/vassilis/Projects/Lexicon-NG/node_modules/@angular/compiler/bundles/compiler.umd.js:31058:20) at AngularCompilerProgram.listLazyRoutes (/Users/vassilis/Projects/Lexicon-NG/node_modules/@angular/compiler-cli/src/transformers/program.js:156:30) at Function.NgTools_InternalApi_NG_2.listLazyRoutes (/Users/vassilis/Projects/Lexicon-NG/node_modules/@angular/compiler-cli/src/ngtools_api.js:44:36) at AngularCompilerPlugin._getLazyRoutesFromNgtools (/Users/vassilis/Projects/Lexicon-NG/node_modules/@angular/cli/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:248:66) at Promise.resolve.then.then (/Users/vassilis/Projects/Lexicon-NG/node_modules/@angular/cli/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:565:50) at
at process._tickCallback (internal/process/next_tick.js:160:7)
Same issue.
I solved it by only updating typescript from 2.6.1 to 2.6.2.
Thank @boogie4eva and @briancodes
Same Issue, here. Already Updated typescript
ERROR in Error: No NgModule metadata found for 'AppModule'. at NgModuleResolver.resolve
Node: 9.4.0
OS: darwin x64
Angular: 5.2.2
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
@angular/cli: 1.6.5
@angular-devkit/build-optimizer: 0.0.42
@angular-devkit/core: 0.0.29
@angular-devkit/schematics: 0.0.52
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.5
@schematics/angular: 0.1.17
typescript: 2.6.2
webpack: 3.10.0`
Any Idea?
I was facing the same issue.
It got resolved after updating Typescript to "2.6.2".
Still having this issue, with Angular 5.2.2 and the ASP core templating.
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
An unhandled exception has occurred: No NgModule metadata found for 'AppM
dule'.
Error: No NgModule metadata found for 'AppModule'.
I have already upgraded my typescript, reinstalled everything, still the issue persists.
I cannot publish it via dotnet.
Does anyone have a fix? I have the same issue with
Angular CLI: 1.6.6 Node: 8.9.4 OS: darwin x64 Angular: 5.2.0 ... common, compiler, compiler-cli, core, forms, http ... language-service, platform-browser, platform-browser-dynamic ... router
@angular/animations: 5.2.1 @angular/cli: 1.6.6 @angular-devkit/build-optimizer: 0.0.42 @angular-devkit/core: 0.0.29 @angular-devkit/schematics: 0.0.52 @ngtools/json-schema: 1.1.0 @ngtools/webpack: 1.9.6 @schematics/angular: 0.1.17 typescript: 2.4.2 webpack: 3.10.0
I got same issue
ERROR in Error: No NgModule metadata found for 'AppModule'.
Environment
Angular CLI: 1.6.6
Node: 6.7.0
OS: darwin x64
Angular: 5.2.2
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
@angular/cli: 1.6.6
@angular-devkit/build-optimizer: 0.0.42
@angular-devkit/core: 0.0.29
@ngtools/json-schema: 1.1.0
typescript: 2.5.3
webpack: 3.10.0
Solution
I resolved it by updating the Typescript to 2.6.2 🎉
I had the same error after upgrade of the following: angular: 4.1.0 -> 5.1.0 angular/cli: 1.2.1 -> 1.6.0 || 1.6.6
What solved my issue was:
- remove node_module
- upgrade typescript 2.3.3 -> 2.6.2
No NgModule metadata found for 'AppModule' happens with cli 1.6.7 too on a fresh ng new my-app and npm update --save-dev && npm update --save, after which installed typescript version was ^2.7.0.
Workaround npm install --save-dev [email protected] fixed it for me.
Edit: Funnily enough, when you run ng serve, first you get the error and nothing works in browser. Then save any .ts file and it recompiles just fine, everything works in the browser. Obviously won't help when you want to do ng build. :)
@angular versions:
"@angular/animations": "^5.2.3",
"@angular/cdk": "^5.1.1",
"@angular/common": "^5.2.3",
"@angular/compiler": "^5.2.3",
"@angular/core": "^5.2.3",
"@angular/flex-layout": "^2.0.0-beta.12",
"@angular/forms": "^5.2.3",
"@angular/http": "^5.2.3",
"@angular/material": "^5.1.1",
"@angular/platform-browser": "^5.2.3",
"@angular/platform-browser-dynamic": "^5.2.3",
"@angular/router": "^5.2.3",