Resolve Dependency Issues with npm –legacy-peer-deps

Updated

npm package manager logo

--legacy-peer-deps makes npm install ignore peerDependencies entirely, the way npm 3–6 did. It will get you past an ERESOLVE error, and it does so by shipping the exact tree the package authors declared incompatible — including, in the case below, not installing a required package at all. npm exits 0 and says “added 1 package”. The failure surfaces later, in your application.

What the flag actually does, measured

Two local packages: hw-plugin declares "peerDependencies": { "hw-core": "^1.0.0" }, and a project that depends on hw-plugin alone. Under npm 10’s default behaviour, npm goes and gets the peer:

$ npm install
npm warn peer hw-core@"^1.0.0" from hw-plugin@1.0.0
npm error request to https://registry.npmjs.org/hw-core failed
     (this scratch package does not exist on the public registry — the point is that npm went looking)

Same package.json, one flag added:

$ npm install --legacy-peer-deps
added 1 package in 162ms
$ ls node_modules
hw-plugin
$ node -e "require('hw-core')"
Error: Cannot find module 'hw-core'

Exit status 0. No warning. The peer dependency the plugin says it needs is simply not there. npm 6 behaved this way too — the difference is that npm 6 printed a loud UNMET PEER DEPENDENCY warning, and --legacy-peer-deps on npm 10 does not. npm’s own config documentation is exact about the scope: the flag “causes npm to completely ignore peerDependencies when building a package tree, as in npm versions 3 through 6.”

The ERESOLVE error you are trying to escape

A genuine conflict — the project pins hw-core@2.0.0 while hw-plugin requires a ^1.0.0 peer:

$ npm install
npm error code ERESOLVE
npm error ERESOLVE unable to resolve dependency tree
npm error
npm error While resolving: hw-eresolve-demo@1.0.0
npm error Found: hw-core@2.0.0
npm error node_modules/hw-core
npm error   hw-core@"file:pkgs/hw-core-2.0.0.tgz" from the root project
npm error
npm error Could not resolve dependency:
npm error peer hw-core@"^1.0.0" from hw-plugin@1.0.0
npm error node_modules/hw-plugin
npm error   hw-plugin@"file:pkgs/hw-plugin-1.0.0.tgz" from the root project
npm error
npm error Fix the upstream dependency conflict, or retry
npm error this command with --force or --legacy-peer-deps
npm error to accept an incorrect (and potentially broken) dependency resolution.

Read that error before you silence it. It names both sides: something wants hw-core@^1.0.0, something else pinned 2.0.0. npm’s own last line calls the result of the two escape hatches “an incorrect (and potentially broken) dependency resolution”, which is a more honest description than most write-ups give.

–legacy-peer-deps vs –force vs fixing it

These are not three flavours of the same thing.

--legacy-peer-deps stops looking at peer ranges. Applied to the conflict above, it installed and exited 0 — and left the contradiction in place, which npm ls will still tell you about if you ask:

$ npm ls --all
hw-eresolve-demo@1.0.0
├── hw-core@2.0.0
└─┬ hw-plugin@1.0.0
  └── hw-core@2.0.0 deduped invalid: "^1.0.0" from node_modules/hw-plugin

npm error code ELSPROBLEMS
npm error invalid: hw-core@2.0.0 /path/node_modules/hw-core
$ echo $?
1

That is the check to put in CI if you use this flag: npm ls exits non-zero and prints invalid: for every package whose peer range is violated. It is the only thing between you and forgetting.

--force is different: it downgrades ERESOLVE to a warning and then goes back to the registry to satisfy the peer, typically by installing a second, nested copy of the package at the required version. Under --force the same install emitted npm warn ERESOLVE overriding peer dependency and then a request for hw-core@^1.0.0 from registry.npmjs.org. Two copies of a library in one tree brings its own class of bug — duplicated singletons, two React instances, instanceof checks that fail. npm documents --force broadly, as removing “various protections against unfortunate side effects, common mistakes, unnecessary performance degradation, and malicious input”; peer conflicts are one item on that list, so you get the rest of it too.

Fixing the tree means changing a version so the declared ranges agree. Here, using the version the peer actually asks for:

$ npm install                    # hw-core pinned to 1.0.0 instead
added 2 packages in 158ms
$ npm ls --all
hw-eresolve-demo@1.0.0
├── hw-core@1.0.0
└─┬ hw-plugin@1.0.0
  └── hw-core@1.0.0 deduped
$ echo $?
0

One thing to know before you go looking for a middle ground: npm has no “I reviewed this peer conflict and it’s fine” mechanism. overrides in package.json does not relax a peer range — pointing an override at a package you depend on directly is explicitly disallowed per the package.json documentation (“You may not set an override for a package that you directly depend on unless both the dependency and the override itself share the exact same spec”), and adding a peerDependencies block inside an override changed nothing here: the install still failed with the same ERESOLVE. Your options really are the two blunt flags or a version change.

If you use the flag anyway

Sometimes the upstream package is abandoned and the peer range is simply stale. In that case: put it in .npmrc rather than in people’s fingers, so it is visible in the repository and applies identically in CI —

legacy-peer-deps=true

— add a comment next to it saying which package forced it and what would let you remove it, and run npm ls in CI so a new conflict cannot hide behind the old one. Never put it on a project-wide default “just in case”: from then on, every genuine incompatibility anyone introduces installs silently.

Not tested here

The conflict was constructed from two packages packed locally with npm pack, so the whole run is offline and reproducible; it is not a React/Angular version clash, though the resolution mechanics are identical. The --force path could not be run to completion, because completing it requires fetching a satisfying version from the registry and these scratch packages are not published — what was observed is that --force issued that registry request while --legacy-peer-deps did not, which is the behavioural difference being described. Quotations are from npm 10’s own config and package.json documentation. Everything under a $ prompt ran on npm 10.9.4 / Node 22.22.1 on 2026-08-10.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.