npm install报错unable to resolve dependency tree

这篇具有很好参考价值的文章主要介绍了npm install报错unable to resolve dependency tree。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、问题背景

npm install安装项目依赖时报错

PS D:\test> npm install
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: vue-admin-template@4.2.1
npm ERR! Found: webpack@5.74.0
npm ERR! node_modules/webpack
npm ERR!   dev webpack@"^5.11.0" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer webpack@"^3.1.0" from extract-text-webpack-plugin@3.0.2
npm ERR! node_modules/extract-text-webpack-plugin
npm ERR!   extract-text-webpack-plugin@"^3.0.2" 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.
npm ERR!
npm ERR! See D:\Program\nodejs\node_cache\eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     D:\Program\nodejs\node_cache\_logs\2022-08-23T01_38_42_815Z-debug.log
PS D:\test> npm install --legacy-peer-deps

解决:npm install --legacy-peer-deps

其实提示上有:npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force, or --legacy-peer-deps

二、什么是peerDependency?

  在日常使用命令npm install / npm install XX下载依赖的操作中,如果你使用的是 npm v7 以上的版本,那么应该经常会遇到无法解析依赖树的问题(依赖冲突),就如我在上面遇到的那样报错

  但是每当遇到这种情况的时候,我用命令npm install --legacy-peer-deps就可以顺利进行下载操作。

1、这是为什么呢?带着好奇心,我去研究学习了一番npm install xxxx --legacy-peer-deps命令是什么?为什么可以解决下载时候产生的依赖冲突呢?

  我们日常在前端项目开发过程中,见到最多的一定是package.json文件里的devDependencies(用于在开发环境下依赖的模块)和dependencies(无论开发环境还是生产环境都需要的依赖模块)这两个字段

  那么命令--legacy-peer-dep里的peerDependency是什么依赖呢?根据geeksforgeeks网站里的定义:

Peer Dependencies: In package.json file, there is an object called as peerDependencies and it consists of all the packages that are exactly required in the project or to the person who is downloading and the version numbers should also be the same. That is the reason they were named as peerDependencies. The best example is ‘react’ which is common in every project to run similarly.

  翻译一下就是说,在package.json文件中,存在一个叫做peerDependencies(对等依赖关系)的对象,它包含了项目里需要的所有的包或者用户正在下载的版本号相同的所有的包(很绕,但意思就是对等依赖关系指定我们的包与某个特定版本的npm包兼容);对等依赖关系最好的例子就是React,一个声明式的创建用户界面的JS库。

2、那么我们为什么需要对等依赖关系呢?

  假设我们现在有一个Hello工程,已经在其根目录下的package.json文件中的dependencies字段里声明了packageA作为依赖,而其下面有两个项目app_A和app_B,它们也依赖packageA。如果我们用dependencies而不是peerDepenedencies来声明,那么npm install安装完项目之后的依赖结构如下图所示:

npm install报错unable to resolve dependency tree

 从上图可以看出,packageA依赖包被安装了3次,造成了2次安装冗余。

  而如果采用peerDepenedency来下载,就可以避免这个核心依赖库被重复下载的问题。还是上面那个场景,我们在项目app_A和app_B的package.json文件里的peerDependencies字段声明一下核心依赖库packageA,然后在根目录的package.json文件里的dependencies字段也声明一下packageA。

npm install报错unable to resolve dependency tree

接着再执行npm install,生成的依赖结构就会如下图所示:

npm install报错unable to resolve dependency tree

如上图所示,packageA就只会被安装一次。可以看到这时候生成的依赖图是扁平的,packageA 也只会被安装一次。

  因此我们总结下在插件使用dependencies声明依赖库的特点:

  • 如果用户显式依赖了核心库,则可以忽略各插件的peerDependencies声明;

  • 如果用户没有显式依赖核心库,则按照插件peerDependencies中声明的版本将库安装到项目根目录中;

  • 当用户依赖的版本、各插件依赖的版本之间不相互兼容,会报错让用户自行修复。

  npm 从版本v7开始,install就默认以peerDependencies的方式去下载了:

  1. 如果用户在根目录的package.json文件里显式依赖了核心库,那么各个子项目里的peerDepenedencies声明就可以忽略

  1. 如果用户没有显式依赖核心库,那么就按照子项目的peerDepenedencies中声明的版本将依赖安装到项目根目录里

  而方式2就会导致一个问题:用户依赖的包版本与各个子项目依赖的包版本相互不兼容,那么就会报错(无法解析依赖树的问题(依赖冲突))让用户自行去修复,因而导致安装过程的中断。(因为是从npm v7引入的,因此npm v3-v6就不会发生这个错误)

3、npm install xxxx --legacy-peer-deps命令是什么?为什么可以解决下载时候产生的依赖冲突呢?

  npm install xxxx --legacy-peer-deps命令与其说是告诉npm要去干什么,不如说是告诉npm不要去干什么。

  legacy的意思:遗产/(软件或硬件)已过时但因使用范围广而难以替代的;而npm install xxxx --legacy-peer-deps命令用于绕过peerDependency里依赖的自动安装;它告诉npm忽略项目中引入的各个依赖模块之间依赖相同但版本不同的问题,以npm v3-v6的方式去继续执行安装操作

  所以其实该命令并没有真的解决冲突,而是忽略了冲突,以“过时”(v3-v6)的方式进行下载操作

4、--legacy-peer-deps命令作用

  在NPM v7中,现在默认安装peerDependencies。在很多情况下,这会导致版本冲突,从而中断安装过程

  --legacy-peer-deps标志是在v7中引入的,目的是绕过peerDependency自动安装;它告诉 NPM 忽略项目中引入的各个modules之间的相同modules但不同版本的问题并继续安装,保证各个引入的依赖之间对自身所使用的不同版本modules共存文章来源地址https://www.toymoban.com/news/detail-425621.html

到了这里,关于npm install报错unable to resolve dependency tree的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • npm ERR! code ERESOLVEnpm ERR! ERESOLVE unable to resolve dependency tree

    拉取项目到本地 执行 npm install 报错 遇到这个问题首先确认的就是版本是不是太高了,降一下版本。或者通过yarn命令替代npm install命令安装,同理,启动也可以采用yarn dev 启动代替npm run dev 下面教大家用一个NVM工具,这个工具是用来管理node.js版本的 nvm流程安装 1、卸载node.

    2024年02月13日
    浏览(28)
  • 解决npm install时报错Could not resolve dependency

    最近在新的项目使用npm install时发现报错: npm WARN cli npm v9.6.4 does not support Node.js v15.14.0. This version of npm supports the following node versions: `^14.17.0 || ^16.13.0 || =18.0.0`. You can find the latest version at https://nodejs.org/. npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR!  npm ERR! While resolving:

    2024年02月08日
    浏览(37)
  • 解决:ERESOLVE unable to resolve dependency tree

    NPM版本问题报错的解决方案 在安装项目依赖时,很大可能会遇到安装不成功的问题,其中有一个很大的原因,可能就是因为你的npm版本导致的。 1.npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree 2.ERESOLVE unable to resolve dependency tree 3.如图: 4.报错原因 在新版本的npm中,

    2024年02月12日
    浏览(44)
  • npm install:Could not resolve dependency:peer... 原因和解决方案

    电脑 npm install 报错; 报错日志如下: 上述报错日志中有个眼: this command with --force, or --legacy-peer-deps 那么npm:何时使用 --force 和 --legacy-peer-deps ? --force 会无视冲突,并强制获取远端 npm 库资源,即使本地有资源也会覆盖掉 --legacy-peer-deps :安装时忽略所有 peerDependen

    2024年02月13日
    浏览(37)
  • npm install / webdriver-manager update报错 unable to get local issuer certificate

    我这边遇到的问题,用的是angular,跑npm install的时候报错,一开始在.npmrc添加strict-ssl=false但是还是报错,搜索下记录。 参考解决: selenium - webdriver-manager update, Error: unable to get local issuer certificate - Stack Overflow 这边主要问题是跑script  webdriver-manager update --gecko false 的时候报错,

    2024年02月07日
    浏览(39)
  • 安装 element-ui 的时候出现 ERESOLVE unable to resolve dependency tree

    在安装 element-ui 的时候报错 unable to resolve dependency tree (无法解决的冲突依赖) 尝试了以下方法: 1)卸载重装 node.js (npm 与 node 版本不匹配) 2) 执行 npm clean cache --force (清除缓存) 3) 删除 node_modules 和 package-lock.json;并重新执行 npm install 最后依旧解决不了问题,正当我放弃的时

    2024年02月15日
    浏览(50)
  • npm install报错fatal: unable to access ‘https://github.com/nhn/raphael.git/‘: OpenSSL SSL_read 10054

    报错信息:npm ERR! E:toolsGittGitcmdgit.EXE ls-remote -h -t https://github.com/nhn/raphael.git npm ERR! npm ERR! fatal: unable to access \\\'https://github.com/nhn/raphael.git/\\\': OpenSSL SSL_read: Connection was reset, errno 10054 npm ERR! npm ERR! exited with error code: 128 处理方法 1.git config --global url.\\\"https://\\\" .insteadOf git:// 2.可能是

    2024年02月16日
    浏览(42)
  • vue项目启动npm install和npm run serve时出现错误Failed to resolve loader:node-sass

    解决方法: 解决方法: node sass-node loader-sass 14.16.0 4.14.1 8.0.2

    2024年04月26日
    浏览(33)
  • 【Synopsys Bug记录】DC综合报错(显示warning:Unable to resolve reference)

      在综合一个SOC时,发现综合后的门级网表文件缺少了apb系统下的子模块的网表。该SOC已经成功在FPGA上运行了,按理说在设计上是没有问题的。在反复查看综合报告与RTL设计源码后,终于解决了Bug。   查看综合报告,发现以下警告和报错:   首先分析Warning,这个Wa

    2024年01月20日
    浏览(43)
  • IDEA项目启动报错:Failed to execute goal on project xxx: Could not resolve dependencies for project

    [ERROR] Failed to execute goal on project xxx: Could not resolve dependencies for project Failed to collect dependencies at xxx .xxx-service:jar:dev: Failed to read artifact descriptor for xxxx/maven-snapshots/ during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of nexus-snaps

    2024年02月03日
    浏览(61)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包