升级Angular 17时npm install出现devDependency依赖解析错误求助
升级Angular 17时的依赖冲突解决问题
疑问与操作背景
- 已在仓库保存当前依赖版本,是否需要删除
node_modules后执行npm install? - 为升级到Angular 17,执行
ncu -u更新了package.json中的依赖版本,随后执行npm install时遇到devDependency冲突。
已尝试的操作及问题
- 首次
npm install错误:eslint@9.0.0与@typescript-eslint/parser@7.6.0存在peer依赖冲突,将"eslint": "^9.0.0"改为"eslint": "^8.56.0"后问题解决。 - 二次
npm install错误:@angular-devkit/build-angular@17.3.4与@angular/compiler-cli存在依赖冲突,将"@angular/compiler-cli": "^17.3.4"改为"@angular/compiler-cli": "^17.0.0"后问题仍未解决。
相关命令输出与文件内容
ncu -u执行结果
➜ client git:(master) ncu -u Upgrading /Users/charles/yogabandy2020/client/package.json [====================] 58/58 100% @angular-devkit/build-angular ^16.2.14 → ^17.3.4 @angular/animations ^16.2.12 → ^17.3.4 @angular/cdk ^15.1.2 → ^17.3.4 @angular/cli ^16.2.14 → ^17.3.4 @angular/common ^16.2.12 → ^17.3.4 @angular/compiler ^16.2.12 → ^17.3.4 @angular/compiler-cli ^16.2.12 → ^17.3.4 @angular/core ^16.2.12 → ^17.3.4 @angular/forms ^16.2.12 → ^17.3.4 @angular/google-maps ^15.2.9 → ^17.3.4 @angular/localize ^16.2.12 → ^17.3.4 @angular/platform-browser ^16.2.12 → ^17.3.4 @angular/platform-browser-dynamic ^16.2.12 → ^17.3.4 @angular/router ^16.2.12 → ^17.3.4 @auth0/angular-jwt ^5.1.2 → ^5.2.0 @cloudinary/ng ^1.11.2 → ^2.0.2 @cloudinary/url-gen ^1.9.2 → ^1.19.0 @fortawesome/fontawesome-free ^6.2.1 → ^6.5.2 @stripe/stripe-js ^1.48.0 → ^3.3.0 @types/jasmine ^4.3.1 → ^5.1.4 @types/jasminewd2 ^2.0.10 → ^2.0.13 @types/lodash ^4.14.191 → ^4.17.0 @types/node ^18.11.18 → ^20.12.7 @typescript-eslint/eslint-plugin ^5.49.0 → ^7.6.0 @typescript-eslint/parser ^5.49.0 → ^7.6.0 bootstrap ^5.2.3 → ^5.3.3 eslint ^8.32.0 → ^9.0.0 eslint-plugin-html ^7.1.0 → ^8.1.0 heic2any ^0.0.3 → ^0.0.4 jasmine-core ~4.5.0 → ~5.1.2 karma ~6.4.1 → ~6.4.3 karma-chrome-launcher ~3.1.1 → ~3.2.0 karma-jasmine-html-reporter ^2.0.0 → ^2.1.0 luxon ^3.2.1 → ^3.4.4 ng-gallery ^8.0.2 → ^11.0.0 ngx-bootstrap ^10.2.0 → ^12.0.0 ngx-image-cropper ^6.3.2 → ^7.2.1 ngx-mask ^15.0.3 → ^17.0.7 ngx-stripe ^15.5.0 → ^17.2.0 rxjs ~7.8.0 → ~7.8.1 swiper ^8.4.7 → ^11.1.1 ts-node ~10.9.1 → ~10.9.2 tslib ^2.5.0 → ^2.6.2 typescript ^4.9.4 → ^5.4.5 zone.js ^0.13.0 → ^0.14.4 Run npm install to install new versions. ➜ client git:(master) ✗
修改后的package.json
{ "name": "client", "version": "0.0.0", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e" }, "private": true, "dependencies": { "@angular/animations": "^17.3.4", "@angular/cdk": "^17.3.4", "@angular/common": "^17.3.4", "@angular/compiler": "^17.3.4", "@angular/core": "^17.3.4", "@angular/forms": "^17.3.4", "@angular/google-maps": "^17.3.4", "@angular/localize": "^17.3.4", "@angular/platform-browser": "^17.3.4", "@angular/platform-browser-dynamic": "^17.3.4", "@angular/router": "^17.3.4", "@auth0/angular-jwt": "^5.2.0", "@cloudinary/ng": "^2.0.2", "@cloudinary/url-gen": "^1.19.0", "@fortawesome/fontawesome-free": "^6.5.2", "@ngneat/content-loader": "^7.0.0", "@stripe/stripe-js": "^3.3.0", "bootstrap": "^5.3.3", "hammerjs": "^2.0.8", "heic2any": "^0.0.4", "image-filters": "^1.0.1", "lodash": "^4.17.21", "lodash-es": "^4.17.21", "luxon": "^3.4.4", "ng-gallery": "^11.0.0", "ngx-bootstrap": "^12.0.0", "ngx-image-cropper": "^7.2.1", "ngx-mask": "^17.0.7", "ngx-stripe": "^17.2.0", "ngx-webcam": "^0.4.1", "rxjs": "~7.8.1", "tslib": "^2.6.2", "tz-lookup": "^6.1.25", "zone.js": "^0.14.4" }, "devDependencies": { "@angular-devkit/build-angular": "^17.3.4", "@angular/cli": "^17.3.4", "@angular/compiler-cli": "^17.3.4", "@types/jasmine": "^5.1.4", "@types/jasminewd2": "^2.0.13", "@types/lodash": "^4.17.0", "@types/node": "^20.12.7", "@typescript-eslint/eslint-plugin": "^7.6.0", "@typescript-eslint/parser": "^7.6.0", "codelyzer": "^6.0.2", "eslint": "^9.0.0", "eslint-config-google": "^0.14.0", "eslint-plugin-html": "^8.1.0", "jasmine-core": "~5.1.2", "jasmine-spec-reporter": "~7.0.0", "karma": "~6.4.3", "karma-chrome-launcher": "~3.2.0", "karma-coverage-istanbul-reporter": "~3.0.3", "karma-jasmine": "~5.1.0", "karma-jasmine-html-reporter": "^2.1.0", "protractor": "~7.0.0", "swiper": "^11.1.1", "ts-node": "~10.9.2", "typescript": "^5.4.5" } }
首次npm install错误日志
➜ client git:(master) ✗ npm install npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: client@0.0.0 npm ERR! Found: eslint@9.0.0 npm ERR! node_modules/eslint npm ERR! dev eslint@"^9.0.0" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer eslint@"^8.56.0" from @typescript-eslint/parser@7.6.0 npm ERR! node_modules/@typescript-eslint/parser npm ERR! dev @typescript-eslint/parser@"^7.6.0" from the root project npm ERR! peer @typescript-eslint/parser@"^7.0.0" from @typescript-eslint/eslint-plugin@7.6.0 npm ERR! node_modules/@typescript-eslint/eslint-plugin npm ERR! dev @typescript-eslint/eslint-plugin@"^7.6.0" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! npm ERR! For a full report see: npm ERR! /Users/charles/.npm/_logs/2024-04-13T01_02_45_312Z-eresolve-report.txt npm ERR! A complete log of this run can be found in: /Users/charles/.npm/_logs/2024-04-13T01_02_45_312Z-debug-0.log ➜ client git:(master) ✗
二次npm install错误日志
➜ client git:(master) ✗ npm install npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: client@0.0.0 npm ERR! Found: @angular-devkit/build-angular@16.2.14 npm ERR! node_modules/@angular-devkit/build-angular npm ERR! dev @angular-devkit/build-angular@"^17.3.4" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! dev @angular-devkit/build-angular@"^17.3.4" from the root project npm ERR! npm ERR! Conflicting peer dependency: @angular/compiler-cli@17.3.4 npm ERR! node_modules/@angular/compiler-cli npm ERR! peer @angular/compiler-cli@"^17.0.0" from @angular-devkit/build-angular@17.3.4 npm ERR! node_modules/@angular-devkit/build-angular npm ERR! dev @angular-devkit/build-angular@"^17.3.4" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! npm ERR! For a full report see: npm ERR! /Users/charles/.npm/_logs/2024-04-13T01_27_59_149Z-eresolve-report.txt npm ERR! A complete log of this run can be found in: /Users/charles/.npm/_logs/2024-04-13T01_27_59_149Z-debug-0.log ➜ client git:(master) ✗
解决方案建议
- 清理旧依赖:删除
node_modules文件夹和package-lock.json文件,避免残留旧版本依赖干扰新安装。 - 统一Angular版本:确保所有Angular核心包(包括
@angular/compiler-cli)与@angular-devkit/build-angular版本完全一致,不要降级@angular/compiler-cli,保持其为^17.3.4——@angular-devkit/build-angular@17.3.4要求@angular/compiler-cli至少为17.0.0,版本不一致反而会引发冲突。 - 处理ESLint冲突:保持
eslint为^8.56.0,因为@typescript-eslint/parser@7.6.0暂不支持ESLint 9。若后续需升级ESLint,需同步升级@typescript-eslint/eslint-plugin和@typescript-eslint/parser到支持ESLint 9的版本。 - 重试安装:执行以下命令:
若仍有冲突,可尝试添加rm -rf node_modules package-lock.json npm install--legacy-peer-deps参数跳过严格的peer依赖检查,但需注意这可能带来潜在兼容性问题,仅作为临时解决方案。
内容的提问来源于stack exchange,提问作者chuckd
相关产品推荐
相关产品推荐

