简体   繁体   English

npm install --legacy-peer-deps 究竟做了什么? 什么时候推荐/什么是潜在用例?

[英]What does npm install --legacy-peer-deps do exactly? When is it recommended / What's a potential use case?

Just ran into this error:刚遇到这个错误:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: nexttwin@0.1.0
npm ERR! Found: react@17.0.1
npm ERR! node_modules/react
npm ERR!   react@"17.0.1" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.8.0" from react-hook-mousetrap@2.0.4
npm ERR! node_modules/react-hook-mousetrap
npm ERR!   react-hook-mousetrap@"*" from the root project
npm ERR! 

The module I am trying to install seems to have a different peer dependency from what I have installed.我尝试安装的模块似乎与我安装的模块具有不同的对等依赖性。 It seems like npm changed its behaviour in this regard and now lets the install fail.似乎 npm 在这方面改变了它的行为,现在让安装失败。

What can I do now to fix this?我现在该怎么做才能解决这个问题? I don't want to downgrade my React version for this.我不想为此降级我的 React 版本。

I know there is a flag called --legacy-peer-deps but I am not sure what exactly this does and whether it's recommended to use it / what the potential disadvantages are?我知道有一个名为--legacy-peer-deps的标志,但我不确定这到底是做什么的,是否建议使用它/潜在的缺点是什么? I assume there is a reason npm did let the install fail.我假设 npm 确实让安装失败是有原因的。

It's just strange because I was using yarn up until very recently and everything was fine.这很奇怪,因为直到最近我才使用yarn并且一切都很好。

TL;DR:长话短说:

You may be arriving upon this answer if you're upgrading from NPM v6 / Node v12.如果您从 NPM v6 / Node v12 升级,您可能会得到这个答案。

  • NPM v7+ installs peerDependencies by default ; NPM v7+ 默认安装 peerDependencies ; this is not the case with previous versions of NPM.以前版本的 NPM不是这种情况。
  • NPM modules must name specific versions of their peerDependencies NPM 模块必须命名其 peerDependencies 的特定版本
  • If you already have a peerDependency installed, but not with a version named by the module, then NPM v7+ will throw an error如果您已经安装了 peerDependency,但没有安装模块命名的版本,那么 NPM v7+ 将抛出错误
  • Adding --legacy-peer-deps ignores this new requirement, at the risk of introducing breaking changes添加--legacy-peer-deps忽略了这个新要求,有引入重大更改的风险

--legacy-peer-deps restores peerDependency installation behavior from NPM v4 thru v6 --legacy-peer-deps 从 NPM v4 到 v6 恢复 peerDependency 安装行为

One way of thinking of this flag is that it isn't doing something new;考虑这个标志的一种方式是它没有做新的事情; rather it's telling NPM not to do something new, since NPM v7 now installs peerDependencies by default .相反,它告诉 NPM不要做一些新的事情,因为 NPM v7现在默认安装 peerDependencies

In many cases, this is leading to version conflicts, which will break the installation process.在许多情况下,这会导致版本冲突,从而破坏安装过程。

The --legacy-peer-deps flag was introduced with v7 as a way to bypass peerDependency auto-installation; --legacy-peer-deps标志是在 v7 中引入的,作为绕过 peerDependency 自动安装的一种方式; it tells NPM to ignore peer deps and proceed with the installation anyway.它告诉 NPM 忽略 peer deps 并继续安装。 This is how things used to be with NPM v4 thru v6.这就是过去 NPM v4 到 v6 的情况。

If you're unclear about the difference between regular deps and peer deps, here is a bit of context:如果您不清楚常规 deps 和 peer deps 之间的区别,这里有一些上下文:

Dependencies vs peerDependencies依赖与 peerDependencies

Dependencies: Libraries or modules that an NPM module needs in order to work in production .依赖项: NPM 模块在生产环境中工作所需的库或模块。 (Example: I recently built a pie chart mocking library that uses Chance.js to calculate random numbers within a specified range; Chance is therefore a dependency of my module.) (示例:我最近构建了一个饼图 mocking 库,它使用Chance.js计算指定范围内的随机数;因此 Chance 是我模块的依赖项。)

peerDependencies : A peer dependency is a specific version or set of versions of a third-party software library that a module is designed to work with . peerDependencies :对等依赖项是模块设计用于使用的第三方软件库的特定版本或版本集 They're similar in concept to the relationship between a browser extension and a browser.它们在概念上类似于浏览器扩展和浏览器之间的关系。 (Example: react-redux has two quite logical peerDependencies: react and redux .) (例如: react-redux有两个非常符合逻辑的 peerDependencies: reactredux

This issue is being driven, in part, by React v17+这个问题部分是由 React v17+ 驱动的

Due to the large number of modules that haven't specifically added React v17 (or more recently, React 18) as a peerDependency, it's now commonplace to encounter the unable to resolve dependency tree error when running npm installs within a v17 React application.由于大量模块没有专门添加 React v17(或最近的 React 18)作为 peerDependency,现在在 v17 React 应用程序中运行 npm 安装时遇到unable to resolve dependency tree错误已经司空见惯。

This error will fire whenever a module (or any of its own dependencies) lists a previous major version of React as a peerDependency without specifically including React v17 as well.每当模块(或其任何依赖项)将 React 的先前主要版本列为 peerDependency 而没有特别包括React v17 时,就会触发此错误。

( Note: Similar behavior will occur with the major-version update of any other framework or library.) 注意:任何其他框架或库的主要版本更新都会发生类似的行为。)

How to check peerDependencies for any given module如何检查任何给定模块的 peerDependencies

NPM itself doesn't list peer deps on the pages of a given module. NPM 本身不会在给定模块的页面上列出对等 dep。 However, there is a simple workaround to check for peer deps, either before or after install.但是,有一个简单的解决方法可以在安装之前或之后检查对等依赖。 Simply run:只需运行:

npm info name-of-module peerDependencies

This command will return the name of each peerDependency along with all compatible version(s).此命令将返回每个 peerDependency 的名称以及所有兼容版本。

Here's how I solved this problem:这是我解决这个问题的方法:

First, what's happening: react-hook-mousetrap is looking for react@16.8.0, but it is not finding it.首先,发生了什么: react-hook-mousetrap 正在寻找 react@16.8.0,但没有找到。 Instead it is finding @react17.0.1, which is a newer version.相反,它正在寻找 @react17.0.1,这是一个较新的版本。 For some reason mousetrap doesn't like this newer version, and you are being notified (it is not a big deal, but they decided it was worth stopping your build).出于某种原因,捕鼠器不喜欢这个较新的版本,并且您会收到通知(这不是什么大问题,但他们认为值得停止您的构建)。

One solution: forcibly install the specific version of react that mousetrap wants:一种解决方案:强行安装mousetrap想要的特定版本的反应

yarn add react@16.8.0

What this does is roll back your react version to a slightly older one that is compatible with mousetrap.这样做是将您的反应版本回滚到与捕鼠器兼容的稍旧版本。 You won't notice any difference, and in future iterations, hopefully mousetrap is updated, so this goes away.您不会注意到任何差异,并且在未来的迭代中,希望捕鼠器得到更新,所以它会消失。

Another solution: make a sweeping decision to not install any older version dependencies:另一个解决方案:彻底决定不安装任何旧版本依赖项:

npm add xxxx --legacy-peer-deps

What this does is ignore old dependencies for this package. It is more comprehensive, and makes a lot of the decisions for you.它所做的是忽略这个 package 的旧依赖项。它更全面,并为您做出很多决定。

I resolved (with yarn) adding the following to package.json我解决了(用纱线)将以下内容添加到 package.json

"resolutions": {
    "**/react": "17.0.2",
    "**/react-dom": "17.0.2"
},

If you don't want to block installing older dependencies, you can make npm neglect those warnings by forcing the script you're running.如果您不想阻止安装旧的依赖项,您可以通过强制运行您正在运行的脚本来使 npm 忽略这些警告。 --force

Just ran into this error:刚刚遇到这个错误:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: nexttwin@0.1.0
npm ERR! Found: react@17.0.1
npm ERR! node_modules/react
npm ERR!   react@"17.0.1" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.8.0" from react-hook-mousetrap@2.0.4
npm ERR! node_modules/react-hook-mousetrap
npm ERR!   react-hook-mousetrap@"*" from the root project
npm ERR! 

The module I am trying to install seems to have a different peer dependency from what I have installed.我尝试安装的模块似乎与我安装的模块具有不同的对等依赖项。 It seems like npm changed its behaviour in this regard and now lets the install fail.似乎 npm 在这方面改变了它的行为,现在让安装失败。

What can I do now to fix this?我现在能做些什么来解决这个问题? I don't want to downgrade my React version for this.我不想为此降级我的 React 版本。

I know there is a flag called --legacy-peer-deps but I am not sure what exactly this does and whether it's recommended to use it / what the potential disadvantages are?我知道有一个名为--legacy-peer-deps的标志,但我不确定这到底是做什么的以及是否建议使用它/潜在的缺点是什么? I assume there is a reason npm did let the install fail.我认为 npm 确实让安装失败是有原因的。

It's just strange because I was using yarn up until very recently and everything was fine.这很奇怪,因为直到最近我还在使用yarn ,一切都很好。

--leagcy-peer-deps jumps the installation of all the peer dependencies and gives warnings about the peer deps to notice developers install them manually. --leagcy-peer-deps跳过所有对等依赖项的安装,并发出有关对等依赖项的警告,以通知开发人员手动安装它们。 When encountering the peer deps conflicts, other than --legacy-peer-deps , another choice is use --force .当遇到 peer deps 冲突时,除了--legacy-peer-deps之外,另一个选择是使用--force
The official doc of handling peer deps conflicts is this处理 peer deps 冲突的官方文档是这个

ps附言
Correct the top answer: --leagcy-peer-deps restores peerDependency installation behavior from NPM v3 thru v6, rather than v4 thru v6.更正最上面的答案: --leagcy-peer-deps从 NPM v3到 v6,而不是 v4 到 v6 恢复 peerDependency 安装行为。

legacy-peer-deps : legacy-peer-deps

  • Default: false默认值:假
  • Type: Boolean类型:Boolean

Causes npm to completely ignore peerDependencies when building a package tree, as in npm versions 3 through 6.导致 npm 在构建 package 树时完全忽略peerDependencies ,如 npm 版本 3 到 6。

If a package cannot be installed because of overly strict peerDependencies that collide, it provides a way to move forward resolving the situation.如果 package 由于过于严格的peerDependencies发生冲突而无法安装,它提供了一种向前解决这种情况的方法。

This differs from --omit=peer , in that --omit=peer will avoid unpacking peerDependencies on disk, but will still design a tree such that peerDependencies could be unpacked in a correct place.这与--omit=peer不同,因为--omit=peer将避免在磁盘上解包peerDependencies ,但仍会设计一个树,以便peerDependencies可以在正确的位置解包。

Use of legacy-peer-deps is not recommended, as it will not enforce the peerDependencies contract that meta-dependencies may rely on.不推荐使用legacy-peer-deps ,因为它不会强制执行元依赖项可能依赖的peerDependencies契约。


If you want to continue using legacy-peer-deps without needing to add the flag to every command, you can configure it in your .npmrc (either at the project level or globally on your machine):如果你想继续使用legacy-peer-deps而不需要在每个命令中添加标志,你可以在你的.npmrc中配置它(在项目级别或在你的机器上全局):

echo "legacy-peer-deps=true" >> .npmrc

npmrc : npmrc :

npm gets its config settings from the command line, environment variables, and npmrc files. npm 从命令行、环境变量和npmrc文件获取其配置设置。

The npm config command can be used to update and edit the contents of the user and global npmrc files. npm config命令可用于更新和编辑用户和全局 npmrc 文件的内容。

One other way is to downgrade your npm version to version 6另一种方法是将 npm 版本降级到版本 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM