安装npm包时出现无法解析依赖树的错误

720

尝试使用npm i命令安装npm软件包时,我遇到了以下异常:

图片描述

我已尝试重新安装Node.js软件包并通过以下方式关闭代理:

set HTTP_PROXY=
set HTTPS_PROXY=
问题依然存在。我做错了什么吗? 更新: 当我运行以下命令时:
npm install --legacy-peer-deps
以下错误显示:

在此输入图片描述


5
请展示你的package.json文件 - 看起来你已经升级了@angular/core,但没有升级@angular/http? - Adam Jenkins
package-lock.json文件不可用。是的,我已经尝试删除node_modules,但没有成功。 - Pearl
1
这个问题还在发生吗?你能分享一下 package.json 文件吗? - Supun Praneeth
这个问题解决了吗?如果是的话,如果任何答案对您有帮助,请将其中一个标记为“已接受”。否则,如果您需要更多澄清,请告诉我们。 - Eliezer Berlin
对我来说,由于我有备份,所以我删除了本地仓库并进行了全新的拉取,然后再次尝试 npm install,这对我起作用了。 - Huzaifa
显示剩余7条评论
37个回答

2
  1. If you have node_modules folder and package-lock.json file in your root directory then remove those:

    rm -r node_modules
    rm package-lock.json
    
  2. Then run commands:

    npm install --save --legacy-peer-deps
    npm audit fix --force
    
  3. Create .env file in the root directory and paste below code:

    SKIP_PREFLIGHT_CHECK=true
    
  4. Now, start your project:

    npm start
    

2

我曾多次遇到此问题,最终找到了解决方案:

npm install react-native-paper  --legacy-peer-deps

2

使用 Angular 13 为我安装单词,命令为 npm install --save --legacy-peer-deps。


我在Ubuntu上没有Word。 - Oleg Ushakov

2

我刚刚更新了我的Node.js,现在它可以正常工作:

node -v

输出:

V xxxx

并且:

sudo npm install -g n

使用此命令安装稳定版本的Node。
sudo n stable

1
我们遇到了同样的问题,导致出现以下错误:

npm ERR!code ERESOLVE npm
ERR!ERESOLVE 无法解析 npm
ERR! npm
ERR! 在解析时:@angular/material-moment-adapter@12.1.4 npm
ERR!找到:@angular/material@12.0.6 npm
ERR! node_modules/@angular/material npm
ERR! @angular/material@"~12.0.4" 来自根项目
...

我们在 Azure-Pipelines 中使用 npm ci 进行干净的安装。
问题经常出现在 package.json 和 package-lock.json 不再同步的情况下。
解决方法是在本地执行 npm install 并推送新的 package-lock.json
作为额外提示,我们在管道中添加了一个新任务,以获取有关作业失败的其他信息。
 - task: Npm@1
    displayName: npm install
    inputs:
      command: custom
      customCommand: ci
      customRegistry: useNpmrc

  # ##vso[task.logissue type=error] writes the text to the summary page (error-log).
  - bash: echo "##vso[task.logissue type=error] If 'npm install' fails with 'ERESOLVE could not resolve', 'package.json' and 'package-lock.json' (needed for 'npm ci') may be out of sync. Run 'npm install' locally and push the new package-lock.json."
    condition: failed() # Only execute on fail
    displayName: npm install failed hint

1
如果有帮助的话,我在推送更新包和 package-lock JSON 文件时,在 GitHub actions 上遇到了这个错误。问题出在我们内部 NPM 包的不一致性上。我不得不添加:
legacy-peer-deps=true

在我们的

.npmrc

文件中覆盖依赖关系。

因此,我认为有时候最好使用legacy-peer-deps来覆盖项目包中不需要的依赖关系。


1
在这种情况下,我遇到了一个问题:
ERESOLVE 无法解析依赖树
。这是在一个使用来自 Azure DevOps 私有 npm 源的一些包的 Angular 13 项目中发生的。
为了访问这个仓库,我创建了一个 .npmrc 文件。因此,npm install 命令会在我的私有仓库中搜索所有的包,而不再是在 npm 源中。由于 npm install 命令找不到许多托管在 npm 源中而不是我的私有源中的包,所以出现了无法解析依赖树的错误。
我找到了这个关于如何限定包范围的绝妙答案
基于此,我做了一些更改:
  1. In my library Package.json, update the name to have a scope name @mylib

    "name": "@myLib/command-queue",
    
  2. Build and publish this package to my private feed

  3. In my client app (the one that uses this package), update the .npmrc file to use my private feed for packages in this scope only

    @myLib:registry=https://pkgs.dev.azure.com/...
    always-auth=true
    
现在,每当我运行命令npm install时,如果包的作用域为@myLib,则会在我的私有源中查找它,并针对所有其他情况使用npm源(即@angular/...)。
这是我的客户端应用程序的package.json文件示例:
    "@angular/platform-browser-dynamic": "~13.3.0",
    "@angular/router": "~13.3.0",        <-- this comes from npm
    "@myLib/jcg-command-queue": "^2.2.0", <-- This comes from my private feed

此外,随着这个变化,再也不需要在npm安装命令中添加--legacy-peer-deps了。

1

我在我的Macbook Pro 2016上执行了这个命令,它对我起作用了。

> npm install --legacy-peer-deps

那么

> npm install

0
这里大部分的答案都指出了两个解决方案。
npm i --force

并且

npm i -legacy-peer-deps

当然,它们是一个快速的解决方案。让我们了解一下它们实际上是做什么的。 npm i --force 命令是与 Node Package Manager (npm) 一起使用的,用于强制安装包。当你运行这个命令时,npm 将安装指定的包,即使它们与你的项目的当前版本不兼容,或者存在与其他依赖项冲突的潜在问题。
当你使用 npm i --legacy-peer-deps 时,npm 将使用旧的、更灵活的算法来解决对等依赖关系,这类似于 npm 版本 4 及更早版本中解决对等依赖关系的方式。这在某些情况下很有帮助,例如包没有更新以声明与 npm 5 引入的更严格的对等依赖关系规则的兼容性。而且,这个问题在你的项目中仍然存在。每当你尝试安装任何其他项目时,你都必须扩展安装选项 --force--legacy-peer-deps
如果有其他不可打破的解决方案,这两个选项都不被推荐使用。
我在大多数项目中也使用了这个方法。但最近,我尝试了一种不同的方式。让我解释一下我做了什么。
  1. 删除 node_modulespackage-lock.json
  2. 运行 npm cache clean --force
  3. package.json 中的所有包列在记事本中。
  4. 然后从 package.json 中移除依赖项。
  5. 随后,手动安装所有依赖项,例如 npm i package1 package2
  6. 之后,我需要解决一些版本升级问题,但最终成功摆脱了这个问题。

0

这是一个Node.js版本的问题。一些最新版本的Node.js可能会显示类似这样的错误。

https://github.com/nvm-sh/nvm

我使用 NVM 来管理系统上的 Node.js 版本,并使用 Node.js 12 来解决此错误。

更改版本的命令:

nvm use 12

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