Togoder security

Glossary

npm ci vs npm install: what is the difference?

Also called: npm clean-install, npm ci, frozen lockfile installUpdated

Definition

npm ci (clean install) installs exactly the versions recorded in package-lock.json. It deletes node_modules first, stops with an error if the lockfile and package.json disagree, and never changes the lockfile. npm install resolves the version ranges in package.json, can pick newer versions and writes the result back to the lockfile.

npm ci and npm install compared

npm cinpm install
Needs a lockfileYes, fails without oneNo, creates one
Lockfile and package.json disagreeStops with an errorUpdates the lockfile
Writes package-lock.jsonNeverYes, when something changes
Existing node_modulesDeleted firstReused and adjusted
Can add one packageNoYes: npm install express
Use it forCI, Docker builds, deploymentsDevelopment, when you add or update dependencies

Why npm ci is the safer install

  • No surprise versions. The build gets the versions that were reviewed and committed, not the versions that are newest today. A malicious release published an hour ago cannot enter through a version range.
  • Integrity is checked. Each package is compared with the hash in the lockfile.
  • Changes are visible. Dependencies change only through a lockfile change in a pull request, which a person or a scanner can look at.
npm ci --ignore-scripts

Add --ignore-scripts on older npm versions so that install scripts of dependencies do not run; npm 12 blocks them unless you approve them.

The same in other package managers

yarn install --immutable
pnpm install --frozen-lockfile
bun install --frozen-lockfile
poetry sync
cargo build --locked

What npm ci does not do

npm ci installs what the lockfile says, without a judgment on it. If a malicious version is already in the lockfile, npm ci installs it faithfully each time. Check what a lockfile change brings in before you merge it: npm audit for reported problems, and a source scan of the lockfile in CI for unreported ones.

Frequently asked questions

Is npm ci faster than npm install?

In CI, usually yes: it skips dependency resolution and installs directly from the lockfile. On a developer machine it can be slower, because it deletes node_modules and installs everything again.

Does npm ci update package-lock.json?

No. npm ci never writes to package.json or package-lock.json. If they do not match, it stops with an error and you must run npm install to update the lockfile.

Does npm ci run postinstall scripts?

On npm versions before 12, yes, unless you add --ignore-scripts. npm 12 blocks the lifecycle scripts of dependencies unless you approve them.

Should I use npm ci in a Dockerfile?

Yes. Copy package.json and package-lock.json, then run npm ci, so the image contains exactly the locked versions and the layer is cached until the lockfile changes.