AI summary

How package-lock.json pins your dependency versions, why it belongs in the repo, and how npm install and npm ci treat it differently.

  • package.json says which versions each package may use. package-lock.json records the exact version that got installed.
  • Commit package-lock.json and use npm ci in CI and deployments.
  • npm install can rewrite package-lock.json. npm ci never does: it stops with an error instead.

If your package.json says "express": "^4.18.0", that isn’t a version. It’s a range of acceptable versions. Say you install the project today and a teammate installs it two months later. If a new 4.x came out in between, you’re running different code. The same goes for the packages Express itself depends on.

package-lock.json, the lockfile, closes that gap. npm installs packages into the node_modules folder, and it writes the lockfile whenever it changes that folder or package.json.

The lockfile records the exact version of every installed package, including the packages your packages depend on, and where each one was downloaded from.

It also stores an integrity hash for each package. That’s a fingerprint of the package file, so npm can check that a new download is the same file it locked.

There’s a speed benefit too: npm can skip asking the registry again about packages it has already installed.

Commit the lockfile

The npm docs say the file is meant to go in your repository. Dependency changes then show up in diffs, so when something “worked yesterday” you can see which package moved.

If you publish a package, the lockfile doesn’t ship with it. Your users’ npm picks versions from your ranges on its own, so the file only affects your project.

What ^ and ~ allow

Versions follow major.minor.patch: a bug fix bumps patch, a backward-compatible feature bumps minor, and a breaking change bumps major. The symbol at the start of a range, the caret (^) or the tilde (~), says which digits can move.

Which versions a range acceptsPick a range.

^5.0.0

Caret pins the leftmost non-zero digit. Here that's the major.

  • 5.0.1 accepted (added)
  • 5.9.0 accepted (added)
  • 6.0.0 rejected (removed)

~5.7.1

Tilde only allows patch updates.

  • 5.7.9 accepted (added)
  • 5.8.0 rejected (removed)
  • 6.0.0 rejected (removed)

5.7.1

With no symbol, only this version.

  • 5.7.1 accepted (added)
  • 5.7.2 rejected (removed)

^0.2.3

With major 0, caret gets stricter and the minor stays fixed.

  • 0.2.9 accepted (added)
  • 0.3.0 rejected (removed)

npm install vs npm ci

npm install is the everyday command. npm ci is the one meant for CI and deployments: it installs exactly what the lockfile says.

The two commands treat the lockfile differently:

What happens to the lockfile on installPick a case. The versions are examples.

No lockfile

npm picks the newest version in the range and writes down the result.

  • package.json: express ^4.18.0
  • Installed: newest 4.x on that day (changed)
  • package-lock.json is created (added)

npm install, lockfile matches

The locked version fits the range, so the lockfile wins. A newer 4.x won't be installed.

  • package.json: express ^4.18.0
  • Lockfile: 4.18.2
  • Installed: 4.18.2

npm install, lockfile doesn't match

When they don't match, package.json wins and the lockfile gets updated.

  • package.json: express ^5.0.0 (changed)
  • Lockfile: 4.18.2 (removed)
  • Lockfile rewritten with a 5.x version (added)

npm ci, lockfile doesn't match

It exits with an error and doesn't try to fix the lockfile.

  • package.json: express ^5.0.0 (changed)
  • Lockfile: 4.18.2
  • Error, nothing installed (removed)

Here’s the same thing with real packages. I ran it with npm 12.2.2 on Node.js 24.

The --no-audit and --no-fund flags only turn off npm’s security and funding notes. head keeps the first lines of the error. npm ls express shows the installed Express version and where it sits in the dependency tree.

The newest 4.x on the registry was 4.22.3, but npm install kept the locked 4.18.2:

bash
# package.json: "express": "^4.18.0". Lockfile: express 4.18.2.
$ npm install --no-audit --no-fund

added 68 packages in 346ms
$ npm ls express
[email protected] /app
`-- [email protected]

# Change the range in package.json to "^5.0.0", then:
$ npm ci 2>&1 | head -6
npm error code EUSAGE
npm error
npm error `npm ci` can only install packages when your package.json and package-lock.json are in sync. Please update your lock file with `npm install` before continuing.
npm error
npm error Invalid: lock file's [email protected] does not satisfy [email protected]
npm error Invalid: lock file's [email protected] does not satisfy [email protected]

$ npm install --no-audit --no-fund

added 4 packages, removed 7 packages, and changed 28 packages in 1s
$ npm ls express
[email protected] /app
`-- [email protected]

The real error goes on with a line for every package that doesn’t match the lockfile, then npm ci’s usage help. Nothing gets installed. npm install rewrites the lockfile and moves to 5.x.

Use npm ci in CI and deployments. It fails if the lockfile is missing or doesn’t match package.json.

The two files can stop matching without anyone noticing. For example, someone changes a range in package.json by hand and commits it without running npm install. The lockfile still holds the old version. If the new range doesn’t accept that version, npm ci stops before anything is installed, as in the output above.

A lockfile conflict resolved badly during a merge can leave the same mismatch. So after any merge that touched the lockfile, run npm ci: it shows the mismatch right away. I cover this case in my article on git merge –squash.

npm ci also deletes node_modules before it starts, and it never writes to package.json or the lockfile.

My site’s deploy works this way. Its GitHub Actions workflow runs npm ci before the build and the checks. The rest of that setup is in my Terraform and Cloudflare Pages article.

While developing, use npm install to add packages and npm update to move within your ranges. By default npm update leaves package.json alone; it updates node_modules and the lockfile.

Don’t edit the lockfile by hand. Deleting it and regenerating it isn’t a safe fix either: npm picks a version for every range again, so you end up doing a bulk upgrade without meaning to.

References

  1. npm ls documentation
  2. npm package-lock.json documentation
  3. npm ci documentation
  4. npm install documentation
  5. npm package.json documentation: files npm never publishes
  6. npm update documentation
  7. node-semver range rules

Written by:

Full Stack Software EngineerSamsun, Türkiye

Full Stack Software Engineer. I started with client sites at an agency in 2019, then worked on embeds, a dashboard and analytics at a startup called Popupsmart.