ERESOLVE unable to resolve dependency tree when npm install runs in nodejs and netlify

npm install is a command to install dependencies of an application. You can also check other posts on npm command deprecate option is deprecated and Fix for digital envelope routines::unsupported

Sometimes, When you are running npm install, You will get a dependency error npm ERR! ERESOLVE is unable to resolve the dependency tree. as seen below.

5:43:24 PM: Installing NPM modules using NPM version 7.11.1
5:43:33 PM: npm ERR! code ERESOLVE
5:43:33 PM: npm ERR! ERESOLVE is unable to resolve the dependency tree
5:43:33 PM: npm ERR!
5:43:33 PM: npm ERR! Found: [email protected]
5:43:33 PM: npm ERR! node_modules/compression-webpack-plugin
5:43:33 PM: npm ERR! compression-webpack-plugin@"9.0.1" from the root project
5:43:33 PM: npm ERR!
5:43:33 PM: npm ERR! Could not resolve dependency:
5:43:33 PM: npm ERR! compression-webpack-plugin@"9.0.1" from the root project
5:43:33 PM: npm ERR!
5:43:33 PM: npm ERR! Conflicting peer dependency: [email protected]
5:43:33 PM: npm ERR! node_modules/webpack
5:43:33 PM: npm ERR! peer webpack@"^5.1.0" from [email protected]
5:43:33 PM: npm ERR! node_modules/compression-webpack-plugin
5:43:33 PM: npm ERR! compression-webpack-plugin@"9.0.1" from the root project
5:43:33 PM: npm ERR!
5:43:33 PM: npm ERR! Fix the upstream dependency conflict, or retry
5:43:33 PM: npm ERR! this command with --force, or --legacy-peer-deps
5:43:33 PM: npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
5:43:33 PM: npm ERR!
5:43:33 PM: npm ERR! See /opt/buildhome/.npm/eresolve-report.txt for a full report.
5:43:33 PM: npm ERR! A complete log of this run can be found in:
5:43:33 PM: npm ERR! /opt/buildhome/.npm/\_logs/2021-11-26T12_13_33_353Z-debug.log
5:43:33 PM: Error during NPM install

This is an error related to dependency conflict version mismatch between direct and indirect dependency with incorrect and broken dependency.

As per the stack trace error above, You upgraded the webpack from 5.1.0 to 5.64.4 But did not upgrade indirect dependency compression-webpack-plugin 3.1.0 instead of 9.0.1

An application has webpack dependency trying to resolve from two places

This error occurs with any application type like angular, react,vuejs and gatsby. In that case, What is the solution for this?

How to Fix Conflicting peer dependency in javascript

There are multiple ways to fix peer dependency issues in Nodejs applications such as Angular, React and VuejS frameworks.

Solution#1, Upgrade to latest the npm version to the previous version

Here are the steps

  • remove the node_modules folder
  • remove package-lock.json
  • please do the npm install one more time to do a fresh installation of the dependency

Here is a sequence of commands you can do

rm -rf node_modules
rm package-lock.json
npm install

Solution#2 downgrade or upgrade the dependency In the above example, either downgrade webpack dependency to 5.1.0 or upgrade compression-webpack-plugin to 9.0.1

Solution#3 Run npm install with --force or --legacy-peer-deps option to set peer dependencies and resolve them automatically.

please try one of the below commands to resolve the dependency conflict

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

you can also set legacy-peer-deps using the command line or npmrc file globally.

Update .npmrc file with the below configuration .npmrc file

legacy-peer-deps=true

The same can be configured with the npm command using the below.

npm config set legacy-peer-deps true

Solution#4,

If the above approaches did not work, try with npm cache verify

How do you fix the dependency tree conflict in react?

To fix the dependency tree conflict in react application, Follow the below steps to resolve this.

  • Remove node_modules folder and package-lock.json file in a react projects
  • Run npm install —force instead of npm install command
  • if you got the same error, try with the npm install --legacy-peer-deps approach,
  • Upgrade the npm version to the latest version
  • or try npm cache clean --force command if it does not work

Fix for netlify Dependency conflict version

This is an error you used to get during the build and deployment pipeline in the netlify environment.

There is no command line to set peerDependency in netlify.

To resolve this, Go to the application root directory Create .npmrc file

Add or update the below configuration .npmrc file

legacy-peer-deps=true

Commit changes and deploy to the netlify environment.

It resolves an error and dependencies are solved successfully.

How do you fix npm Cannot resolve dependency?

To fix npm cannot resolve the dependency, the Following are steps are required to follow

  • Check the version of npm package
  • Install packages using npm install —force —legacy-peer-deps
  • also remove node_modules and package-lock.json and do fresh installation

How do I fix upstream dependency conflict?

To resolve upstream dependency conflicts, Try below approaches

  1. Remove node_modules and package-lock.json and do npm install
  2. (or) try with npm install —force
  3. (or) try with npm install —save-exact to install a specific version of a dependency
  4. (or ) try with npm install --legacy-peer-deps command

How do I fix conflicting peer dependencies npm?

In the Node.js project, you may have run into a situation where you have a conflict between dependencies. This can happen when you have different versions of a dependency in your application, or when a dependency has a conflict with another dependency.

There are a few ways to fix this issue. One way is to manually resolve the conflict by editing the package.json file. This can be done by specifying the exact version of the dependency that you want to use.

Another way to fix this issue is to use the --save-exact flag when installing dependencies. This will ensure that the exact version of the dependency is installed and that any conflicts are resolved.

If you’re still having trouble resolving dependency conflicts, you can try using a tool like npm install —force. This command will help you resolve conflicts by forcing the installation of specific versions of dependencies.

if you still got issues, try npm-force-resolutions to modify the transitive dependency version

Conclusion

To Sum up, Learned Different ways to resolve dependency version conflict in nodejs applications and netlify environment.