问题
Our dev team recently updated an Angular 5 project to Angular 7. I've downloaded the repo and I'm trying to build the source, however when I run the ng build command I get a ERROR in No NgModule metadata found for 'AppModule'.
If I run the ng --version command it shows the following:
- Angular CLI: 7.3.6
- Node: 11.10.0
- OS: win32 x64
- Angular: 7.2.10
The app.module.ts file is:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { AdminComponent } from './layout/admin/admin.component';
import { AdminComponent1 } from './layout/admin1/estimate.component';
import { AuthComponent } from './layout/auth/auth.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { SharedModule } from './shared/shared.module';
import { MenuItems } from './shared/menu-items/menu-items';
import { BreadcrumbsComponent } from './layout/admin/breadcrumbs/breadcrumbs.component';
import { ServicesProvider } from '../providers/services';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AuthGuardService } from './activate-guard';
import { AuthLoginService } from './deactivate-guard';
import { SupperAdmin } from './supperadmin-guard';
import { NgProgressModule } from 'ngx-progressbar';
import {SimpleNotificationsModule} from 'angular2-notifications';
import { ConfigService } from '../assets/config/ConfigService';
@NgModule({
declarations: [
AppComponent,
AdminComponent,
AdminComponent1,
AuthComponent,
BreadcrumbsComponent,
],
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
ReactiveFormsModule,
AppRoutingModule,
SharedModule,
NgProgressModule,
SimpleNotificationsModule.forRoot(),
BrowserModule,
],
providers: [MenuItems, ServicesProvider, AuthGuardService, SupperAdmin, AuthLoginService, NgProgressModule, ConfigService],
bootstrap: [AppComponent]
})
export class AppModule { }
and the main.ts file is:
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.log(err));
And the index.html is as follows:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome</title>
<base href="/">
<!-- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=xxx"></script>
<!-- <script src="https://maps.googleapis.com/maps/api/js"
async defer></script> -->
<!-- <script src="../src/assets/js/pdf.js"></script>
<script src="../src/assets/js/pdf.worker.js"></script> -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="Visual Estimator" />
<meta name="keywords" content="visualestimator" />
<meta name="author" content="phoenixcoded" />
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
<link rel="icon" type="image/x-icon" href="favicon.ico">
<style>
</style>
<!-- Google font-->
<link href="https://fonts.googleapis.com/css?family=Roboto:400,500" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/ng2-toastr@4.1.2/ng2-toastr.css" rel="stylesheet" />
</head>
<body>
<app-root>
<div class="theme-loader">
<div class="loader-track">
<div class="preloader-wrapper">
<div class="spinner-layer spinner-blue">
<div class="circle-clipper left">
<div class="circle"></div>
</div>
<div class="gap-patch">
<div class="circle"></div>
</div>
<div class="circle-clipper right">
<div class="circle"></div>
</div>
</div>
<div class="spinner-layer spinner-red">
<div class="circle-clipper left">
<div class="circle"></div>
</div>
<div class="gap-patch">
<div class="circle"></div>
</div>
<div class="circle-clipper right">
<div class="circle"></div>
</div>
</div>
<div class="spinner-layer spinner-yellow">
<div class="circle-clipper left">
<div class="circle"></div>
</div>
<div class="gap-patch">
<div class="circle"></div>
</div>
<div class="circle-clipper right">
<div class="circle"></div>
</div>
</div>
<div class="spinner-layer spinner-green">
<div class="circle-clipper left">
<div class="circle"></div>
</div>
<div class="gap-patch">
<div class="circle"></div>
</div>
<div class="circle-clipper right">
<div class="circle"></div>
</div>
</div>
</div>
</div>
</div>
</app-root>
</body>
<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-image/v0.0.4/leaflet-image.js'></script>
<script>
</script>
</html>
And the angular.json file:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"mega-able": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist",
"index": "src/index.html",
"main": "src/main.ts",
"tsConfig": "src/tsconfig.app.json",
"polyfills": "src/polyfills.ts",
"assets": [
"src/assets",
"src/favicon.ico",
"src/marker-icon.png",
"src/marker-icon-2x.png"
],
"styles": [
"./node_modules/bootstrap/dist/css/bootstrap.min.css",
"./node_modules/ngx-bootstrap/datepicker/bs-datepicker.css",
"node_modules/bootstrap/scss/bootstrap.scss",
"node_modules/font-awesome-scss/scss/font-awesome.scss",
"src/assets/images/zommer/leaflet/dist/leaflet.css",
"src/assets/images/zommer/leaflet-draw-toolbar/dist/leaflet.draw-toolbar.css",
"src/assets/images/zommer/leaflet-toolbar/dist/leaflet.toolbar.css",
"src/assets/images/zommer/leaflet-draw/dist/leaflet.draw-src.css",
"src/scss/style.scss"
],
"scripts": [
"src/assets/images/zommer/jquery.min.js",
"src/assets/images/zommer/bootstrap.min.js",
"src/assets/images/zommer/leaflet/dist/leaflet-src.js",
"src/assets/images/zommer/leaflet/dist/leaflet.rotatedMarker.js",
"src/assets/images/zommer/leaflet-toolbar/dist/leaflet.toolbar-src.js",
"src/assets/images/zommer/leaflet-draw/dist/leaflet.draw-src.js",
"src/assets/images/zommer/leaflet-draw-toolbar/dist/leaflet.draw-toolbar.js",
"src/assets/images/zommer/color.js",
"src/assets/images/zommer/pdf.js",
"src/assets/images/zommer/pdf.worker.js",
"src/assets/images/zommer/leafletfunction.js"
]
},
"configurations": {
"prod": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
]
},
"production": {
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "mega-able:build"
},
"configurations": {
"prod": {
"browserTarget": "mega-able:build:prod"
},
"production": {
"browserTarget": "mega-able:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "mega-able:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"karmaConfig": "./karma.conf.js",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"scripts": [
"src/assets/images/zommer/jquery.min.js",
"src/assets/images/zommer/bootstrap.min.js",
"src/assets/images/zommer/leaflet/dist/leaflet-src.js",
"src/assets/images/zommer/leaflet/dist/leaflet.rotatedMarker.js",
"src/assets/images/zommer/leaflet-toolbar/dist/leaflet.toolbar-src.js",
"src/assets/images/zommer/leaflet-draw/dist/leaflet.draw-src.js",
"src/assets/images/zommer/leaflet-draw-toolbar/dist/leaflet.draw-toolbar.js",
"src/assets/images/zommer/color.js",
"src/assets/images/zommer/pdf.js",
"src/assets/images/zommer/pdf.worker.js",
"src/assets/images/zommer/leafletfunction.js"
],
"styles": [
"./node_modules/bootstrap/dist/css/bootstrap.min.css",
"./node_modules/ngx-bootstrap/datepicker/bs-datepicker.css",
"node_modules/bootstrap/scss/bootstrap.scss",
"node_modules/font-awesome-scss/scss/font-awesome.scss",
"src/assets/images/zommer/leaflet/dist/leaflet.css",
"src/assets/images/zommer/leaflet-draw-toolbar/dist/leaflet.draw-toolbar.css",
"src/assets/images/zommer/leaflet-toolbar/dist/leaflet.toolbar.css",
"src/assets/images/zommer/leaflet-draw/dist/leaflet.draw-src.css",
"src/scss/style.scss"
],
"assets": [
"src/assets",
"src/favicon.ico",
"src/marker-icon.png",
"src/marker-icon-2x.png"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"mega-able-e2e": {
"root": "e2e",
"sourceRoot": "e2e",
"projectType": "application",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "./protractor.conf.js",
"devServerTarget": "mega-able:serve"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"e2e/tsconfig.e2e.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "mega-able",
"schematics": {
"@schematics/angular:component": {
"prefix": "app",
"styleext": "scss"
},
"@schematics/angular:directive": {
"prefix": "app"
}
}
}
Below are all the various things I have tried based on Google searches. All the commands were run from a Git Bash window unless otherwise specified:
- npm install -g @angular/cli
- npm install @angular/cli
- ng update @angular/cli
- ng update
- ng update @angular/cli @angular/core
- ng build
The same issue about the NgModule metadata was still present. Next I tried the following:
- npm remove webpack
- npm install –save-dev @angular/cli@latest
- npm cache clean –force
- npm install
- npm install –save-dev @angular/cli@latest
- ng build
Issue persisted. Next:
- Manually deleted the node_modules folder
- npm install
- ng build
Issue persisted. Next:
- npm i -g @angular/cli@latest
- Manually deleted node_modules folder
- npm cache clear –force
- npm cache verify
- npm install
- npm uninstall webpack
- npm install –save-dev –save-exact @angular/cli@latest
- ng build
Issue persisted. Next:
- Manually deleted the node_modules folder
- Manually deleted the package-lock.json file
- npm install
- ng build
Issue persisted. Next:
- npm remove webpack
- npm install –save-dev @angular/cli@latest
- ng build
Issue persisted. Next:
- npm cache clean –force
- npm install
- npm install –save-dev @angular/cli@latest
- ng build
Issue persisted. Next:
- Manually deleted the node_modules folder
- Manually deleted the package-lock.json file
- npm cache clean –force
- npm install
- npm install –save-dev @angular/cli@latest
Issue persisted. Next:
- Opened project in Visual Studio code
- In the terminal window ran ng s
- Confirmed compilation error
- Opened the app.component.ts file
- Added a blank space, removed it, and saved
- Project automatically recompiled
- Successfully access the site at http://localhost:4200
- Closed Visual Studio Code
- From a Git Bash window ran ng serve
- Project failed to compile
- Re-opened Visual Studio Code
- Ran ng serve from terminal window – error persisted
- Resaved the app.component.ts file
- Successfully accessed the site at http://localhost:4200
- Restarted Visual Studio Code
- Ran ng build from terminal window in Visual Studio Code – error returned
- Resaved the app.component.ts file
- Reran ng build command – error persisted
I'm at a lost of what else to try at this point and the dev team is off until at Tuesday for a holiday so any help or suggestions would be greatly appreciated as I'm under some time constraints. Thank you in advance for any help.
回答1:
I think by now, you've already read this article: https://github.com/angular/angular-cli/issues/8798
If not, please read it and notice that the TS also modified the package.json file manually to resolve his issue.
Since we have not seen the actual error message in your build, we cannot deduce if the issue has the same circumstance as the one in the link. So my idea for you to resolve your issue is to do the following:
- Remove the npm_modules folder
- Move the package.json file to a backup folder (in case we need it back) - so this should not exist in your project folder as it will get generated when you do the next steps
- Do the npm install
- Do ng build
- Do ng serve
After the above or if you get it working again I suggest to view the difference between your old package.json and new package.json file and let us know what you observed as it is always good to give back.
回答2:
It should be @NgModule not just NgModule.
@NgModule({
declarations: [
AppComponent,
AdminComponent,
AdminComponent1,
AuthComponent,
BreadcrumbsComponent,
],
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
ReactiveFormsModule,
AppRoutingModule,
SharedModule,
NgProgressModule,
SimpleNotificationsModule.forRoot(),
BrowserModule,
],
providers: [MenuItems, ServicesProvider, AuthGuardService, SupperAdmin, AuthLoginService, NgProgressModule, ConfigService],
bootstrap: [AppComponent]
})
Metadata is used to decorate a class so that it can configure the expected behavior of the class. We leverage the Typescript syntax and attach the classes with metadata using the decorator feature. To make a class as a component, we add the @Component decorator, as shown in the following code:
@Component({...})
export class FirstComponent {...}
回答3:
You have multiple trailing commas in your @NgModule's
declarations and imports arrays. Delete those and see if that solves it.
回答4:
I was able to resolve the issue by updating the tsconfig.app.json file to include the files property and specifically listing app/app.module.ts
in the array.
来源:https://stackoverflow.com/questions/55320181/no-ngmodule-metadata-found-for-appmodule-after-upgrade-from-angular-5-to-angul