With npm, a lockfile was written by `npm install --legacy-peer-deps` on a laptop and `npm ci` now fails in CI — why, and what is the durable fix?
answer
- flags that shape the tree
- ci rebuilds before it compares
- same flags on every install
- a committed project config file
- config set writes the user file by default
basics
~20 s--legacy-peer-deps changes the tree npm builds, so npm ci without it computes a different tree than the lock records and fails. Commit legacy-peer-deps=true to the project .npmrc, or fix the peer conflict and regenerate the lock without it.
solid answer
~40 s`npm ci` doesn't just copy the lock: it builds the tree `package.json` calls for under the current configuration and checks that the lock matches. `--legacy-peer-deps` makes npm ignore `peerDependencies` entirely while building, as npm 3 to 6 did, so a lock written with it lacks the peer placements a default build expects. In CI the default build then hits the original `ERESOLVE` peer conflict or reports the lock out of sync. npm's rule is that tree-shaping flags used to write the lock, such as `--legacy-peer-deps` and `--install-links`, must be given to `npm ci` too. The durable step is `npm config set legacy-peer-deps=true --location=project` and committing that `.npmrc`, so every install agrees. Better still, fix the peer conflict upstream and drop the flag.
code
bash · 6 lines# write the setting into the project's .npmrc, not the user's ~/.npmrc
npm config set legacy-peer-deps=true --location=project
cat .npmrc
# legacy-peer-deps=true
git add .npmrc package-lock.json
git commit -m "Build every install with legacy-peer-deps until the peer conflict is fixed"go deeper
Recall that npm ci must run with the same tree-shaping flags as the install that wrote the lock, legacy-peer-deps being the usual one.
Explain that npm ci builds the tree from package.json with the current configuration and compares it with the lock, so a flag that changes the tree changes the comparison.
Give the durable fix: a committed project .npmrc for the flag now, then a plan to fix the peer conflict and remove the flag, rather than a CI-only workaround.
Treat tree-shaping flags as project configuration with an owner and an exit plan, because a permanent legacy-peer-deps hides every future peer conflict.
## What `npm ci` actually compares A common belief is that `npm ci` simply unpacks whatever `package-lock.json` lists. It does more: it loads the lock, then **builds the ideal tree from `package.json` using the current configuration**, and compares the two. If they differ, it stops with "`npm ci` can only install packages when your package.json and package-lock.json are in sync" and lists the `Missing:` and `Invalid:` entries. Any setting that changes how npm builds a tree therefore changes the comparison. npm's documentation for `npm ci` says it outright: if you created the lock with flags that affect the shape of the dependency tree, such as `--legacy-peer-deps` or `--install-links`, you must provide the same flags to `npm ci` or you are likely to see errors. ## How `--legacy-peer-deps` changes the tree Since npm 7, npm installs `peerDependencies` by default. A conflict deep in the graph that npm can reasonably guess around only produces a warning, but colliding peer ranges it cannot place stop the install with an `ERESOLVE` error, plus a report saved as `eresolve-report.txt`. The error offers two escape hatches, retrying with `--force` or `--legacy-peer-deps`, "to accept an incorrect (and potentially broken) dependency resolution". - **`--legacy-peer-deps`** makes npm completely ignore `peerDependencies` when building the tree, as npm 3 to 6 did. - **`--omit=peer`** is different: npm still designs the tree for peers but does not unpack them on disk. - **`strict-peer-deps`**, the opposite direction, turns every peer conflict, even one npm could guess around, into a failure. A developer who hit `ERESOLVE` and reran with `--legacy-peer-deps` got a lock describing a tree **without** the peer placements. On the CI runner, `npm ci` runs with default settings, rebuilds the tree **with** peers, and either meets the same `ERESOLVE` or finds peers missing from the lock. ## The rule: same tree-shaping flags everywhere | Flag | What it changes in the tree | Needed at `npm ci`? | |---|---|---| | `--legacy-peer-deps` | ignores `peerDependencies` while building | yes, if the lock was written with it | | `--install-links` | packs `file:` dependencies and installs them like registry packages instead of symlinking (no effect on workspaces) | yes, if the lock was written with it | The safe pattern is to stop passing these on the command line at all and make them **project configuration**. ## Making the setting stick 1. Run `npm config set legacy-peer-deps=true --location=project`. Without `--location`, `npm config set` writes the **per-user** file (`~/.npmrc`), which never leaves the laptop. 2. Confirm the project's `.npmrc`, next to `package.json`, now contains `legacy-peer-deps=true`. In `.npmrc` the key is written without leading hyphens. 3. Commit `.npmrc`. Every `npm install` and every `npm ci`, on every machine, now builds the same tree. 4. Regenerate the lock once with the setting in place if it was written inconsistently, and commit it. Project `.npmrc` sits above the user and global files in npm's precedence, below environment variables and command-line flags, so a committed value applies everywhere unless someone overrides it deliberately. ## Diagnosing which case you are in Before changing anything, establish what the runner and the laptop actually used: - **Read the `npm ci` output.** An `ERESOLVE` block means the default build could not place the peers at all; `Missing:` lines under the out-of-sync headline mean it placed them, but the lock never recorded them. - **Ask npm for the effective value.** `npm config get legacy-peer-deps` on the laptop and in the CI job shows whether the two environments really differ. - **Check the repository.** If the project has no `.npmrc`, or it lacks the key, the laptop's setting came from a user file or from a flag typed once and forgotten. ## Getting rid of the flag Committing `legacy-peer-deps=true` makes the build consistent, but it also hides every **future** peer conflict, because npm stops checking the peer contract that packages may rely on. Treat it as debt: - Read the `ERESOLVE` report to find which package declares the stale peer range. - Upgrade that package to a release whose peer range accepts what you have, or align your own versions with it. - Run `npm install` without the flag, delete the line from `.npmrc`, and commit the regenerated lock. - Don't swap in `--force` as a project default: it removes several unrelated protections at once, from `engines` checks to `npm audit fix` staying within your ranges.
- Why does `npm config set legacy-peer-deps=true` on the laptop not fix CI?Without `--location`, `npm config set` modifies the user configuration file, `~/.npmrc`, which exists only on that laptop. The runner never sees it. `--location=project` writes the project's `.npmrc` beside `package.json`, which you commit, so every machine that installs the project reads the same setting.
- Is `--force` a better flag to commit than `--legacy-peer-deps`?No. npm's `ERESOLVE` message offers both only as ways to accept an incorrect and potentially broken resolution. `--force` also removes unrelated protections, such as `engines` checks and `npm audit fix` staying inside your ranges, so making it a project default widens the blast radius. Fixing the peer conflict is the durable answer.
- Which other install flag carries the same rule?`--install-links`. With it, `file:` dependencies are packed and installed like registry packages instead of symlinked, so the lock records a different tree. A lock written with it needs `--install-links` at `npm ci` as well, or `install-links=true` in the committed project `.npmrc`.
- npm ci only unpacks the lock, so install flags cannot affect it.
- Setting the flag with a plain npm config set is enough for CI to pick it up.
- Adding --force to npm ci is the clean fix for a peer conflict.
- legacy-peer-deps and --omit=peer do the same thing.
- Regenerating the lock in CI with npm install solves it for good.