Unable to resolve dependency tree error when installing npm packages

If you install the node modules using the latest node.js and npm version 7, you will get an error Unable to resolve dependency tree error when installing npm packages

In this tutorial, we will discuss what exactly “Unable to resolve dependency tree error when installing npm packages” is and how to fix this error.

What is Unable to resolve dependency tree error when installing npm packages?

Many developers are facing this issue while installing the dependencies in angular projects.

A similar issue was observed while installing the react-facebook-login package through npm version 7.

If you have recently updated the NPM to the latest version (v7) and tried to update or install the dependencies in your project, you might get a dependency tree error. 

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

What is causing the ERESOLVE unable to resolve dependency tree?

In the new version of npm (v7), by default, npm install will fail when it encounters conflicting peerDependencies. This issue was not happening in npm version 3 to 6 as it used to ignore the peerDependencies when building a package tree ultimately.

Take a look at the official NPM documentation on legacy-peer-deps for more info about peer dependencies in npm v7.

The differences between the two are below –

  • --legacy-peer-deps: ignore all peerDependencies when installing, in the style of npm version 4 through version 6.
  • --strict-peer-deps: fail and abort the install process for any conflicting peerDependencies when encountered. By default, npm will only crash for peerDependencies conflicts caused by the direct dependencies of the root project.

If the package cannot be installed because of overly strict peerDependencies that collide, it provides a way to move forward resolving the situation.

Use of legacy-peer-deps is not recommended, as it will not enforce the peerDependencies contract that meta-dependencies may rely on.

How to fix Unable to resolve dependency tree error when installing npm packages?

There are two solutions to resolve this issue. Let us look at each of those in detail.

Solution 1: Ignore the peerDependencies 

The easiest way to fix the issue is to pass an additional parameter –legacy-peer-deps to npm install. The --legacy-peer-deps tells the npm to ignore the peer dependencies and continue the installation of the package.

Try the below command to install the dependencies for your project.

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

You can also set this permanently by adding this into a configuration by running the below command.

npm config set legacy-peer-deps true

Note: In this solution, the peer dependencies will not be installed by default, even if you are using the latest version of NPM.

Solution 2: Using –force

The other solution is to use the --force flag. The -f or --force argument will force npm to fetch remote resources even if a local copy exists on disk.

Step 1: Delete the current node_modules. You can remove it by using the below command.

rm -rf node_modules

Step 2: Remove the package-lock.json by running the below command

rm package-lock.json

Step 3: Clear the npm cache and perform the clean installation with --force flag as shown below.

npm cache clean --force
npm install --force

By performing the above steps, you should resolve the issue successfully.

Conclusion

The Unable to resolve dependency tree error when installing npm packages occurs when you install the node dependencies with the latest version of NPM(v7).

The error is due to the strict peerDependencies that is set in the npm version 7, and as of npm v7, peerDependencies are installed by default which results in conflict and causes the issue.

We can resolve the ERESOLVE unable to resolve dependency tree by using the --force flag while installing the dependencies, or we can ignore the peerDependencies by passing an additional argument --legacy-peer-deps

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Node.js

Node.js Environment Setup

Table of Contents Hide NodeJS Installation Video TutorialInstalling Node.js and npm on Windows Operating SystemInstalling Node.js and npm on MacInstalling Node.js using XCode and HomebrewInstall Node.js on Ubuntu It’s very…
View Post