Fix npm ERR! code ERESOLVE: unable to resolve dependency tree
npm ERESOLVE unable to resolve dependency tree means a peer requirement clashes with your project's version. Read the Found and peer lines, then align them.
npm error code ERESOLVEnpm error ERESOLVE unable to resolve dependency treenpm errornpm error While resolving: app@1.0.0npm error Found: react@18.3.1npm error node_modules/reactnpm error react@"18" from the root projectnpm errornpm error Could not resolve dependency:npm error peer react@"17.0.2" from react-dom@17.0.2npm error node_modules/react-domnpm error react-dom@"17" from the root projectnpm ERESOLVE means the package you are installing (react-dom@17.0.2) requires a peer version (react@17.0.2) that does not overlap with the version already chosen for the project (react@18.3.1), so npm cannot build a tree. The fix is to move one of the two versions to match the other. The --force or --legacy-peer-deps hint at the end hides the conflict; it does not solve it.
npm 10 prefixes each line with npm error; older releases print the same lines with npm ERR!. The output above comes from npm install react@18 react-dom@17 in an empty project.
[1] Read two lines of the error. Found: is the version in the tree (or the one the root requires). The peer … from … line under Could not resolve dependency: tells you who requires what.
| Line | Value above | Meaning |
|---|---|---|
Found: react@18.3.1 · react@"18" from the root project | your package.json asks for react 18 | side you can change, #1 |
peer react@"17.0.2" from react-dom@17.0.2 | react-dom 17 wants react exactly 17.0.2 | side you can change, #2 |
Anything marked from the root project is a direct dependency, so you pick its version. If it says from <another package>, that package has to be upgraded.
[2] Check what other versions of the package accept. This only reads the registry; your project does not change.
$ npm view react-dom@17 peerDependenciesreact-dom@17.0.0 { react: '17.0.0' }react-dom@17.0.1 { react: '17.0.1' }react-dom@17.0.2 { react: '17.0.2' }No 17.x release accepts react 18, so upgrade react-dom to the same major as react. When the requirement comes from a library or plugin, run npm view <package>@latest peerDependencies to see whether the newest release accepts your version. If even the latest one does not, the library does not support it yet, and the only clean option is to lower the root version.
[3] Install matching versions and check the tree.
$ npm install react@18 react-dom@18$ npm ls react; echo exit=$?added 5 packages, and audited 6 packages in 2s found 0 vulnerabilitiesapp@1.0.0 /srv/app+-- react-dom@18.3.1| `-- react@18.3.1 deduped`-- react@18.3.1 exit=0Exit code 0 and no invalid in npm ls means the peers line up. Use the same exit code as a CI check.
[4] Know what --legacy-peer-deps leaves behind. The npm docs describe it as ignoring peerDependencies entirely, as npm 3 through 6 did, and say it is not recommended because it does not enforce the peer contract. With the same conflict, the install finishes but the tree stays broken.
$ npm install react@18 react-dom@17 --legacy-peer-deps$ npm ls react; echo exit=$?added 6 packages, and audited 7 packages in 3s found 0 vulnerabilitiesapp@1.0.0 /srv/app+-- react-dom@17.0.2| `-- react@18.3.1 deduped invalid: "17.0.2" from node_modules/react-dom`-- react@18.3.1 invalid: "17.0.2" from node_modules/react-dom npm error code ELSPROBLEMSnpm error invalid: react@18.3.1 /srv/app/node_modules/react…exit=1The bigger problem is the next install. The flag is not stored in package-lock.json, so after deleting node_modules in the same folder, a plain npm ci failed again.
$ rm -rf node_modules; npm cinpm error code ERESOLVEnpm error ERESOLVE could not resolvenpm errornpm error While resolving: react-dom@17.0.2…npm error Conflicting peer dependency: react@17.0.2If ERESOLVE shows up only in CI, a Docker build or a teammate's machine while your local install worked, this is usually why. The wording differs from the first error: could not resolve plus a Conflicting peer dependency line instead of unable to resolve dependency tree.
[5] If you cannot align versions yet, record the workaround in the project. One line in the project's .npmrc makes npm ci follow the same rule; in the test, npm ci passed once this line was added.
legacy-peer-deps=trueThis turns off every peer check in that project, so future conflicts will install silently too. Add a comment with the reason and when to remove it (for example "delete after moving to react-dom 18"). To undo, delete the line and run npm install to re-check.
--force disables other protections as well. On the same conflict it printed npm warn using --force Recommended protections disabled. and then installed. The npm docs say not to use it unless you have a clear idea of what you want.
[6] Do not change the global setting. npm config set legacy-peer-deps true goes into your user-wide ~/.npmrc and hides conflicts in every other project. If you already set it, check with npm config get legacy-peer-deps and remove it with npm config delete legacy-peer-deps.
EACCES: permission denied on a global install is a different problem; see npm install -g EACCES. If npm stops before resolving because it cannot find package.json, see Fix npm ENOENT: Could not read package.json; if resolution succeeds but a native module build then fails with gyp ERR! find Python, see Fix gyp ERR! find Python.