This error commonly occurs while you update or install the npm package.
The reason is dependency conflict either due to incorrect or broken dependencies.
For example, the latest version for any deprecated package is 7.2.16 while you request an upper version, let's say - 8.2.4 then the error will be raised.
Programmers face this issue mostly while installing the node modules using the latest node.js and npm version 7.
Here, we will discuss this error in detail and fix the issue.
When you get this dependency tree error the output will be similar to the terminal below:
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
In earlier versions(v3 to v6) of npm, it ignores the peerDependencies during building the package tree and result in no error during updates.
But in the new version(v7) of npm, npm install fails due to conflicts in peerDependencies tree as we have discussed earlier in the article.
To resolve this error, we can use a couple of ways such as using --legacy-peer-deps or --force flag with npm command.
Let's understand with some examples.
Here, we used the --legacy-peer-deps parameter with the npm command to install. This flag is used to ignore the peer dependencies and continue the installation of the package.
It basically instructs the npm to ignore the errors and install the rest of the dependencies. Use the below command to install the dependencies.
npm install --save --legacy-peer-deps
If you want to set it permanently into the configuration then use 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.
This is another solution where you can use the --force or -f flag. This is used to force npm to install the dependencies even if it is broken.
Use the below step by step solution:
Step 1: First remove the current node_modules by using the rm command as below.
rm -rf node_modules
Step 2: Now, remove the package-lock.json file by using the below command.
rm package-lock.json
Step 3: Now, clear the cache by using the clean command and use the -- force flag with npm to install the new updates.
npm cache clean --force
npm install --force
Now, the error will be resolved and you will get the updated package successfully.
This article consists of the solutions for the error: unable to resolve dependency tree error while installing npm packages with the latest version(v7) of npm.
Programmers face this issue mostly while installing the node modules using the latest node.js and npm version 7.
We resolved this issue by using the --legacy-peer-deps and --force flag with the npm command while installing the dependencies.
For more information, you can refer to this official article.