将Node从v14.17.6升级到v18.16.1时遭遇依赖解析失败问题
Node 14升级至18后的npm依赖冲突问题及解决方案请求
项目当前使用Node 14.17.6,计划升级至v18.16.1。升级后执行npm install时出现如下ERESOLVE错误:
npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: @nestjs/graphql@10.1.1 npm ERR! Found: ts-morph@10.1.0 npm ERR! node_modules/ts-morph npm ERR! ts-morph@"^10.0.1" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peerOptional ts-morph@"^13.0.2 || ^14.0.0 || ^15.0.0" from @nestjs/graphql@10.1.1 npm ERR! node_modules/@nestjs/graphql npm ERR! @nestjs/graphql@"^10.1.1" from the root project npm ERR! peer @nestjs/graphql@"^10.0.0" from @nestjs/apollo@10.1.0 npm ERR! node_modules/@nestjs/apollo npm ERR! @nestjs/apollo@"^10.1.0" from the root project npm ERR! npm ERR! Conflicting peer dependency: ts-morph@15.1.0 npm ERR! node_modules/ts-morph npm ERR! peerOptional ts-morph@"^13.0.2 || ^14.0.0 || ^15.0.0" from @nestjs/graphql@10.1.1 npm ERR! node_modules/@nestjs/graphql npm ERR! @nestjs/graphql@"^10.1.1" from the root project npm ERR! peer @nestjs/graphql@"^10.0.0" from @nestjs/apollo@10.1.0 npm ERR! node_modules/@nestjs/apollo npm ERR! @nestjs/apollo@"^10.1.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!
已将ts-morph从10.1.0升级到13.0.2,但问题仍未解决,反而出现其他依赖错误。
当前项目的核心依赖版本如下:
"@apollo/gateway": "^2.1.1", "@nestjs/apollo": "^10.1.0", "@nestjs/common": "^9.0.11", "@nestjs/config": "^2.2.0", "@nestjs/core": "^9.0.11", "@nestjs/graphql": "^10.1.1", "@newrelic/apollo-server-plugin": "^2.0.1", "apollo-server-express": "^3.10.2", "apollo-server-plugin-query-complexity": "^1.0.0", "graphql": "^16.6.0",
尝试使用ncu工具执行ncu -u更新所有npm包版本后,运行npm i仍未解决问题,出现如下错误:
npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: user-service@0.0.1 npm ERR! Found: @nestjs/apollo@10.1.0 npm ERR! node_modules/@nestjs/apollo npm ERR! @nestjs/apollo@"^12.0.7" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! @nestjs/apollo@"^12.0.7" from the root project npm ERR! npm ERR! Conflicting peer dependency: @nestjs/common@10.1.0 npm ERR! node_modules/@nestjs/common npm ERR! peer @nestjs/common@"^9.3.8 || ^10.0.0" from @nestjs/apollo@12.0.7 npm ERR! node_modules/@nestjs/apollo npm ERR! @nestjs/apollo@"^12.0.7" 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.
可行解决方案
1. 对齐NestJS生态版本(核心方案)
NestJS各包版本强关联,全量更新易引发跨版本冲突,建议分步调整:
- 先升级
@nestjs/common和@nestjs/core到9.x系列最新稳定版(如^9.4.3),该版本兼容@nestjs/apollo@10.x和@nestjs/graphql@10.x,同时满足@nestjs/apollo@12.x的最低peer依赖要求 - 将
ts-morph直接升级到^15.1.0,满足@nestjs/graphql@10.1.1的peerOptional要求 - 若不想升级NestJS核心版本,可将
@nestjs/apollo降级回^10.1.0,保持和@nestjs/graphql@10.1.1的版本匹配
调整后的依赖示例:
"@nestjs/common": "^9.4.3", "@nestjs/core": "^9.4.3", "@nestjs/apollo": "^10.1.0", "@nestjs/graphql": "^10.1.1", "ts-morph": "^15.1.0"
2. 清理缓存后重新安装
依赖冲突常因缓存或锁文件异常导致,执行以下步骤:
- 删除
node_modules文件夹和package-lock.json文件 - 执行
npm cache clean --force清理npm缓存 - 运行
npm install重新安装依赖
3. 临时应急方案(不推荐长期使用)
若需快速推进升级,可使用npm install --legacy-peer-deps跳过peer依赖检查,但此方案可能引入潜在兼容性问题,后续需及时解决依赖冲突。
内容的提问来源于stack exchange,提问作者sonnet
相关产品推荐
相关产品推荐

