Peer Dependencies & overrides
Some packages don't work alone; they plug into a host. react-dom renders your react, react-redux connects your React to your Redux store, a Webpack plugin hooks into your Webpack. If each of them brought its own copy of the host, you'd get two Reacts in one page and errors that are hard to explain. So they declare the host as a peer dependency: "I need React, but use the one the application has."
Reading peer ranges
npm view react-dom@18.3.1 peerDependencies
npm view react-dom@19.3.1 peerDependencies
npm view react-redux@9.3.0 peerDependencies peerDependenciesMeta{ react: '^18.3.1' }
{ react: '^19.3.1' }
peerDependencies = {
react: '^18.0 || ^19',
redux: '^5.0.0',
'@types/react': '^18.2.25 || ^19'
}
peerDependenciesMeta = { redux: { optional: true }, '@types/react': { optional: true } }
react-dom18.3.1 needsreact@^18.3.1;react-dom19.3.1 needsreact@^19.3.1. The two must move together.react-redux9 accepts React 18 or 19, and Redux 5;peerDependenciesMetamarksreduxand@types/reactas optional peers: used if present, never installed for you.
A healthy tree
npm 7+ installs missing peers automatically and checks that every peer range is satisfied by a single copy:
mkdir -p shop && cd shop
npm init -y > /dev/null
npm install react@18.3.1 react-dom@18.3.1 redux@5.0.1 react-redux@9.3.0 2>&1 | grep added
npm ls --all | grep -E 'react@|redux@|UNMET'added 9 packages in 2s
│ ├── react@18.3.1 deduped
├─┬ react-redux@9.3.0
│ ├── UNMET OPTIONAL DEPENDENCY @types/react@^18.2.25 || ^19
│ ├── react@18.3.1 deduped
│ ├── redux@5.0.1 deduped
│ └── react@18.3.1 deduped
├─┬ react@18.3.1
└── redux@5.0.1
Every react@18.3.1 deduped line is a peer pointing at the one top-level React. UNMET OPTIONAL DEPENDENCY @types/react is informational: we're not using TypeScript here.
The conflict: React 18 with react-dom 19
Upgrade only react-dom and npm refuses:
mkdir -p shop && cd shop
npm init -y > /dev/null
npm install react@18.3.1 > /dev/null
npm install react-dom@19.3.1 2>&1 | sed -n '1,12p'
echo "exit code: ${PIPESTATUS[0]}"npm error code ERESOLVE
npm error ERESOLVE unable to resolve dependency tree
npm error
npm error While resolving: shop@1.0.0
npm error Found: react@18.3.1
npm error node_modules/react
npm error react@"^18.3.1" from the root project
npm error
npm error Could not resolve dependency:
npm error peer react@"^19.3.1" from react-dom@19.3.1
npm error node_modules/react-dom
npm error react-dom@"19.3.1" from the root project
exit code: 1
Read an ERESOLVE report from the bottom up: "Could not resolve dependency: peer react@^19.3.1 from react-dom@19.3.1" is the requirement that can't be met; "Found: react@18.3.1" is what's in the way and who asked for it.
What the escape hatches really do
npm suggests --legacy-peer-deps and --force. Both make the install succeed; neither makes the code work:
mkdir -p shop && cd shop
npm init -y > /dev/null
npm install react@18.3.1 > /dev/null
npm install react-dom@19.3.1 --legacy-peer-deps 2>&1 | grep added
npm ls react react-dom
cat > render.js <<'JS'
const React = require('react');
const { renderToString } = require('react-dom/server');
console.log(renderToString(React.createElement('h1', null, 'hello')));
JS
node render.js 2>&1 | grep -m1 '^Error'added 2 packages in 641ms
shop@1.0.0 /home/learner/shop
├─┬ react-dom@19.3.1
│ └── react@18.3.1 deduped invalid: "^19.3.1" from node_modules/react-dom
└── react@18.3.1 invalid: "^19.3.1" from node_modules/react-dom
npm error code ELSPROBLEMS
npm error invalid: react@18.3.1 /home/learner/shop/node_modules/react
npm error A complete log of this run can be found in: /home/learner/.npm/_logs/2026-10-23T17_39_12_020Z-debug-0.log
Error: Incompatible React versions: The "react" and "react-dom" packages must have the exact same version. Instead got:
--legacy-peer-deps ignores peer dependencies during the install (npm 6 behaviour), so the broken tree went in without complaint. npm ls flags it afterwards (invalid, exit code ELSPROBLEMS), and React itself refuses at runtime. --force goes further and overrides any conflict. Use them only to get unstuck temporarily, never as a committed fix (and never as legacy-peer-deps=true in a shared .npmrc, where everyone forgets about it).
The real fix is to move the peers together:
mkdir -p shop && cd shop
npm init -y > /dev/null
npm install react@18.3.1 react-dom@18.3.1 react-redux@9.3.0 > /dev/null
npm install react@19.3.1 react-dom@19.3.1 2>&1 | grep -E 'added|changed'
npm ls react react-dom
cat > render.js <<'JS'
const React = require('react');
const { renderToString } = require('react-dom/server');
console.log(renderToString(React.createElement('h1', null, 'hello from React ' + React.version)));
JS
node render.jsremoved 2 packages, and changed 3 packages in 790ms
shop@1.0.0 /home/learner/shop
├─┬ react-dom@19.3.1
│ └── react@19.3.1 deduped
├─┬ react-redux@9.3.0
│ ├── react@19.3.1 deduped
│ └─┬ use-sync-external-store@1.7.0
│ └── react@19.3.1 deduped
└── react@19.3.1
<h1>hello from React 19.3.1</h1>
react-redux accepted the upgrade without a change because its peer range already covers React 19. That's what a library's peer range is for: the widest range it is tested with.
Peer dependencies as a library author
| Do | Why |
|---|---|
list the host (react, webpack, eslint) in peerDependencies |
one copy, chosen by the app |
also list it in devDependencies at a concrete version |
so your own tests have it |
use wide ranges: "react": "^18 || ^19" |
every extra major you accept saves your users a conflict |
mark integrations optional in peerDependenciesMeta |
users without TypeScript don't need @types/react |
don't put the host in dependencies |
that's how apps end up with two Reacts |
overrides: forcing a version deep in the tree
Sometimes the conflict is not yours to fix: a dependency of a dependency pins an old version with a bug or a security hole. overrides in the root package.json replaces a package's version everywhere in the tree (or under one parent):
mkdir -p ov && cd ov
npm init -y > /dev/null
npm install is-odd@3.0.1 is-number@7.0.0 > /dev/null
npm ls --all
echo "--- force is-number 7 everywhere"
npm pkg set 'overrides.is-number=$is-number'
npm install 2>&1 | grep -E 'removed|changed|up to date'
npm ls --all
node -e 'console.log(require("is-odd")(7))'ov@1.0.0 /home/learner/ov
├── is-number@7.0.0
└─┬ is-odd@3.0.1
└── is-number@6.0.0
--- force is-number 7 everywhere
removed 1 package in 356ms
ov@1.0.0 /home/learner/ov
├── is-number@7.0.0
└─┬ is-odd@3.0.1
└── is-number@7.0.0 deduped
true
$is-number means "whatever version the root project depends on". npm requires that form here: an override for a package you also depend on directly must agree with your direct dependency, and writing 7.0.0 against the root's ^7.0.0 fails with EOVERRIDE. is-odd asked for ^6.0.0 and now shares the top-level 7.0.0 (deduped); the nested 6.0.0 was removed. Here it still works, because is-odd only calls isNumber(x); in general you are now running a combination nobody tested, so add a test and a comment. Overrides can be scoped: "overrides": { "is-odd": { "is-number": "7.0.0" } } only changes is-number under is-odd. Yarn calls the same feature resolutions, pnpm pnpm.overrides (or overrides: in pnpm-workspace.yaml).
Test yourself
Why does react-dom list react as a peer dependency rather than a dependency?
- To make installs faster
- So that the application's single copy of React is used instead of a private one
- Because React is optional
- Peer dependencies are never installed
What does --legacy-peer-deps do?
- Upgrades peers to match
- Ignores peer dependencies during resolution, so conflicts aren't reported
- Installs both versions side by side
- Removes peer dependencies from package.json
A transitive dependency pins a vulnerable version. Which root package.json field lets npm replace it everywhere?
- peerDependencies
- resolutions
- overrides
- bundleDependencies