More Node.js answers

by Danny
Published 2 min read

npm stops with ERESOLVE when two packages want incompatible versions of a peer dependency. Learn how to read the error and fix it with upgrades, overrides or legacy peer deps.

The short answer

#

One of your packages declares a peer dependency, usually something like React or ESLint, at a version range that does not match the version you have installed. Since npm 7, npm installs peer dependencies for you and refuses to continue when the ranges conflict. The best fix is to upgrade the package that is out of date. If you cannot, pin the shared version with overrides, or fall back to --legacy-peer-deps.


_10
# See who needs what
_10
npm explain react
_10
_10
# Last resort: skip automatic peer dependency installs
_10
npm install --legacy-peer-deps

Why it happens

#

A peer dependency is a package that a library expects you to provide, rather than one it installs for itself. A React component library, for example, lists react as a peer so that your app and the library share one copy.

npm 6 only warned about peer conflicts. npm 7 and later treat them as part of the dependency tree and install them automatically, so a conflict becomes a hard error. The message has a pattern worth reading slowly (recent npm versions print npm error instead of npm ERR!):


_10
npm ERR! Found: [email protected]
_10
npm ERR! Could not resolve dependency:
_10
npm ERR! peer react@"^17.0.0 || ^18.0.0" from [email protected]

"Found" is what you have. "Could not resolve" names the package with the strict peer range. Here the date picker has not declared support for React 19 yet.

How to fix it

#

Upgrade the package with the old peer range

Check whether a newer release of that package supports your version. This is the real fix, because the maintainers have tested it.


_10
npm view some-date-picker peerDependencies
_10
npm install some-date-picker@latest

If no release supports it yet, check the project's issues. Often a beta or a known-good workaround exists.

Pin the version with overrides

If you have checked that the package works with your version, tell npm to use your version everywhere with overrides in package.json (npm 8.3 and later). The $react value means "the version in my own dependencies".


_11
{
_11
"dependencies": {
_11
"react": "^19.2.0",
_11
"some-date-picker": "^4.1.0"
_11
},
_11
"overrides": {
_11
"some-date-picker": {
_11
"react": "$react"
_11
}
_11
}
_11
}

Scoping the override to one package, as above, is safer than overriding react for the whole tree. Delete node_modules and package-lock.json only if the install still uses the old resolution, then run npm install again.

Use --legacy-peer-deps

This restores npm 6 behaviour: peer dependencies are not installed or checked. It works, but it hides every peer conflict, not just this one. If your project needs it, record it in .npmrc so everyone on the team and CI installs the same way.


_10
echo "legacy-peer-deps=true" >> .npmrc

Avoid --force

npm install --force also gets past the error, but it can install conflicting copies and change other parts of the tree unpredictably. Prefer the options above.

Common pitfalls

#
  • Running --legacy-peer-deps locally but not in CI, so the build fails with the same error later. Put it in .npmrc if you use it.
  • Deleting the lock file as a first step. It throws away every pinned version, not only the broken one.
  • Overriding a peer to a major version the package truly does not support. The install succeeds and the app fails at runtime.
  • Ignoring the error in a fresh create-* project. It usually means a template is behind the latest framework release, and an older framework version may be the better choice.
#

Was this resource helpful?