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_10npm install_10_10# CI and deploys: installs exactly what the lockfile says_10npm 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 install | npm ci | |
|---|---|---|
| Reads the lockfile | Yes | Yes |
| Can change the lockfile | Yes | Never |
| If package.json and lockfile disagree | Updates the lockfile | Fails with an error |
| Existing node_modules | Kept and updated | Deleted first |
| Install a single package | Yes | No |
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.
_10npm ci_10npm test_10npm 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:
_10npm ci --omit=dev
Use npm install while developing
Add or upgrade packages with npm install, then commit both files together.
_10npm install zod_10npm install -D vitest@latest_10git add package.json package-lock.json_10git 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 ciimpossible and installs drift between machines. Remove it from.gitignoreand commit it. - "npm ci can only install packages when your package.json and package-lock.json are in sync". Someone edited
package.jsonwithout runningnpm 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 cifirst, and update specific packages withnpm install name@version. - Mixing package managers. A
yarn.lockorpnpm-lock.yamlnext topackage-lock.jsonmeans 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.
Related reading
#Was this resource helpful?