从 8 升级到 9 后无法构建 Angular 项目

Posted

技术标签:

【中文标题】从 8 升级到 9 后无法构建 Angular 项目【英文标题】:Can't build Angular project after upgrade from 8 to 9 【发布时间】:2020-07-01 11:21:30 【问题描述】:

我已使用ng update 工具将我的 Angular 项目从 8 升级到 9。在本地(Windows)一切正常,但是当我尝试在 docker 中构建项目时,出现以下错误:

Step 6/11 : RUN npm run build
  ---> Running in d60f739a56f6
 > check-it-frontend@0.0.0 build /app
 > ng build --prod
 Compiling @angular/core : es2015 as esm2015
 ERROR in EINVAL: invalid argument, rename '/app/node_modules/@angular/core/core.d.ts' -> '/app/node_modules/@angular/core/core.d.ts.__ivy_ngcc_bak'
 npm ERR! code ELIFECYCLE
 npm ERR! errno 1
 npm ERR! check-it-frontend@0.0.0 build: `ng build --prod`
 npm ERR! Exit status 1
 npm ERR! 
 npm ERR! Failed at the check-it-frontend@0.0.0 build script.
 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
 npm ERR! A complete log of this run can be found in:
 npm ERR!     /root/.npm/_logs/2020-03-20T11_44_02_696Z-debug.log
 The command '/bin/sh -c npm run build' returned a non-zero code: 1

码头文件:

FROM node:13.10.1-stretch as base

RUN apt-get update -qq && apt-get install -y build-essential

ADD . /app
WORKDIR /app

RUN npm ci
RUN npm run build

依赖关系:

"dependencies": 
  "@angular/animations": "^9.0.7",
  "@angular/cdk": "^8.2.3",
  "@angular/common": "^9.0.7",
  "@angular/compiler": "^9.0.7",
  "@angular/core": "^9.0.7",
  "@angular/forms": "^9.0.7",
  "@angular/material": "^8.2.3",
  "@angular/platform-browser": "^9.0.7",
  "@angular/platform-browser-dynamic": "^9.0.7",
  "@angular/router": "^9.0.7",
  "core-js": "^2.4.1",
  "rxjs": "^6.5.4",
  "rxjs-compat": "^6.0.0-rc.0",
  "tslib": "^1.10.0",
  "zone.js": "~0.10.2"
,
"devDependencies": 
  "@angular-devkit/build-angular": "~0.900.7",
  "@angular/cli": "^9.0.7",
  "@angular/compiler-cli": "^9.0.7",
  "@angular/language-service": "^9.0.7",
  "@types/jasmine": "~2.8.3",
  "@types/jasminewd2": "~2.0.2",
  "@types/node": "^12.11.1",
  "codelyzer": "^5.1.2",
  "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",
  "node-sass": "^4.13.1",
  "protractor": "~5.1.2",
  "ts-node": "~4.1.0",
  "tslint": "~5.9.1",
  "typescript": "~3.7.5"

我能做些什么来解决它? Angular 8 的图像很好。我尝试了不同的节点图像,但没有任何帮助...

【问题讨论】:

当我通过npm install 安装cli 时,ng 命令不存在。我已经在我的 dockerfile 中修复了显式的旧版本,但它仍然不起作用。 谢谢,我不是节点开发人员,所以我不知道npm runnpm ci。无论如何,更改后仍然会引发相同的错误。 @Djent What is the difference between “npm install” and “npm ci”? 你有没有试过删除所有node_modules并通过npm i重新安装? 您可以尝试在您的package.json 文件中添加"postinstall": "ngcc" 吗? 【参考方案1】:

我不确定您的问题是什么或导致它的原因,但我快速搜索并偶然发现了这个github issue。它涉及并行构建(我不认为是你的情况),但我认为它仍然可以应用在这里,因为错误发生在角度核心的编译期间。

建议的解决方法是将角度兼容性编译器 (ngcc) 作为 postinstall 脚本运行,而不是让构建自行触发。

修改您的package.json 文件并将以下内容添加到scripts 部分:

"postinstall": "ngcc

【讨论】:

获取信息:angular.io/guide/ivy#ivy-and-universalapp-shell

以上是关于从 8 升级到 9 后无法构建 Angular 项目的主要内容,如果未能解决你的问题,请参考以下文章

升级到 9.0 和 angular 7 后修复 angular-redux/store

升级后或新项目无法构建 Angular 12 和 Webpack 5

v9升级后Angular应用程序冻结/无限加载而没有错误

从 Angular 7 升级到 Angular 8 后,我的管道测试停止工作:TypeError: undefined is not iterable

更新到 Angular 10 后,如果没有“new”,就无法调用类构造函数 UpgradeComponent

从 2.5.3 升级到 flutter 2.8.0 后无法运行 flutter run