What is the difference between npm install and npm ci?
npm install can update the lock file and resolves versions from package.json. npm ci deletes node_modules and installs exactly what package-lock.json says, failing if the two disagree, which makes builds repeatable.
Explain it simply
npm install is like cooking from a recipe with notes such as "some tomatoes". npm ci is like cooking from a printed bill of exact quantities. The first is flexible and good on your laptop. The second gives the same result every time, which is what a build server wants.
Example
# daily development: add or update packages
npm install axios
# CI pipeline or Docker build: clean, exact, fast
npm ci
# production image, skip devDependencies
npm ci --omit=devWhat npm ci does differently:
- Removes any existing
node_modulesfirst. - Never writes to
package.jsonorpackage-lock.json. - Exits with an error if the lock file and package.json do not match, or if there is no lock file.
- Installs only what the lock file lists, so it is often faster.
If the pipeline fails with "package.json and package-lock.json are not in sync", someone changed a dependency without committing the updated lock file.
What to say in the interview
"I use npm install while developing, and npm ci in CI and Docker because it installs exactly from the lock file, wipes node_modules, and fails on mismatch, so builds are reproducible."
Common mistakes
- Running
npm installin a pipeline and getting different versions from the developer's machine. - Not committing package-lock.json.