当前位置: 首页 > 知识库问答 >
问题:

从Angular 5升级到Angular 7后,找不到“AppModule”的NgModule元数据

华心思
2023-03-14

我们的开发团队最近将Angular 5项目更新为Angular 7。我已经下载了repo并且我正在尝试构建源代码,但是当我运行ng build命令时,我得到一个错误,没有找到'appmodule'的NgModule元数据。

如果运行ng--version命令,它将显示以下内容:

  • 角度CLI:7.3.6
  • 节点:11.10.0
  • 操作系统:win32 x64
  • 角度:7.2.10

app.module.ts文件为:

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 { }

main.ts文件为:

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

而index.html如下所示:

<!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>

和Angular.json文件:

{
  "$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"
    }
  }
}

下面是我在谷歌搜索的基础上尝试的所有不同的东西。除非另有指定,否则所有命令都是从Git Bash窗口运行的:

  1. NPM安装-g@Angular/CLI
  2. NPM安装@Angular/CLI
  3. NG更新@Angular/CLI
  4. NG更新
  5. NG更新@Angular/CLI@Angular/Core
  6. NG生成

关于NgModule元数据的相同问题仍然存在。接下来我尝试了以下操作:

  1. NPM删除WebPack
  2. npm install-save-dev@Angular/cli@latest
  3. NPM缓存清理-强制
  4. NPM安装
  5. npm install-save-dev@Angular/cli@latest
  6. NG生成

问题持续存在。下一个:

  1. 手动删除了node_modules文件夹
  2. NPM安装
  3. NG生成

问题持续存在。下一个:

  1. NPM I-G@Angular/CLI@最新
  2. 手动删除了node_modules文件夹
  3. NPM缓存清除-强制
  4. NPM缓存验证
  5. NPM安装
  6. NPM卸载WebPack
  7. npm install-save-dev-save-exact@Angular/cli@latew
  8. NG生成

问题持续存在。下一个:

  1. 手动删除了node_modules文件夹
  2. 手动删除了package-lock.json文件
  3. NPM安装
  4. NG生成

问题持续存在。下一个:

  1. NPM删除WebPack
  2. npm install-save-dev@Angular/cli@latest
  3. NG生成

问题持续存在。下一个:

  1. NPM缓存清理-强制
  2. NPM安装
  3. npm install-save-dev@Angular/cli@latest
  4. NG生成

问题持续存在。下一个:

  1. 手动删除了node_modules文件夹
  2. 手动删除了package-lock.json文件
  3. NPM缓存清理-强制
  4. NPM安装
  5. npm install-save-dev@Angular/cli@latest

问题持续存在。下一个:

  1. 在Visual Studio代码中打开的项目
  2. 在终端窗口中运行ng s
  3. 确认的编译错误
  4. 打开了app.component.ts文件
  5. 添加空格,删除空格,然后保存
  6. 项目自动重新编译
  7. 成功访问网站http://localhost:4200
  8. 关闭的Visual Studio代码
  9. 从Git Bash窗口运行ng Serve
  10. 项目无法编译
  11. 重新打开的Visual Studio代码
  12. 从终端窗口运行ng服务-错误持久
  13. 重新发送app.component.ts文件
  14. 已成功访问网站http://localhost:4200
  15. 重新启动Visual Studio代码
  16. 在Visual Studio代码中从终端窗口运行ng生成-返回错误
  17. 重新发送app.component.ts文件
  18. RERAN ng build命令-持久错误

我现在不知道还有什么可以尝试,开发团队要到周二休假,所以任何帮助或建议都将非常感谢,因为我有一些时间限制。事先谢谢你的帮助。

共有2个答案

沙宣
2023-03-14

在tsconfig.app.json中(或者在任何定义tsconfig设置的地方),如果这个角度的编译器选项设置为true,即使文件没有在ngmodule元数据中直接引用,也会导致各种元数据失败(只需在index.ts中导入一个文件就足够了)。

单个文件/类型可以使用//@dynamic注释显示,或者...您可以检查以确保StrictMetadataEmit设置为false:

tsconfig.json:

"angularCompilerOptions": {
    "strictMetadataEmit": false
}

在我的案例中,这在某一点上被设置为真,所以Angular在与模块defs无关的类元数据上失败了。将其设置为false(这是默认值,如此处所指定的:https://angular.io/guide/angula-compiler-options)可以解决此问题。

谢华彩
2023-03-14

我想现在您已经阅读了这篇文章:https://github.com/angular/angula-cli/issues/8798

如果没有,请阅读它并注意到TS还手动修改了package.json文件以解决他的问题。

由于我们没有在您的构建中看到实际的错误消息,我们无法推断该问题是否与链接中的问题具有相同的情况。因此,我对你解决问题的想法是:

  1. 删除npm_modules文件夹
  2. 将package.json文件移动到一个备份文件夹中(以防我们需要它回来)--因此项目文件夹中不应该存在这个文件,因为它将在您执行下一个步骤时生成
  3. 是否安装npm
  4. ng生成
  5. 提供ng服务

在完成上述工作之后,或者如果您再次使用它,我建议查看旧的package.json文件和新的package.json文件之间的差异,并让我们知道您观察到了什么,因为反馈总是很好的。

 类似资料:
  • 我用Play2.0创建了一个应用程序。我实现了scribe-java库来使用一些OAuth服务。直到今天,一切都很好,但当我升级jdk并重新启动服务器时,我不能使用scribe-java库。它似乎在sun.security包中使用了和类,但找不到。 “java-version”返回: 播放2.0 StackTrace:

  • 问题内容: 我们最近尝试在构建服务器上升级到节点v7.0.0,但是在“ bower_concat”步骤 失败的应用程序构建任务期间开始出现问题: 我们已经安装了什么: v7.0.0(通过 “ nodesource”存储库安装) 4.0.1 使用(如果相关) 0.6.0 Grunt构建任务配置: 和艰巨的任务: 请注意,它在节点v6.9.0上没有任何问题。 是什么原因引起的?我们是否可以解决该问题而

  • 我有使用WebPack构建的Angular2应用程序。我将WebPack从v1.8升级到v2,一切似乎都很好。唯一的问题是旧代码有以下内容: 升级后,这给了我一个错误:

  • 在导入react原生项目expo并升级react后,我遇到了以下问题。 C:\users\user\gramic\android\app\src\main\java\com\shop\mainapplication.java:5:错误:找不到符号import com.facebook.react.reactapplication;^symbol:class ReactApplication Loc

  • 我升级到Appium1.5,现在当我启动并尝试运行我的测试时,我看到了这个错误:检查我的设置,一切似乎都很好,甚至检查器也很好。 [AndroidDriver]错误:找不到操作系统为1的活动设备或模拟器。以下是可用的:192.168.99.101:5555(4.3)at object.wrappdlogger.errorandthrow(lib/logger.js:60:13)at object.

  • 在pom.xml中从Spring Boot Version2.3.5.Release升级到2.4.0之后,不能再导入类ProxyProvider()。请注意,即使在Spring Boot的较晚版本(尝试了2.5.0和2.5.2)中,它也不起作用。通过检查Maven依赖关系,我注意到: 在版本2.3.5.release中,该类取自 在2.4.0版本中,该类取自 我可以通过检查导入的类的来源在Inte