---
title: "npm install vs npm ci: which should I use, and should I commit package-lock.json?"
description: "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."
url: https://www.danny.engineering/answer/nodejs/npm-install-vs-npm-ci-which-should-i-use-and-should-i-commit-package-lockjson
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T20:30:00.000Z
tags: ["Node.js"]
---

# npm install vs npm ci: which should I use, and should I commit package-lock.json?

## 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`.


```bash
# Developing: may update package-lock.json
npm install

# CI and deploys: installs exactly what the lockfile says
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 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.


```bash
npm ci
npm test
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:


```bash
npm ci --omit=dev
```


### Use npm install while developing


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


```bash
npm install zod
npm install -D vitest@latest
git add package.json package-lock.json
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.

## Related reading

- [npm docs: npm ci](https://docs.npmjs.com/cli/commands/npm-ci)
- [npm docs: package-lock.json](https://docs.npmjs.com/cli/configuring-npm/package-lock-json)
