Yarn 4.3.1下typescript-eslint未提供typescript依赖问题求助
问题描述
使用Yarn 4.3.1安装依赖后,收到提示:
部分peer依赖被依赖项错误地满足;运行yarn explain peer-requirements查看详情。
执行yarn explain peer-requirements后,关键报错信息如下:
... (dependencies working) ... pd8655 → ✘ @typescript-eslint/utils@npm:7.17.0 [eb8b2] doesn't provide typescript to @typescript-eslint/typescript-estree@npm:7.17.0 [05a60] and 1 other dependency ... (dependencies working) ...
进一步执行yarn explain peer-requirements pd8655,得到详细依赖链:
Package @typescript-eslint/utils@npm:7.17.0 [eb8b2] is requested to provide typescript by its descendants @typescript-eslint/utils@npm:7.17.0 [eb8b2] └─ @typescript-eslint/typescript-estree@npm:7.17.0 [05a60] (via *) └─ ts-api-utils@npm:1.3.0 [b4f1c] (via >=4.2.0) ✘ Package @typescript-eslint/utils@npm:7.17.0 [eb8b2] does not provide typescript.
项目的package.json配置如下:
{ "name": "backend-ts", "packageManager": "yarn@4.3.1", "devDependencies": { "@types/eslint": "^9.6.0", "@types/node": "^20.14.11", "@types/webpack": "^5.28.5", "eslint": "^8.56.0", "eslint-config-prettier": "^9.1.0", "eslint-webpack-plugin": "^4.2.0", "prettier": "^3.3.3", "ts-loader": "^9.5.1", "ts-node": "^10.9.2", "typescript": "^5.5.4", "typescript-eslint": "^7.17.0", "webpack": "^5.93.0", "webpack-cli": "^5.1.4" } }
已安装TypeScript@5.5.4,且版本符合typescript-eslint官方兼容范围,仍出现此peer依赖错误,需明确原因及解决办法。
问题原因
这个报错源于Yarn 4的peer依赖解析逻辑特性:
@typescript-eslint/typescript-estree依赖ts-api-utils,而ts-api-utils需要typescript作为peer依赖- Yarn 4的解析链中,错误地要求
@typescript-eslint/utils(typescript-estree的上游依赖)提供typescript,但实际上typescript是项目根目录的直接依赖,并非utils包的依赖 - 本质是Yarn 4对嵌套peer依赖的解析规则与旧版本不同,出现了误判
解决方法
方法1:通过packageExtensions修正依赖规则
在项目根目录的.yarnrc.yml中添加以下配置,告知Yarn让@typescript-eslint/utils继承项目根的typescript依赖:
packageExtensions: "@typescript-eslint/utils@*": peerDependencies: typescript: "*"
添加后执行yarn install,Yarn会正确识别项目根的typescript来满足依赖链的peer需求。
方法2:降级Yarn版本
如果不想修改配置,可以暂时降级到Yarn 3.x版本,Yarn 3.x的peer依赖解析逻辑不会出现此类误判:
yarn set version 3.6.4 yarn install
方法3:通过resolutions强制指定依赖关系
在package.json中添加resolutions字段,强制指定@typescript-eslint/typescript-estree使用项目根的typescript:
{ "resolutions": { "@typescript-eslint/typescript-estree/typescript": "^5.5.4" } }
添加后执行yarn install即可。
内容的提问来源于stack exchange,提问作者dgnin
相关产品推荐
相关产品推荐

