检测到 Angular 编译器,但它是错误类的实例

Posted

技术标签:

【中文标题】检测到 Angular 编译器,但它是错误类的实例【英文标题】:Angular Compiler was detected but it was an instance of the wrong class 【发布时间】:2019-01-24 17:34:52 【问题描述】:

我知道这已经是一个问题,我已经尝试了我在那里找到的所有可能的东西。仍然无法破解它。任何帮助表示赞赏。

错误:

ERROR in ./src/main.ts
Module build failed: Error: Angular Compiler was detected but it was an instance of the wrong class.
This likely means you have several @ngtools/webpack packages installed. You can check this with `npm ls @ngtools/webpack`, and then remove the extra copies.
    at Object.ngcLoader (C:\WORK\folder\folder\folder\node_modules\@ngtools\webpack\src\loader.js:33:15)
 @ multi ./src/main.ts
ERROR in ./src/polyfills.ts
Module build failed: Error: Angular Compiler was detected but it was an instance of the wrong class.
This likely means you have several @ngtools/webpack packages installed. You can check this with `npm ls @ngtools/webpack`, and then remove the extra copies.
    at Object.ngcLoader (C:\WORK\folder\folder\folder\node_modules\@ngtools\webpack\src\loader.js:33:15)
 @ multi ./src/polyfills.ts
ERROR in Error: No NgModule metadata found for 'AppModule'.
    at NgModuleResolver.resolve (C:\WORK\folder\folder\folder\node_modules\@angular\compiler\bundles\compiler.umd.js:20291:23)
    at CompileMetadataResolver.getNgModuleMetadata (C:\WORK\folder\folder\folder\node_modules\@angular\compiler\bundles\compiler.umd.js:15244:60)
    at visitLazyRoute (C:\WORK\folder\folder\folder\node_modules\@angular\compiler\bundles\compiler.umd.js:31182:104)
    at AotCompiler.listLazyRoutes (C:\WORK\folder\folder\folder\node_modules\@angular\compiler\bundles\compiler.umd.js:31150:20)
    at AngularCompilerProgram.listLazyRoutes (C:\WORK\folder\folder\folder\node_modules\@angular\compiler-cli\src\transformers\program.js:228:34)
    at Function.NgTools_InternalApi_NG_2.listLazyRoutes (C:\WORK\folder\folder\folder\node_modules\@angular\compiler-cli\src\ngtools_api.js:47:40)
    at AngularCompilerPlugin._getLazyRoutesFromNgtools (C:\WORK\folder\folder\folder\node_modules\@angular\cli\node_modules\@ngtools\webpack\src\ang

配置:

Angular CLI: 1.7.0-beta.2
Node: 8.9.4
OS: win32 x64
Angular: 5.2.11
... animations, common, compiler, core, forms, http
... platform-browser, platform-browser-dynamic, router

@angular/cli: 1.7.0-beta.2
@angular/compiler-cli: 6.1.3
@angular/language-service: 6.1.3
@angular-devkit/architect: 0.8.0-beta.3
@angular-devkit/build-angular: 0.8.0-beta.3
@angular-devkit/build-optimizer: 0.8.0-beta.3
@angular-devkit/build-webpack: 0.8.0-beta.3
@angular-devkit/core: 0.8.0-beta.3
@angular-devkit/schematics: 0.0.52
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 6.2.0-beta.3
@schematics/angular: 0.1.17
@schematics/package-update: 0.0.7
typescript: 2.9.2
webpack: 4.16.5

包.json


  "name": "app1",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": 
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  ,
  "private": true,
  "dependencies": 
    "@angular/animations": "^5.2.9",
    "@angular/common": "^5.2.0",
    "@angular/compiler": "^5.2.0",
    "@angular/core": "^5.2.0",
    "@angular/forms": "^5.2.0",
    "@angular/http": "^5.2.0",
    "@angular/platform-browser": "^5.2.0",
    "@angular/platform-browser-dynamic": "^5.2.0",
    "@angular/router": "^5.2.0",
    "@progress/kendo-angular-buttons": "^3.0.4",
    "@progress/kendo-angular-dateinputs": "^2.2.0",
    "@progress/kendo-angular-dropdowns": "^2.1.0",
    "@progress/kendo-angular-excel-export": "^1.0.7",
    "@progress/kendo-angular-grid": "^2.1.2",
    "@progress/kendo-angular-inputs": "^2.2.0",
    "@progress/kendo-angular-intl": "^1.3.2",
    "@progress/kendo-angular-l10n": "^1.0.7",
    "@progress/kendo-data-query": "^1.2.0",
    "@progress/kendo-drawing": "^1.5.1",
    "@progress/kendo-ui": "^2018.2.620",
    "angular-font-awesome": "^3.1.2",
    "core-js": "^2.4.1",
    "font-awesome": "^4.7.0",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.19"
  ,
  "devDependencies": 
    "@angular-devkit/build-angular": "~0.8.0-beta.0",
    "@angular/cli": "1.7.0-beta.2",
    "@angular/compiler-cli": "^6.1.0",
    "@angular/language-service": "^6.1.0",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.3.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~2.9.2"
  

npm list -global --depth 0

 +-- @angular/cli@1.6.8
    +-- angular-1.6-cli@1.6.3
    +-- gulp@3.9.1
    +-- gulp-cli@2.0.1
    +-- typescript@2.9.2
    +-- uglify-js@3.3.24
    +-- webpack@4.16.2
    +-- webpack-cli@3.1.0
    `-- webpack-dev-server@3.1.5

我已经尝试了所有可能的想法

删除 node_module 文件夹和 Package.lock.json 重新安装 npm - npm 安装 尝试了 npm 更新

任何关于缺少什么的帮助都将非常感谢我被困在这里。

---------------------------------

经过大量研究,我能够通过将 package.json 更改为以下来解决此问题


  "name": "app1",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": 
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  ,
  "private": true,
  "dependencies": 
    "@angular/animations": "^5.2.0",
    "@angular/cdk": "^5.2.4",
    "@angular/common": "^5.2.0",
    "@angular/compiler": "^5.2.0",
    "@angular/core": "^5.2.0",
    "@angular/forms": "^5.2.0",
    "@angular/http": "^5.2.0",
    "@angular/material": "^5.2.4",
    "@angular/platform-browser": "^5.2.0",
    "@angular/platform-browser-dynamic": "^5.2.0",
    "@angular/router": "^5.2.0",
    "@progress/kendo-angular-buttons": "^3.0.4",
    "@progress/kendo-angular-dateinputs": "^2.2.0",
    "@progress/kendo-angular-dropdowns": "^2.1.0",
    "@progress/kendo-angular-excel-export": "^1.0.7",
    "@progress/kendo-angular-grid": "^2.1.2",
    "@progress/kendo-angular-inputs": "^2.2.0",
    "@progress/kendo-angular-intl": "^1.3.2",
    "@progress/kendo-angular-l10n": "^1.0.7",
    "@progress/kendo-data-query": "^1.2.0",
    "@progress/kendo-drawing": "^1.5.1",
    "@progress/kendo-ui": "^2018.2.620",
    "angular-font-awesome": "^3.1.2",
    "core-js": "^2.4.1",
    "ng2-tooltip": "0.0.7",
    "rxjs": "^5.5.6",
    "webpack": "^2.5.1",
    "zone.js": "^0.8.19"
  ,
  "devDependencies": 
    "@angular/cli": "~1.7.2",
    "@angular/compiler-cli": "^5.2.0",
    "@angular/language-service": "^5.2.0",
    "@types/jasmine": "~2.8.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~4.1.0",
    "tslint": "~5.9.1",
    "typescript": "~2.5.3"
  

但还有一个问题。

ERROR in No NgModule metadata found for 'AppModule'.

有人可以在这里帮忙吗?任何帮助都非常感谢

【问题讨论】:

你能分享你app.module.ts文件吗? 【参考方案1】:

我遇到了同样的问题并通过以下方式解决:

1) 更新包@ngtools/webpack到最新版本

2) 删除node_modulespackage-lock.json 并运行干净的npm install

【讨论】:

【参考方案2】:

这似乎是最好的解决方案,为我工作了两次。运行以下命令 -

npm 重复数据删除

您将能够看到一些审计细节。修复只需运行 -

npm 审计修复

如有必要(错误仍然存​​在),请运行以下 -

npm 审计修复 --force

这将删除所有重复的包以及修复它们的版本号。

希望这会有所帮助!

【讨论】:

【参考方案3】:

这个对我有用 npm卸载@angular-devkit/build-angular

npm install @angular-devkit/build-angular -D

【讨论】:

【参考方案4】:

我删除了@ngtools\webpack 文件夹和package-lock.json,然后运行yarn不是 npm installnpm update)。我不确定这是不是很好的解决方法,但它对我有用。

【讨论】:

【参考方案5】:

您的项目的依赖项可能存在冲突。例如@angular/compiler-cli 的版本是 6.1.0 而@angular/core 的版本仍然是 5.20。尝试将@angular/compiler-cli 降级到 5.x 或升级到 angular 6 (https://update.angular.io/)

【讨论】:

非常感谢您抽出时间提供帮助,我将编译器更改为 5.2.0 和 5.2.0-beta.1 仍然没有运气,遇到同样的错误 升级到 Angular 6 怎么样?突破性的变化很小。或者你想保留第 5 版? 因为这是一个项目上线的时间越来越近,迁移到 6 我们暂时不能接受。 npm 有很多关于嵌套依赖的问题。让我们试试yarn 根据我检查 npm ls @ngtools/webpack 时显示的错误建议,+-- @angular-devkit/build-angular@0.8.0-beta.3 | -- @ngtools/webpack@6.2.0-beta.3 -- @angular/cli@1.7.0-beta.2 `-- @ngtools/webpack@1.10.0-beta.2 如何删除这个

以上是关于检测到 Angular 编译器,但它是错误类的实例的主要内容,如果未能解决你的问题,请参考以下文章

Angular5 - 错误:使用无效数据调用函数 DocumentReference.set()。数据必须是一个对象,但它是:一个自定义的 PlatformModel 对象

Angular Cdk步进器,如何检测何时添加了新步骤

angular2-jwt 编译元数据解析器错误

如何解决使用 Angular 4 检测到的循环依赖?

我认为这是一个编译器错误,这不应该影响我的代码,但它是 [关闭]

最终类的编译器错误实例[重复]