跳到主内容

Fix the upstream dependency conflict, or retry this command with --force, or --legacy-peer-deps

· 2分钟阅读

今天升级了 node 新版本,在项目安装 node_modules 依赖,突然报了以下错误

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! Could not resolve dependency:
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! A complete log of this run can be found in:
npm ERR! /Users/xxx/.npm/_logs/2021-12-28T09_24_00_473Z-debug-0.log

原因

主要的原因是 npm7 以上的版本,新增了一个对等依赖的特性,在以前的版本上,只会显示警告,但是还是可以安装成功,然而在新的版本是,npm 安装依赖则会直接报错,无法安装,这也是为了应用更加健壮而加的新的特性吧!

解决方法

如果要保持旧的安装模块方法,可以尝试 npm i 加入 --legacy-peer-deps 参数,这个参数保持跟旧版本一样的安装模块,忽略对等依赖

npm i --legacy-peer-deps

除了 --legacy-peer-deps 选项外,还可以使用 --force 饶过冲突

npm i --force