npm ERR! How to solve ERESOLVE unable to resolve dependency tree error?
Find the source of the npm ERESOLVE error; Match peer dependency versions, learn the difference between legacy-peer-deps and force, and verify the installation.
npm installseen duringERESOLVE unable to resolve dependency treeerror indicates that the version ranges claimed by the packages do not match in the same dependency tree. For example, if your project uses React 18 but a component reports that it only works with React 17, npm may not be able to resolve this relationship.
The permanent solution is to bring the packages mentioned in the bug report into compatible versions.--legacy-peer-depscan undergo installation; but it does not guarantee that the packages will work together correctly. Let's find out which relationship is broken first.
1. Find the two version ranges in the bug report
The following output is an instructive example;example-widgetIt's not a real package recommendation:
Found: react@18.2.0
Could not resolve dependency:
peer react@"^17.0.0" from example-widget@1.0.0
Foundinstalled version,peerindicates the version range that the other package expects. Peer dependency is the dependency that the package expects from the surrounding application. In npm 7 and later, these relationships are resolved more efficiently in the installation. Seeing the name of the error alone is not enough; Also record the conflicting packet and range.
node --version
npm --version
npm ls react
npm explain react
If your example is not React related, use the dependency name from the bug report in the last two commands.npm lsIt may give a non-zero exit code when it finds an invalid relationship; This is a result that can be expected at diagnosis.
2. Select compatible package version
Check the current version of the conflicting package and the peer dependency range it reports. In the following command, replace the placeholder with the actual package name from the error report:
npm view @latest peerDependencies
npm view versions --json
If the latest version is compatible with the framework your application uses, upgrade the package. If not compatible, consider another supported version or an alternative that is under maintenance. It is sometimes possible to downgrade the main framework version; but it also affects the application's other dependencies and the features it uses.
Actual version of your choicenpm install Install with . All packages at oncelatestUpgrading can turn a minor conflict into a major migration effort. Fixing the relationship in the report first makes it easier to track the impact of the change.
3. What do legacy-peer-deps and force do?
npm install --legacy-peer-depsuses the old behavior of not taking peer dependency relationships into account when building the dependency tree. Consider only in temporary situations where you are aware of the lack of release notification and can test the implementation.
npm install --forceloosens broader protections. No two options are the same; neither will make an incompatible API compatible. A successful installation is not the same as an application running.
The lock file temporarily created with the legacy setting may require the same setting in a CI environment. Clearly document this decision in the project configuration; Adding a hidden global npm setting to the developer's computer can create different setups across the team.
4. Do not delete the Lock file randomly
package-lock.jsonkeeps track of resolved versions. Deleting it may cause all allowed versions to be reselected. First fix the package ranges, update the lock file with normal installation and check out the Git difference. Clearing the cache also doesn't fix the version range conflict.
Check for clean installation in a separate checkout or CI environment.npm ciavailablenode_modulesIt rebuilds the directory and waits for the package.json and lock file to match:
npm ci
npm ls
npm run build
How do you know if the solution is working?
The installation must be completed without errors,npm lsrelevant relationshipinvalidand the screen or process using the changed package should work. Also run tests of your project. Verify that the same solution is replicated in both environments by matching the local and CI Node/npm versions.
Short guide
When to use?
If npm cannot install the dependency tree when adding new packages or upgrading the framework.
Common mistake
Using force without reading the bug report and considering the successful installation as proof of compatibility.