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

时间:2020-10-28 12:46:51

标签: angularjs angular typescript npm angular9

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

enter image description here

我尝试使用以下方法重新安装node js软件包并将代理设置为关闭:

set HTTP_PROXY=
set HTTPS_PROXY=

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

更新

当我运行以下命令时:

npm install --legacy-peer-deps

显示以下错误: enter image description here

29 个答案:

答案 0 :(得分:23)

更新到 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

或者等到这些包更新。

答案 1 :(得分:4)

希望我的帖子能帮到你,我先试试

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

它解决了我的问题

答案 2 :(得分:4)

这与http代理无关。

您说有依赖冲突不正确并且可能是依赖),因此,请尝试使用{{1 }}或--force。如果未生效,则临时解决方案使用节点的先前版本(降级节点版本),因为有时会导致此类错误。

基于OP的更新

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

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

看看angular/http page请注意,该不推荐使用的软件包的最新版本为--legacy-peer-deps,而您请求更高版本(例如7.2.16)!因此,请尝试检查项目依赖项并遵循所提出的错误以解决问题。

答案 3 :(得分:3)

使用npm 7时,我在使用node_modules 6生成的package-lock.json / npm时遇到了这种情况。通常使用--legacy-peer-deps可以使其正常工作

如果这不起作用,则可以选择降级到npm6。降级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

答案 4 :(得分:3)

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

答案 5 :(得分:2)

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

说明:

<块引用>

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

npm docs: peerDependencies

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

npm ERR 日志中所述。

答案 6 :(得分:1)

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

<块引用>

npm 错误!代码 ERESOLVE npm ERR! ERESOLVE 无法解析依赖 树

...

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

在我的情况下,使用 --legacy-peer-deps--force 标志编译会导致一个无用的包。

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

附言我正在使用临时解决方法,直到 npm 7 在我的项目中正常工作:之后,我将删除 yarn.lock、package-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

答案 7 :(得分:1)

就我而言,我遇到了 @babel/core 依赖项的问题,但不想使用 --force,因为我不确定后果,所以我去了 { {3}},查找软件包并将我的旧版本替换为最新版本。这样做了

答案 8 :(得分:1)

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

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

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

  1. @angular/http中删除package.json(在Angular 9中不需要)

  2. 确保您的@angular/common中有package.json

  3. 运行npm i

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

答案 9 :(得分:1)

尝试此命令-

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

答案 10 :(得分:0)

以上答案都没有帮助我。 对我来说,使用 npm i -g npm 将 npm 更新到最新版本(从 7.8 到 7.11.1)为我解决了这个问题。

答案 11 :(得分:0)

这个问题我遇到了很多次,终于找到了解决方案

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

答案 12 :(得分:0)

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

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

答案 13 :(得分:0)

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

答案 14 :(得分:0)

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

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。

(如果你想更好地理解依赖关系,这里是一个非常简单的站点 https://npm.github.io/how-npm-works-docs/npm3/how-npm3-works.html

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 自动使用 --legacy-peer-deps 自动使用以上答案之一中的此代码安装项目包:

npm config set legacy-peer-deps true

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

我在此过程中了解到的其他解决方案:

  • 将节点降级到 v14,这将降级 npm。可能不是 v14,但这是降级最多的版本。
  • 有些人使用 yarn 来强制安装包 - 我个人不明白这是如何工作的,因为我没有使用过 yarn。
  • 将 angular 和全局 angular cli 版本降级到满足要求的版本。在我的情况下,它是 angular/common,在问题中它是 angular/core,但两者都需要降级整个 ​​angular right(这里不确定)。
  • 您安装的软件包可能具有不需要降级 angular 的更高版本。您可以尝试使用 https://updatepackagejson.com/ 将您的软件包升级到最新版本,但这是在您的项目很新的情况下。

答案 15 :(得分:0)

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

答案 16 :(得分:0)

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

git checkout -- package-lock.json

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

答案 17 :(得分:0)

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

答案 18 :(得分:0)

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

答案 19 :(得分:0)

这是解决方案。

进入项目目录

sudo rm ./package-lock.json
sudo npm install --force

答案 20 :(得分:0)

我只是更新我的节点,它对我有用:

$ node -v

 V xxxx
    
$ sudo npm install -g n

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

$ sudo n stable

希望它对你也有用!!!

答案 21 :(得分:0)

问题与依赖冲突或依赖中断有关。您可以通过强制安装来接受依赖项的错误。

解决方案:使用带有 --force 的命令。

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

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

答案 22 :(得分:-1)

请安装大多数用户推荐的LTS版本的节点,这对我有很大帮助,可以挽救生命

答案 23 :(得分:-1)

做简单的事情:

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

答案 24 :(得分:-1)

npm audit fix --force

为我工作

答案 25 :(得分:-2)

我遇到了同样的问题

npm install --legacy-peer-deps 

这也适用于我的。但我有一个更简单的答案。只需更改所需的 package.json 中的版本号。然后使用

npm i

这将解决您的问题(像往常一样生成新项目,没有任何潜在的依赖问题。)

答案 26 :(得分:-2)

首先使用 npm i --legacy-peer-deps 标志,然后使用 --force 标志。

答案 27 :(得分:-2)

1.在package.json中添加依赖 2. 进行 npm 安装

仍然看到错误?

  1. npm install --force

答案 28 :(得分:-2)

终于找到了解决方案

  • 安装 nvm
  • 使用 nvm 安装节点
  • 输入 cmd 以使用 lts 版本
  • 在我的安装成功之后,它应该也适合你。
相关问题