Learning center

CI ran npm install, so it tested a lockfile nobody committed

When package.json and package-lock.json disagree, npm install resolves the difference, rewrites the lockfile on the runner and exits 0. The tests then run against a dependency tree that exists only in that job. npm ci fails instead, and pnpm refuses by default in CI. Use the installer that fails, and treat a green run that used npm install as covering a tree you cannot reproduce from the commit.

What each installer does with a mismatch#

npm's documentation lists the differences: with npm ci, "If dependencies in the package lock do not match those in package.json, npm ci will exit with an error, instead of updating the package lock", and it "will never write to package.json or any of the package-locks". npm ci npm install has no such rule. The lockfile "is automatically generated for any operations where npm modifies either the node_modules tree, or package.json". package-lock.json

pnpm's --frozen-lockfile defaults to true in CI "if a lockfile is present", and then pnpm "fails to install if the lockfile is out of sync with the manifest". pnpm install

Fixture: a dependency added without its lockfile#

npm 11.19.1 and pnpm 12.19.0 on Node 24.22.3. Both lockfiles are committed for ms. Ben then adds "is-number": "7.0.0" to package.json and commits without updating either lockfile:

$ npm ci
npm error code EUSAGE
npm error
npm error `npm ci` can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with `npm install` before continuing.
npm error
exit 1
$ npm install

added 2 packages in 2s
exit 0
$ git status --short
 M package-lock.json
$ git diff --stat
 package-lock.json | 10 ++++++++++
 1 file changed, 10 insertions(+)
$ CI=true pnpm install
Error: ERR_PNPM_OUTDATED_LOCKFILE
[…]
  ╰─▶ Cannot install with "frozen-lockfile" because pnpm-lock.yaml is not up
      to date with package.json.
[…]
exit 1

npm install reported success and left a modified lockfile in the job's workspace. Nothing in the commit records which version of is-number the tests used. Here the version was exact, so a later install would agree. With a range such as ^7.0.0, a later install can pick a newer release, and the tree that passed CI is gone when the job ends.

Make the job fail on drift#

- run: npm ci

If a step must run npm install, for example to try a dependency update, fail the job when it changed the lockfile:

- run: npm install && git diff --exit-code package-lock.json

git diff --exit-code exits 1 when there are differences. git diff For pnpm, keep the CI default and do not pass --no-frozen-lockfile.

Read an install log as evidence#

Install line in the logThe tested treeReproducible from the commit?
npm ci, exit 0The committed lockfileYes
npm install, lockfile unchangedThe committed lockfileYes, but only the diff proves it
npm install, lockfile changedA tree resolved during the jobNo
pnpm install with CI set and a lockfileThe committed lockfile, frozenYes

A failing npm ci has another cause besides drift. npm's documentation warns that a lockfile created by npm install with flags that change the tree's shape, such as --legacy-peer-deps, needs the same flags on npm ci, or it is "likely to encounter errors". Read the error before you regenerate the lockfile.

For the same question in a Python project, read which environment pip-audit checked. For a base image that moved under the same Dockerfile, see a base tag that moved between builds.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading