发生未处理的异常:无法找到模块“@angular/compiler-cli/ngcc”。

4

我刚开始我的第一份工作,被分配到更新一个旧项目。

我拥有了一台新电脑,所以我安装了node.js和Visual Studio的最新版本。

正如我所提到的,我正在WebStorm上处理一个旧项目,并且遇到了以下错误:

发生未处理的异常:找不到模块 '@angular/compiler-cli/ngcc'

当我搜索时,我发现一些解决方案涉及到 @angular/compiler-cli 而不是 @angular/compiler-cli/ngcc

我尝试过:

  • 强制清除Node_Module和缓存。
  • npm uninstall @angular/compiler-cli 并重新安装。
  • 尝试手动更新,但没有解决问题。

这是我的 package.json 文件:

{
  "name": "retail-report-mobile",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "start:en": "ng serve --aot --i18nFile=src/locale/language.en.xlf --i18nFormat=xlf --locale en",
    "build": "ng build --prod --build-optimizer --aot",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.1.7",
    "@angular/cdk": "^6.4.2",
    "@angular/common": "^6.1.0",
    "@angular/compiler": "^6.1.0",
    "@angular/core": "^6.1.0",
    "@angular/forms": "^6.1.0",
    "@angular/http": "^6.1.0",
    "@angular/material": "^6.4.2",
    "@angular/material-moment-adapter": "^7.3.0",
    "@angular/platform-browser": "^6.1.0",
    "@angular/platform-browser-dynamic": "^6.1.0",
    "@angular/router": "^6.1.0",
    "@ngx-translate/core": "^10.0.2",
    "@swimlane/ngx-charts": "^10.0.0",
    "angular-font-awesome": "^3.1.2",
    "angular-mat-datepicker": "0.0.2",
    "angular-webstorage-service": "^1.0.2",
    "bootstrap": "^4.1.3",
    "buffer": "^5.2.1",
    "core-js": "^2.5.4",
    "devextreme": "^18.1.4",
    "devextreme-angular": "^18.1.4",
    "events": "^3.0.0",
    "font-awesome": "^4.7.0",
    "ngx-bootstrap": "^3.0.1",
    "ngx-spinner": "^6.1.2",
    "ngx-toastr": "^9.0.2",
    "ngx-translate-cache": "^0.1.0",
    "node-sass": "^4.10.0",
    "rxjs": "^6.0.0",
    "stream": "0.0.2",
    "timers": "^0.1.1",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.803.5",
    "@angular/cli": "^8.3.5",
    "@angular/compiler-cli": "^7.2.15",
    "ts-loader": "^4.0.1",
    "typescript": "^3.1.6"
  }
}

我的 "npm 版本"

{ 'retail-report-mobile': '0.0.0',
  npm: '6.11.3',
  ares: '1.15.0',
  brotli: '1.0.7',
  cldr: '35.1',
  http_parser: '2.8.0',
  icu: '64.2',
  modules: '64',
  napi: '4',
  nghttp2: '1.39.2',
  node: '10.16.3',
  openssl: '1.1.1c',
  tz: '2019a',
  unicode: '12.1',
  uv: '1.28.0',
  v8: '6.8.275.32-node.54',
  zlib: '1.2.11' }

我的 "ng version"

Angular CLI: 8.3.5
Node: 10.16.3
OS: win32 x64
Angular: 6.1.10
... animations, common, compiler, core, forms, http
... platform-browser, platform-browser-dynamic, router

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.803.5
@angular-devkit/build-angular      0.803.5
@angular-devkit/build-optimizer    0.803.5
@angular-devkit/build-webpack      0.803.5
@angular-devkit/core               7.3.9
@angular-devkit/schematics         7.3.9
@angular/cdk                       6.4.7
@angular/cli                       8.3.5
@angular/compiler-cli              7.2.15
@angular/material                  6.4.7
@angular/material-moment-adapter   7.3.7
@ngtools/webpack                   8.3.5
@schematics/angular                8.3.4
@schematics/update                 0.803.5
rxjs                               6.5.3
typescript                         3.5.3
webpack                            4.39.2

我的错误日志

[error] Error: Cannot find module '@angular/compiler-cli/ngcc'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (C:\Users\***\Desktop\***\***\node_modules\@ngtools\webpack\src\ngcc_processor.js:10:16)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (C:\Users\***\Desktop\***\***\node_modules\@ngtools\webpack\src\angular_compiler_plugin.js:23:26)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)

非常抱歉如果有语法错误,感谢您的帮助。

2个回答

5
我认为您的问题与版本有关。 我的意思是,您正在使用许多不同的版本,它们可能无法很好地协同工作。
让我解释一下:
Angular CLI:8.3.5(最新版本)
但是对于cdk,一年前的版本为: "@angular/cdk": "^6.4.2", "@angular/common": "^6.1.0"
typescript:3.5.3(当时可能不存在angular 6.4)
尝试我的建议:(我曾经在旧项目上苦苦挣扎)
1. 幸运的话:只需运行ng update --all=true --force=true(查看 documentation 获取正确的语法),它可能有效,即使它抛出了一些异常,它也提供了引用有问题的指南。 2. 使用"ng new projectname"创建一个新的angular项目。

a. 使用"ng serve"启动您的项目。如果它正常工作,您可以确信您的npmnode工作正常(这非常重要,因为它消除了许多错误可能性)。(如果不行,重新安装Windows几乎是最快的方法)

b. 在拥有一个可用模板后,只需开始添加其他引用以确保您没有版本不匹配的情况(并且不要忘记ng serve和观察输出结果)。如果出现版本不匹配,请尝试升级它们,或者如果不可能,则删除它们(如果它是旧库,则必须替换它)。

c. 如果没有问题,那么将您的旧代码移至此新文件夹,并尝试消除错误。


我会尝试您的建议,并在我开始项目后尽快告知结果。谢谢 ^^ - bkardo
我已经尝试了第一个选项。现在我遇到了这个错误:“无法从C:/Users//Desktop/abc111/abc/node_modules/devextreme-angular/ui/accordion.js写入对DxiItemComponent的引用到C:/Users//Desktop/abc111/abc/node_modules/devextreme-angular/ui/nested/item-dxi.js”。 - bkardo
我没有见过这个错误,让我们试一下,只需删除(或重命名)你的 node_modules 文件夹并重试。 - Amirreza
“lucky shot” 命令已经生效。在 polyfills.ts 文件中包含 “import @angular/localize/init;” 也起到了作用。这就是诀窍。谢谢!!! - Utkarsh Mankad
ng update --all=true --force=true 绝对帮助我摆脱了错误,尝试将旧项目上的 package.json 移动到 Angular 8。 - StackOverflowUser

1

在构建esm模块时,我遇到了相同的NGCC错误。我通过简单地将tsconfig.json中的compilerOptions的目标从es5更改为es2015来解决了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接