ERESOLVE unable to resolve dependency tree error when installing npm packages
The error “ERESOLVE unable to resolve dependency tree” occurs when installing npm packages has unmet peer dependencies. The conflict or incompatible versions of packages in the project will lead to this error.
To solve this error, try the below steps.
- Ignore the peerDependencies by --legacy-peer-deps flag
- Clear npm cache
- Downgrade the npm version from 7.x to 6.x
- Remove node_modules and package.json. Then run npm install.
Ignore the peerDependencies by --legacy-peer-deps flag
You can solve the error by running the below command.
npm install --save --legacy-peer-deps
In npm version 7.x or more, the peer dependencies are more strict than that of 6.x. So by adding --legacy-peer-deps flag, we are telling npm to use the version 6 algorithm to resolve peer dependencies.
Instead of passing --legacy-peer-deps flag, you can enable it in the npm config by running the below command.
npm config set legacy-peer-deps true
When reading this blog, some of you may be got confused with dependencies, devDependencies, and peerdependencies. Let us explain them.
The libraries under dependencies are packages that our project really needs to run our code. When you run npm install some-package, then npm will install that package and it will also be added to the dependencies object in the package.json file.
The devDependencies are the packages needed during development and they are not necessary for the production build. The Babel plugins and presets, and linter packages are some examples of devDependencies.
The peerdependencies are a little different from dependencies and devDependencies. Let us take an example, you are creating your own library(Library A) and you are using some other npm package(Package B) in it. Under peerdependencies, you can specify the version of Package B you are using Library A. Let us imagine your library(Library A) is going to use in a project (MainProject) and MainProject also uses this peer-lib package(Package B). To make sure the version of the peer-lib package(Package B) used in MainProject will work with your version in Library A, you should use peerDependencies.
Read more about npm 7.
Clear npm cache to solve unable to resolve dependency tree
You are still getting the error after passing --legacy-peer-deps flag, The issue may be with cached npm modules. So our next step is to clear the npm cache. For this run the below command.
npm cache clean --force
Downgrade the npm version from 7.x to 6.x
The npm 7.x has some breaking changes compared to 6.x. In versions before npm 6.x or before developers had to install the peer dependencies. From 7.x, npm uses a new algorithm to install dependencies properly. If a peer dependency, is not compatible with the other or got some conflicts, then npm 7 will now block the installation.
So another option to solve the above error is to downgrade the npm version from 7.x to 6.x.
npm i [email protected]
Remove node_modules and package.json. Then run npm install
The next step is to remove node_modules folder and package.json file from the project.
Then run the below command to clear the npm cache.
npm cache clean --force
Then install the npm packages using running the below command.
To solve the “ERESOLVE unable to resolve dependency tree” error when installing npm packages, you can ignore the peerDependencies by --legacy-peer-deps flag or you can downgrade the npm version from 7.x to 6.x. If the issue still exists, then delete the node_modules folder and package.json file from the project and clear the npm cache. Then run npm install.