ChatGPT解决这个技术问题 Extra ChatGPT

安装 npm 包时无法解决依赖关系树错误

尝试使用 npm i 命令安装 npm 包时,出现以下异常:

https://i.stack.imgur.com/x8N3W.png

我尝试重新安装 Node.js 包并将代理设置为关闭,使用:

set HTTP_PROXY=
set HTTPS_PROXY=

问题仍然存在。我做错了什么?

更新:

当我运行以下命令时:

npm install --legacy-peer-deps

显示以下错误:

https://i.stack.imgur.com/IPCuC.png

显示你的 package.json - 看起来你已经升级了@angular/core,但没有升级@angular/http?
我的@angular/core 版本是 9.1.4,所以我应该更新@angular/http 吗?
请分享您的 package.json 文件。问题似乎出在您的依赖项中
您可以尝试删除 package-lock.json 和 node_modules 并尝试运行 npm update 吗?让我知道它是否有效。
这还在发生吗?你可以分享 package.json 文件吗?

P
Peter Mortensen

这与 HTTP 代理无关。

正如它所说,您有依赖冲突不正确并且可能损坏依赖),因此请尝试使用 --force 或 {2 }。如果没有生效,临时解决方案使用Node.js的旧版本(降级Node.js版本),因为它会导致此类错误有时会发生。

根据 OP 的更新进行更新:

如您所见,它会引发以下错误:

找不到与 @angular/http@^9.1.4 匹配的版本。

看看angular/http page请注意,该已弃用软件包的最新版本是 7.2.16,而您需要更高版本(例如,^9.1.4!因此,请尝试检查项目依赖关系并遵循引发的错误以解决问题。


@Pearl 请显示 --force--legacy-peer-deps 命令的输出结果
谢谢npm install --legacy-peer-deps对我有用
该标志会仅安装对等部门吗?
@Aashiqahmed 它告诉 NPM 忽略对等部门并继续安装
谢谢,将节点版本从 15 降级到 14 对我有用。
A
Ali Hasan

试试这个命令 -

npm install --save --legacy-peer-deps

请解释它的作用
@Gerfried,它告诉 NPM 忽略对等部门并继续安装。
如果使用 npm 5 或更高版本,则不需要 --save 标志,因为它会自动保存依赖项
它一半安装了 Angular 7,一半安装了 Angular 9。如果你这样做,它可能真的有效,但愿上帝怜悯你,因为你肯定在运行一些与你当前版本的 Angular 不兼容的库。
m
makkasi

首先要了解问题。这是我的错误:

npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: project-admin@11.0.0
npm ERR! Found: @angular/common@11.0.3
npm ERR! node_modules/@angular/common
npm ERR!   @angular/common@"11.0.3" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @angular/common@"^9.1.0 || ^10.0.0" from @agm/core@3.0.0-beta.0
npm ERR! node_modules/@agm/core
npm ERR!   @agm/core@"3.0.0-beta.0" from the root project

首先,您应该从下到上开始阅读问题。这里@agm/core@3.0.0-beta.0 需要 angular common 9.1.0 或 10.0.0。最上面的消息说找到的 angular common 实际上是 11.0.3。

(如果你想更好地理解依赖关系,这里是一个非常简单的站点:How npm3 Works

dependencies — these are the essential dependencies that you rely on and call in your project’s code
devDependencies — these are your development dependencies, for example, a prettier library for formatting code
peerDependencies — if you set a peer dependency in your package.json, you are telling the person who installs your package that they need that dependency with the specified version
optionalDependencies — these dependencies are optional and failing to install them will not break the installation process
bundledDependencies — it’s an array of packages that will come bundled with your package. This is useful when some 3rd party library is not on NPM, or you want to include some of your projects as modules

那么解决方案应该是什么呢?问题在于对等依赖关系。解决方案是降级 Angular common,或者解决方案是使用遗留依赖逻辑来使用 --legacy-peer-deps 安装包。所以 --legacy-peer-deps 不会尝试自动安装 peerDependencies。这对你有用吗?大概是。但是您应该添加具体说明如何执行此操作,或者使用 --legacy-peer-deps 自动使用以前答案之一中的此代码安装项目包:

npm config set legacy-peer-deps true

在我的情况下,我安装了软件包并尝试运行 ng serve,但由于使用了 --legacy-peer-deps,因此存在未安装的依赖包。我必须手动安装它们(因为我没有从上面的代码中设置配置)。最后手动安装了大约五个包,全部带有--legacy-peer-deps,我以一个无法安装的包结束,我没有尝试继续,因为我的项目正在抛出警告像疯了一样,还有很多要审计的包。所以我的决定是不使用这个包并寻找替代方案。

我一路上读到的其他解决方案:

将 Node.js 降级到 v14。这将降级 npm。它可能不是 v14,但这是降级最广泛的版本。

有些人使用 Yarn 强制安装包 - 我个人不明白这是如何工作的,因为我没有使用过 Yarn。

将 Angular 和全局 Angular CLI 版本降级为满足要求的版本。在我的情况下,它是 angular/common,在问题中它是 angular/core,但两者都需要降级整个 angular right(我在这里不确定)。

您安装的软件包可能具有不需要降级 Angular 的更高版本。您可能会尝试使用 https://updatepackagejson.com/ 将您的软件包升级到最新版本,但这是为了以防您的项目非常新。


这应该被接受的答案
这是最好的答案,因为它是解释来自 OP 的错误日志而不是愚蠢地应用 force 或 legacy-peer-deps 选项的唯一答案。坚持使用 npm 6 比推送 npm 7 和 force/legacy-peer-deps 更好,以避免修复你的依赖问题
到目前为止,最好的答案是其他人只是解决问题,而不关心潜在问题,也没有就问题发生的原因提供有见地的建议。
f
friederbluemle

除了使用 --legacy-peer-deps 命令行选项之外,还可以将其更永久地设置为配置选项:

npm config set legacy-peer-deps true

如果 npm install 是间接运行的,例如通过脚本运行,则最好的选择。
这是做什么的
@Arefe,Aashiq 在另一个类似的答案中解释说,这个命令(legacy-peer-deps)“告诉 NPM 忽略对等 deps 并继续安装”
npm config set legacy-peer-deps true 这个命令对我有用,谢谢
V
Vinay Somawat

最后,我找到了答案。试试这个命令 -

npm install --save --legacy-peer-deps

此处描述legacy-peer-deps


一个解释会改善这个答案
用解释编辑答案。
T
Trott

使用 npm 7 时,这种情况经常出现,因为对等依赖问题在版本 7 中被视为错误,而在版本 6 中它们通常只是警告。通常使用 --legacy-peer-deps 使其与 npm 7 一起使用。

如果这不起作用,一个选项是降级到 npm 6. 降级 Node.js 不是必需的(但也无害)。相关的依赖管理代码在 npm 中。降级 Node.js 通常会巧合,因为这样做通常也会降级 npm

与降级 npm 相比,破坏性较小的另一个选项是使用 npx 将先前版本的 npm 仅用于安装命令:npx -p npm@6 npm install

当所有其他方法都失败时,通常值得尝试删除 node_modules 目录和 package-lock.json,然后再次运行 npm install。这将重新生成 node_modulespackage-lock.json


永久降级到 npm 6 的最佳方法是什么?
@alentejo 通过删除 C:\Users\{your name}\AppData\Roaming 中的 npm 文件夹, npm cache clear --force 然后 npm install -g npm@
z
ztom

更新到 npm 7 后,某些软件包会发生这种情况。

参数 --legacy-peer-deps 可以帮助:

npm i --legacy-peer-deps

此处描述legacy-peer-deps

导致 npm 在构建包树时完全忽略 peerDependencies,就像在 npm 版本 3 到 6 中一样。如果由于过于严格的 peerDependencies 冲突而无法安装包,它提供了一种解决问题的方法。 ...

您可以将此选项默认设置为 true(npm 不推荐):

npm config set legacy-peer-deps true

或者只是等到这些包更新。


V
Vinit Dabhi

只需执行 2 个简单的步骤:

首先,在您的终端中执行此操作。

npm config set legacy-peer-deps true

二、清除缓存

npm cache clean --force

最后,执行您的命令,这肯定会起作用。


清除缓存在这里有帮助吗?我们真的需要第 2 步吗?
P
Peter Mortensen

该问题与依赖冲突或依赖关系损坏有关。您可以通过强制安装接受不正确的依赖关系来继续。

解决方案: 将命令与 --force 一起使用。

您的命令将类似于 npm install --force @your-npm-package

注意:您可以使用 yarn 安装依赖项,如果它可用于安装 yarn 包管理器。


K
Kavinda Senarathne

尝试删除 node modulespackage-lock.json 文件并运行命令 npm install 或尝试 npm cache clean --force


P
Peter Mortensen

首先我试过

npm install

它给了我错误 unable to resolve dependency tree 并根据此命令的帮助信息,

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 install --legacy-peer-deps

它解决了我的问题。


P
Peter Mortensen

最快的解决方案:npm install --legacy-peer-deps

解释:

在 npm 版本 3 到 6 中,peerDependencies 不会自动安装,如果在树中发现无效版本的对等依赖项,则会发出警告。从 npm v7 开始,默认安装 peerDependencies。 npm 文档:peerDependencies

您的依赖项包含一些与根项目的依赖项冲突的 peerDependencies。

npm ERR 日志中所述。


R
Rigers Leka

NPM 可用于安装和管理项目中的依赖项版本。

我在与 npm 版本相关的 React 版本上遇到了同样的问题

发现 npm 错误 types/react@16.14.20

因此,可能需要根据您的 package.json 安装软件包版本

它在 npm@7 版本中给出错误并且无法安装节点模块。如果您将 npm 版本降级到 6,这些问题将成为警告,问题将得到解决。

尝试证明这个命令: npm install -g npm@6

检查是否已安装版本: npm --version

删除并安装 node_modules 包:a)删除 rm -rf node_modules b)安装:npm i


E
Eliezer Berlin

问题似乎是 gf-kautomata-pipeline-ui 使用的是 Angular 9,而 @angular/http 需要 Angular 7。(@angular/http 已被弃用并最终被删除,其所有功能都移至 @angular/common .)

请参阅:https://www.npmjs.com/package/@angular/http

如果您正在运行 Angular 9,那么

从你的 package.json 中删除 @angular/http(你在 Angular 9 中不需要它)确保你的 package.json 中有 @angular/common。运行 npm i。

如果您正在运行 Angular 7,请打开您的 package.json 并检查以确保您的所有 Angular 包不高于 ^7.0.0。您可能还需要删除 gf-kautomata-pipeline-ui,或联系 gf-kautomata-pipeline-ui 的作者,了解该库是否与 Angular 7 兼容。


@Pearl 我在您写的评论中看到您的 @angular/core 是 Angular 9,而不是 Angular 7。您是从 7 升级到 9 的中途吗?
这绝对是最准确的答案
a
aakash sharma

尝试 2 个选项来解决此问题 option1 => 在运行 npm cache clean --force 后删除 node_modules 和 package_lock.json 在 npm i --force 选项 2 => 运行 npm install --save --legacy-peer-deps


P
Peter Mortensen

就我而言,在将 npm 从版本 6 升级到 7 后,我开始收到错误(如下)。

npm 错误!代码 ERESOLVE npm 错误! ERESOLVE 无法解析依赖树... npm ERR!修复上游依赖冲突,或使用 --force 或 --legacy-peer-deps 重试此命令以接受不正确(并且可能损坏)的依赖解析。

在我的例子中,使用 --legacy-peer-deps--force 标志编译会导致一个无用的包。

所以我尝试使用 yarn install 删除 node_modulespackage-lock.json 和捆绑包。这会生成一个 yarn.lock 文件并创建在后续 npm 运行中运行良好的 package-lock.json。

PS:在 npm 7 与我的项目正常工作之前,我一直在使用临时解决方法:之后,我将删除 yarn.lockpackage-lock.json 和文件夹node_modules,然后用 npm 重新编译

rm -rf node_modules
rm package-lock.json
yarn install
# Generates a yarn.lock file and a new package-lock.json

# Continue with npm
npm start

k
komal dubey

npm install --legacy-peer-deps

这对我有用


F
Fahad

这对我有用 npm install --save --legacy-peer-deps


P
Peter Mortensen

我只是更新了我的 Node.js,它对我有用:

node -v

输出:

V xxxx

和:

sudo npm install -g n

(使用此命令安装稳定的节点版本。)

sudo n stable

P
Peter Mortensen

如果您的根目录中有 node_modules 文件夹和 package-lock.json 文件,则删除它们: rm -r node_modules rm package-lock.json 然后运行命令: npm install --save --legacy-peer-deps npm audit fix - -force 在根目录中创建 .env 文件并粘贴以下代码: SKIP_PREFLIGHT_CHECK=true 现在,启动您的项目: npm start


P
Peter Mortensen

我多次遇到这个问题。最后我找到了解决方案:

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

P
Peter Mortensen

Yarn 具有解决此问题的功能。如果可以,请尝试使用它来安装软件包。


你能说一下是哪个功能吗?
J
Julio Cachay

对于这种情况:我遇到了这个问题:ERESOLVE 无法解析 Angular 13 项目中的依赖关系树,该项目使用了 azure devops 中私有 npm 提要中的一些包。

为了访问这个存储库,我创建了一个 .npmrc 文件。因此,npm install 命令将搜索我的私有存储库中的所有包,而不是 npm 提要中的所有包。发生无法解决依赖关系树错误是因为 npm install 命令找不到许多托管在 npm 提要中的软件包,而不是我的私人提要。

我找到了this amazing answer on how to scope packages

基于此,我做了一些更改:

在我的库 Package.json 中,将名称更新为具有范围名称 @mylib

"name": "@myLib/command-queue",

构建这个包并将其发布到我的私人提要中

@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

此外,通过此更改,不再需要将 --legacy-peer-deps 添加到 npm install 命令。


S
Sebastian Voráč

重置 package-lock.json 一直对我有用:

git checkout -- package-lock.json

详细信息: 在更新旧项目的所有包时经常遇到这种情况 - 我强烈不建议使用 npm audit fixnpm i --force。删除 package-lock.json 也并非一直对我有用。回滚到 package.json + package-lock.json 的工作版本并添加包对我来说是最安全和最快的变体。


A
Andrii Lundiak

以防万一,当我尝试 npm upgrade 我当前基于 Angular 11.x 的样板从以前的 ng new 或创建新的 ng new abc 基于 Angular 12.x 时,我确实有类似的行为。我只是忘记升级 Angular CLI。所以这个 npm install -g @angular/cli@latest 解决了我在 ng new abc 期间的错误。


P
Peter Mortensen

在我的情况下,我遇到了 @babel/core 依赖项的问题,但我不想使用 --force,因为我不确定后果,所以我去了 https://www.npmjs.com/ ,寻找包裹并将我的旧版本替换为最新版本。那完成了工作。


s
samridhgupta

这是节点版本的问题 一些最新版本的节点可能会显示类似这样的错误。

https://github.com/nvm-sh/nvm 我使用 NVM 来管理系统上的 Node 版本,并使用 node 12 来解决这个错误

更改版本的命令

nvm use 12

S
Saurabh Yadav

对于那些在安装 Bootstrap Paginator 或表格时遇到错误的人

npm install react-bootstrap-table2-paginator --save

或者

npm install react-bootstrap-table-next --save

解决方案:在命令后添加 --legacy-peer-deps

npm install react-bootstrap-table2-paginator --save --legacy-peer-deps

npm install react-bootstrap-table-next --save --legacy-peer-deps


R
Raghubir Singh

即使在卸载/安装 NPM 和 CLI 之后,如果仍然无法正常工作,请确保您位于项目文件夹中。例如,您创建了一个根文件夹“myapplication”。现在,您正在运行 CLI 命令以在 myapplication 文件夹下创建一个全新的应用程序。运行命令后,你会得到类似“ERESOLVE 无法解析依赖树”的错误。因此,不要在 myapplication 文件夹上运行命令,而是转到 myapplication 文件夹中的新应用程序文件夹并运行/执行 Angular 应用程序。它会工作得很好。


P
Peter Mortensen

我通过将 Node.js 的版本从上一个版本降低到 LTS 版本来解决这个问题。


我在 LTS 版本的 Node.js 上遇到了这个问题。