unable to resolve dependency tree 问题解析与解决

2,883 阅读2分钟

报错信息如下:


npm ERR! code ERESOLVE

npm ERR! ERESOLVE unable to resolve dependency tree

npm ERR!

npm ERR! Found: react@16.14.0

npm ERR! node_modules/react

npm ERR! peer react@">= 16" from react-scripts@5.0.1

npm ERR! node_modules/react-scripts

npm ERR! react-scripts@"5.0.1" from admin-site@0.1.0

npm ERR! packages/admin-site

npm ERR! react-scripts@"5.0.1" from customer-site@0.1.0

npm ERR! packages/customer-site

npm ERR! peer react@">=16.0.0" from antd@3.1.0

npm ERR! node_modules/antd

npm ERR! antd@"3.1.0" from admin-site@0.1.0

npm ERR! packages/admin-site

npm ERR! antd@"3.1.0" from customer-site@0.1.0

npm ERR! packages/customer-site

npm ERR! 5 more (react-lazy-load, rc-editor-mention, antd, react-dom, react-slick)

npm ERR!

npm ERR! Could not resolve dependency:

npm ERR! peer react@"^18.0.0" from @testing-library/react@13.4.0

npm ERR! node_modules/@testing-library/react

npm ERR! @testing-library/react@"^13.4.0" from admin-site@0.1.0

npm ERR! packages/admin-site

npm ERR! @testing-library/react@"^13.4.0" from customer-site@0.1.0

npm ERR! packages/customer-site

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/zhangxing/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:

npm ERR! /Users/zhangxing/.npm/_logs/2023-02-03T08_43_33_459Z-debug.log

lerna ERR! npm install exited 1 in 'root'

lerna ERR! npm install exited 1 in 'root'

npm ERR! code 1

npm ERR! path /Users/zhangxing/Desktop/lerna-monorepo-demo/packages

npm ERR! command failed

npm ERR! command sh -c lerna bootstrap --hoist

npm ERR! A complete log of this run can be found in:

npm ERR! /Users/zhangxing/.npm/_logs/2023-02-03T08_43_33_555Z-debug.log

解析

以上的信息看起来很复杂,但其实我们只需要关注这三行就够了


npm ERR! Found: react@16.14.0


npm ERR! Could not resolve dependency:


npm ERR! peer react@"^18.0.0" from @testing-library/react@13.4.0

意思就是 npm 为我们找到了 @16.14.0 这个版本的 react, 但是 @testing-library/react@13.4.0 这个包需要的 react 的最低版本是 @18.0.0, 所以 npm 傻眼了😂。

解决

我的项目里因为 @testing-library/react 这个包不需要所以直接删掉,然后把 package-lock.json 文件删掉,再次安装就可以了。

如果是不能删掉呢?

再来看下以上报错信息,先看看 npm 为什么找到了 @16.14.0 这个版本的 react,看这两段:


npm ERR! peer react@">= 16" from react-scripts@5.0.1


npm ERR! peer react@">=16.0.0" from antd@3.1.0

npm ERR! node_modules/antd

意思就是 react-scripts@5.0.1 的同等依赖的 react 版本需要满足 >= 16, antd@3.1.0 的同等依赖的 react 版本需要满足 >= 16.0.0, 然后 npm 找到了 @16.14.0

这个时候我们有两种解决办法:

  1. 提升 antd 或者 react-scripts 的版本直到 npm 根据它们的同等依赖的 react 版本找到一个 >= 18.0.0 的版本

  2. 降低 @testing-library/react 的版本直到 npm 根据它的同等依赖的 react 版本找到一个 >= 16.0.0 的版本