npm ERR! ERESOLVE could not resolve dependency
Muhammad Mamunur Rashid
Sr. Software Engineer In Test at Arogga Ltd. || Software Engineer || Software Test Automation Engineer || Software Development Engineer in Test || Software Quality Assurance Engineer || Manual Test Engineer || Researcher
I was working on a project which had a node install task as a part of a Jenkins file build. The build was not successful due to the error shown below. This error “ERESOLVE unable to resolve dependency tree” occurs when installing npm packages that have 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
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.
领英推荐
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
Then run:
npm install
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.
npm install
Conclusion
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.
Empregado Público
10 个月Thank you kindly!
Information Technology Engineer at Solsten Diagnostics International ApS
10 个月Thank you, it seemed to solve my problem with "npm i redux-devtools-extension" for now
Frontend Developer || Passionate Environmentalist || Crafting Seamless User Experiences while Advocating for a Greener Future.
10 个月I am getting a socket timeout error message.
Electrical Engineering Student
10 个月thank you
Frontend Developer | I aspire to become a Full Stack Developer | HTML && CSS | JavaScript | React | Redux Toolkit | MERN Stack @CodeHelp | B.sc.IT | I am passionate about coding <\>
11 个月Thanks for sharing.