You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NestJS后端更新遇ERESOLVE依赖冲突问题求助

依赖冲突问题:Mongoose 6.x 与 @typegoose/typegoose 7.x 不兼容

升级NestJS后端时,已将mongoose和NestJS升级至最新版本,但出现依赖冲突,无法解析依赖树。具体表现为项目安装的mongoose@6.5.1与@typegoose/typegoose@7.6.3要求的peer依赖mongoose@5.10.0 - 5.10.18不兼容。

错误日志

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: server@0.0.2
npm ERR! Found: mongoose@6.5.1
npm ERR! node_modules/mongoose
npm ERR!   mongoose@"6.5.1" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer mongoose@"5.10.0 - 5.10.18" from @typegoose/typegoose@7.6.3
npm ERR! node_modules/@typegoose/typegoose
npm ERR!   @typegoose/typegoose@"^7.4.8" 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 /Users/XX/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/XX/.npm/_logs/2022-08-07T19_52_18_976Z-debug-0.log

package.json 配置

{  
  "name": "server",  
  "version": "0.0.2",  
  "description": "",  
  "author": "",  
  "license": "MIT",  
  "scripts": {    
    "build": "tsc -p tsconfig.build.json",    
    "format": "prettier --write \"src/**/*.ts\"",    
    "start": "ts-node -r tsconfig-paths/register src/main.ts",    
    "start:dev": "concurrently --handle-input \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" ",    
    "start:debug": "nodemon --config nodemon-debug.json",    
    "prestart:prod": "rimraf dist && npm run build",    
    "start:prod": "node dist/main.js",    
    "lint": "tslint -p tsconfig.json -c tslint.json",    
    "test": "jest",    
    "test:watch": "jest --watch",    
    "test:cov": "jest --coverage",    
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",    
    "test:e2e": "jest --config ./test/jest-e2e.json"  
  },  
  "dependencies": {    
    "@nestjs/common": "^9.0.8",    
    "@nestjs/core": "^9.0.8",    
    "@nestjs/jwt": "^9.0.0",    
    "@nestjs/mongoose": "^9.2.0",    
    "@nestjs/passport": "^9.0.0",    
    "@nestjs/platform-express": "^9.0.8",    
    "@typegoose/typegoose": "^7.4.8",    
    "axios": "^0.27.2",    
    "bcrypt": "^5.0.1",    
    "mongoose": "6.5.1",    
    "nestjs-typegoose": "^7.1.38",    
    "passport": "^0.6.0",    
    "passport-jwt": "^4.0.0",    
    "reflect-metadata": "^0.1.12",    
    "rimraf": "^2.6.2",    
    "rxjs": "^7.5.6"  
  },  
  "devDependencies": {    
    "@nestjs/testing": "^9.0.8",    
    "@types/express": "^4.16.0",    
    "@types/jest": "^23.3.13",    
    "@types/mongoose": "^5.5.6",    
    "@types/node": "^10.12.18",    
    "@types/supertest": "^2.0.7",    
    "concurrently": "^7.3.0",    
    "jest": "^28.1.3",    
    "nodemon": "^2.0.19",    
    "prettier": "^1.15.3",    
    "supertest": "^3.4.1",    
    "ts-jest": "^28.0.7",    
    "ts-node": "8.1.0",    
    "tsconfig-paths": "3.8.0",    
    "tslint": "5.16.0",    
    "typescript": "3.4.3",    
    "wait-on": "^3.2.0"  
  },  
  "jest": {    
    "moduleFileExtensions": [      
      "js",      
      "json",      
      "ts"    
    ],    
    "rootDir": "src",    
    "testRegex": ".spec.ts$",    
    "transform": {      
      "^.+\\.(t|j)s$": "ts-jest"    
    },    
    "coverageDirectory": "../coverage",    
    "testEnvironment": "node"  
  }
}

解决方案

1. 升级@typegoose/typegoose到兼容Mongoose 6.x的版本

@typegoose/typegoose从v8.x版本开始支持Mongoose 6.x,同时需要同步升级nestjs-typegoose到对应的兼容版本:

npm install @typegoose/typegoose@^8.0.0 nestjs-typegoose@^8.0.0

注意:升级后需检查代码是否存在API变更,v8版本可能包含破坏性更新。

2. 降级Mongoose到@typegoose/typegoose 7.x支持的版本

若暂时不想升级typegoose,可将mongoose降级至5.10.x范围内的版本(如5.10.18):

npm install mongoose@5.10.18

注意:当前使用的@nestjs/mongoose@9.2.0要求Mongoose 6.x+,因此降级mongoose后需同步降级@nestjs/mongoose至8.x版本(兼容Mongoose 5.x):

npm install @nestjs/mongoose@^8.0.0

3. 临时跳过依赖检查(不推荐)

使用npm参数强制跳过依赖冲突检查,但可能导致运行时错误:

npm install --legacy-peer-deps
# 或
npm install --force

内容的提问来源于stack exchange,提问作者Samy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 15:36:20