Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No NgModule metadata found for 'AppModule' after upgrade from Angular 5 to Angular 7

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

enter image description here

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/[email protected]/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:

  1. npm install -g @angular/cli
  2. npm install @angular/cli
  3. ng update @angular/cli
  4. ng update
  5. ng update @angular/cli @angular/core
  6. ng build

The same issue about the NgModule metadata was still present. Next I tried the following:

  1. npm remove webpack
  2. npm install –save-dev @angular/cli@latest
  3. npm cache clean –force
  4. npm install
  5. npm install –save-dev @angular/cli@latest
  6. ng build

Issue persisted. Next:

  1. Manually deleted the node_modules folder
  2. npm install
  3. ng build

Issue persisted. Next:

  1. npm i -g @angular/cli@latest
  2. Manually deleted node_modules folder
  3. npm cache clear –force
  4. npm cache verify
  5. npm install
  6. npm uninstall webpack
  7. npm install –save-dev –save-exact @angular/cli@latest
  8. ng build

Issue persisted. Next:

  1. Manually deleted the node_modules folder
  2. Manually deleted the package-lock.json file
  3. npm install
  4. ng build

Issue persisted. Next:

  1. npm remove webpack
  2. npm install –save-dev @angular/cli@latest
  3. ng build

Issue persisted. Next:

  1. npm cache clean –force
  2. npm install
  3. npm install –save-dev @angular/cli@latest
  4. ng build

Issue persisted. Next:

  1. Manually deleted the node_modules folder
  2. Manually deleted the package-lock.json file
  3. npm cache clean –force
  4. npm install
  5. npm install –save-dev @angular/cli@latest

Issue persisted. Next:

  1. Opened project in Visual Studio code
  2. In the terminal window ran ng s
  3. Confirmed compilation error
  4. Opened the app.component.ts file
  5. Added a blank space, removed it, and saved
  6. Project automatically recompiled
  7. Successfully access the site at http://localhost:4200
  8. Closed Visual Studio Code
  9. From a Git Bash window ran ng serve
  10. Project failed to compile
  11. Re-opened Visual Studio Code
  12. Ran ng serve from terminal window – error persisted
  13. Resaved the app.component.ts file
  14. Successfully accessed the site at http://localhost:4200
  15. Restarted Visual Studio Code
  16. Ran ng build from terminal window in Visual Studio Code – error returned
  17. Resaved the app.component.ts file
  18. 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.

like image 328
Dominick Avatar asked Mar 24 '19 02:03

Dominick


2 Answers

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:

  1. Remove the npm_modules folder
  2. 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
  3. Do the npm install
  4. Do ng build
  5. 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.

like image 124
Ian Preglo Avatar answered Oct 10 '22 09:10

Ian Preglo


Just in case this helps someone, in tsconfig.app.json (or wherever you define your tsconfig settings), this Angular compiler option, when set to true, causes various metadata failures even if files are not directly referenced in the NgModule metadata (simply importing a file in an index.ts is enough).

Individual files / types can be surpressed with the // @dynamic comment, or ... you can check to make sure that strictMetadataEmit is set to false:

tsconfig.json:

"angularCompilerOptions": {
    "strictMetadataEmit": false
}

In my case, this had been set to true at some point, so Angular was failing on metadata for classes that had nothing to do with module defs. Setting it to false (which is the default, as specified here: https://angular.io/guide/angular-compiler-options) fixed the issue.

like image 39
Lincoln Avatar answered Oct 10 '22 08:10

Lincoln