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: <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="553b302d2121223c3b15657b647b65">[email protected]</a>
npm ERR! Found: <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0b2a5a1a3b480f1f7eef0eef1">[email protected]</a>
npm ERR! node_modules/react
npm ERR!   <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2d0c7c3c1d6e2">[email protected]</a>"17.0.1" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e2c3b3f3d2a1e">[email protected]</a>"^16.8.0" from <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e7c6b6f6d7a23666161652363617b7d6b7a7c6f7e4e3c203e203a">[email protected]</a>
npm ERR! node_modules/react-hook-mousetrap
npm ERR!   <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f4d5a5e5c4b12575050541252504a4c5a4b4d5e4f7f">[email protected]</a>"*" 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.

What can I do now to fix this? I don’t want to downgrade my React version for this.

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? I assume there is a reason npm did let the install fail.

It’s just strange because I was using yarn up until very recently and everything was fine.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1


You may be arriving upon this answer if you’re upgrading from NPM v6 / Node v12.

  • NPM v7+ installs peerDependencies by default; this is not the case with previous versions of NPM.
  • NPM modules must name specific versions of their peerDependencies
  • If you already have a peerDependency installed, but not with a version named by the module, then NPM v7+ will throw an error
  • Adding --legacy-peer-deps ignores this new requirement, at the risk of introducing breaking changes

–legacy-peer-deps restores peerDependency installation behavior from NPM v4 thru v6

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.

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; it tells NPM to ignore peer deps and proceed with the installation anyway. This is how things used to be with NPM v4 thru v6.

If you’re unclear about the difference between regular deps and peer deps, here is a bit of context:

Dependencies vs peerDependencies

Dependencies: Libraries or modules that an NPM module needs in order to work in production. (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.)

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. 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.)

This issue is being driven, in part, by React v17

Due to the large number of modules that haven’t specifically added React v17 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.

This error will fire whenever a module (or any of its own dependencies) lists a previous version of React as a peerDependency without specifically including React v17 as well.

(Note: Similar behavior will occur with the major-version update of any other framework or library.)

How to check peerDependencies for any given module

NPM itself doesn’t list peer deps on the pages of a given module. 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).

Method 2

Here’s how I solved this problem:

First, what’s happening: react-hook-mousetrap is looking for [email protected], but it is not finding it. Instead it is finding @react17.0.1, which is a newer version. 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:

yarn add <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="562433373522166760786e7866">[email protected]</a>

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.

Method 3

I resolved (with yarn) adding the following to package.json

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

Method 4

If you don’t want to block installing older dependencies, you can make npm neglect those warnings by forcing the script you’re running. --force

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x