npm安装时出现了“无法解决依赖树”的错误提示。

10

我正在尝试执行npm install,但是遇到了以下错误。

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: webapp@21.0.0
npm ERR! Found: @angular/cdk@9.2.4
npm ERR! node_modules/@angular/cdk
npm ERR!   @angular/cdk@"9.2.4" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer @angular/cdk@"8.2.3" from @angular/material@8.2.3
npm ERR! node_modules/@angular/material
npm ERR!   @angular/material@"8.2.3" 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.
4个回答

9

我将node.js更新到了最新的推荐LTS版本,目前是14版,这就是问题的原因。

我尝试使用--force--legacy-peer-deps,但并没有起作用。

解决方案是将node版本降级到12版。

下面是一段复制/粘贴的解决方案。请将所有代码复制并粘贴到项目根目录下的终端中(即您要安装的package.json文件所在的位置)。

rm package-lock.json
rm -rfv node_modules
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install 12
nvm use 12
npm i

这里发生了什么?

  1. 移除 package-lock.json 文件
  2. 移除 node_modules 目录 (-rfv = 递归,强制,详细模式)
  3. 安装 nvm (Node Version Manager)
  4. 配置 nvm
  5. 加载 nvm
  6. 使用 nvm 安装 node v12
  7. 告诉此项目使用 node v12
  8. 使用 npm 安装包

7
尝试执行 npm install --legacy-peer-deps 命令。这对我很有效。但在此之前,请确保删除你的 package-lock.jsonnode_modules 文件夹。

太好了,@Franklin Ukaegbu!我将其与Codo Creed的最后两个建议结合起来。 - MiB

2
npm i package-name -f

如果上述命令无法解决错误,则在终端中使用以下两个命令:
npm config set legacy-peer-deps true
npm cache clean --force

0
你遇到了依赖冲突的问题。忽略它并安装npm包。尝试一下这个方法:
npm install YourPackage --force

或者

npm install YourPackage --legacy-peer-deps

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接