More Node.js answers

by Danny
Published 2 min read

Use npm install while developing and npm ci in CI and deployments. Always commit package-lock.json so every machine installs exactly the same dependency versions.

The short answer

#

Use npm install on your own machine when you add, remove or update packages. Use npm ci in CI pipelines, Docker builds and deployments, where you want an exact, repeatable install. And yes, commit package-lock.json.


_10
# Developing: may update package-lock.json
_10
npm install
_10
_10
# CI and deploys: installs exactly what the lockfile says
_10
npm ci

Why it happens

#

Your package.json lists version ranges, such as ^4.2.0, which means "4.2.0 or any later 4.x". Two people running npm install a week apart could get different versions, because new releases appeared in between. That includes the dependencies of your dependencies, which you never listed at all.

package-lock.json fixes this. It records the exact version, download URL and integrity hash of every package in the tree. When it is committed, everyone gets the same node_modules.

The two commands treat the lockfile differently:

npm installnpm ci
Reads the lockfileYesYes
Can change the lockfileYesNever
If package.json and lockfile disagreeUpdates the lockfileFails with an error
Existing node_modulesKept and updatedDeleted first
Install a single packageYesNo

So npm install is flexible, which is what you want while working. npm ci is strict, which is what you want when a build must match what you tested.

How to fix it

#

Use npm ci in CI

In a pipeline, npm ci gives you the exact tree from the lockfile and fails loudly if someone changed package.json without updating the lockfile.


_10
npm ci
_10
npm test
_10
npm run build

Because it deletes node_modules first, cache the npm download cache (usually ~/.npm) rather than node_modules. Most CI setup actions for Node.js can do this for you.

For a production image that does not need dev tools, leave them out:


_10
npm ci --omit=dev

Use npm install while developing

Add or upgrade packages with npm install, then commit both files together.


_10
npm install zod
_10
npm install -D vitest@latest
_10
git add package.json package-lock.json
_10
git commit -m "Add zod, upgrade vitest"

After pulling someone else's changes, either command works. Running npm ci is a good habit when things feel broken, because it gives you a clean node_modules that matches the lockfile exactly.

Commit package-lock.json

Commit it for apps and for libraries. For an app, it is what makes deploys repeatable. For a library, npm does not publish the lockfile to the registry, so it does not affect your users, but it still keeps your own tests and CI consistent.

If you get a merge conflict in the lockfile, do not edit it by hand. Resolve the conflict in package.json, then run npm install and npm will rebuild the lockfile for you.

Common pitfalls

#
  • Adding the lockfile to .gitignore. This makes npm ci impossible and installs drift between machines. Remove it from .gitignore and commit it.
  • "npm ci can only install packages when your package.json and package-lock.json are in sync". Someone edited package.json without running npm install. Run it locally and commit the updated lockfile.
  • Deleting the lockfile to fix a problem. This silently upgrades every package in the tree to the newest version allowed by your ranges. Try npm ci first, and update specific packages with npm install name@version.
  • Mixing package managers. A yarn.lock or pnpm-lock.yaml next to package-lock.json means people are installing in different ways. Pick one and delete the others.
  • Different npm versions. Older npm versions write older lockfile formats. Keep npm reasonably current across your team and CI to avoid noisy lockfile diffs.
#

Was this resource helpful?