npm依赖树解析失败(ERESOLVE错误):版本冲突修复求助
问题背景
项目更新依赖时出现ERESOLVE依赖树解析错误,已尝试npm audit fix、npm install/update --force、npm install/update --legacy-peer-deps均无效。
错误日志
npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: grocery-shop@0.0.0 npm ERR! Found: @typescript-eslint/eslint-plugin@5.47.1 npm ERR! node_modules/@typescript-eslint/eslint-plugin npm ERR! dev @typescript-eslint/eslint-plugin@"^5.47.1" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer @typescript-eslint/eslint-plugin@"^4.0.1" from eslint-config-standard-with-typescript@21.0.1 npm ERR! node_modules/eslint-config-standard-with-typescript npm ERR! dev eslint-config-standard-with-typescript@"^21.0.1" 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! See /home/pk/.npm/eresolve-report.txt for a full report. npm ERR! A complete log of this run can be found in: npm ERR! /home/pk/.npm/_logs/2022-12-29T13_36_27_051Z-debug-0.log
项目依赖配置(package.json devDependencies)
{ "@nestjs/schematics": "^9.0.0", "@nestjs/testing": "^9.0.0", "@nrwl/eslint-plugin-nx": "15.3.3", "@nrwl/jest": "15.3.3", "@nrwl/linter": "15.3.3", "@nrwl/nest": "^15.3.3", "@nrwl/node": "15.3.3", "@nrwl/nx-cloud": "latest", "@nrwl/workspace": "15.3.3", "@types/jest": "28.1.1", "@types/node": "18.7.1", "@typescript-eslint/eslint-plugin": "^5.47.1", "@typescript-eslint/parser": "^5.36.1", "eslint": "^8.30.0", "eslint-config-prettier": "8.1.0", "eslint-config-standard-with-typescript": "^21.0.1", "eslint-plugin-n": "^15.6.0", "eslint-plugin-promise": "^6.1.1", "husky": "^8.0.0", "jest": "28.1.1", "jest-environment-jsdom": "28.1.1", "lint-staged": "^13.1.0", "nx": "15.3.3", "prettier": "^2.6.2", "prisma": "^4.8.0", "ts-jest": "28.0.5", "ts-node": "10.9.1", "typescript": "^4.9.4" }
冲突分析
核心冲突是两个包的版本不兼容:
- 项目直接依赖的
@typescript-eslint/eslint-plugin版本为^5.47.1(TypeScript ESLint v5版本) eslint-config-standard-with-typescript@21.0.1的peer依赖要求@typescript-eslint/eslint-plugin为^4.0.1(TypeScript ESLint v4版本)
两者属于大版本差异,npm无法自动解析兼容版本,导致依赖树构建失败。
修复方案
方案一:升级eslint-config-standard-with-typescript到兼容v5的版本
eslint-config-standard-with-typescript从v22.x版本开始支持TypeScript ESLint v5。将其版本修改为^22.0.0或更高,同时建议将@typescript-eslint/parser和eslint-plugin保持同版本,避免额外冲突:
修改package.json中的对应字段:
"eslint-config-standard-with-typescript": "^22.0.0", "@typescript-eslint/eslint-plugin": "^5.47.1", "@typescript-eslint/parser": "^5.47.1"
执行命令重新安装:
npm install
方案二:降级@typescript-eslint相关包到v4版本
如果不想升级eslint-config-standard-with-typescript,可以将@typescript-eslint/eslint-plugin和@typescript-eslint/parser降级到v4.x版本,满足peer依赖要求:
修改package.json:
"@typescript-eslint/eslint-plugin": "^4.33.0", "@typescript-eslint/parser": "^4.33.0"
执行命令重新安装:
npm install
方案三:使用npm overrides强制兼容(不推荐,仅限临时应急)
如果上述方案都无法实施,可以在package.json中添加overrides字段,强制指定eslint-config-standard-with-typescript使用v5版本的@typescript-eslint/eslint-plugin:
"overrides": { "eslint-config-standard-with-typescript": { "@typescript-eslint/eslint-plugin": "^5.47.1" } }
执行安装命令:
npm install
注意:此方法可能导致eslint-config-standard-with-typescript运行异常,仅作为临时解决方案。
验证修复
安装完成后,执行ESLint检查验证是否正常工作:
npx eslint .
内容的提问来源于stack exchange,提问作者displaynone

