Benjamin Crozat “Heard about Sevalla? They let you deploy PHP apps with ease.” Claim $50 →

npm ci vs. npm install or reliability vs. speed

2 minutes read

npm ci vs. npm install or reliability vs. speed

Introduction

If you’ve ever wondered whether to run npm ci or stick with good old npm install, you’re in the right place. Here’s exactly what I learned.

What npm install really does

When I run npm install, here’s what’s happening:

Semver resolution and lockfile rewrites

First, npm reads my package.json, figures out the latest acceptable versions based on semver ranges, and checks against my package-lock.json. If anything doesn’t match exactly, it quietly rewrites the lockfile.

Incremental node_modules mutation

npm install tries to save time by only updating what’s necessary in node_modules. This incremental change is great for local development, especially for fast hot-reloading.

The npm ci difference

But what about npm ci? Here’s why it’s special:

Lockfile-first philosophy

npm ci completely trusts the lockfile. No version guessing, no automatic upgrades. Just precise, byte-for-byte consistency.

The “nuke & pave” node_modules step

Every time I run npm ci, it wipes out the entire node_modules folder before rebuilding it exactly according to the lockfile. This ensures absolute cleanliness.

Strict sync checks

If my package-lock.json and package.json aren’t perfectly synced, npm ci throws an error instead of guessing. That saved me countless hours chasing subtle bugs.

When I reach for npm ci (and when I don’t)

Here’s my personal rule-of-thumb:

  • npm ci: Always in CI pipelines, Docker builds, and production deployments. It ensures deterministic, fast, and predictable outcomes.
  • npm install: Daily local dev, especially when adding or upgrading dependencies frequently.

Common errors I still hit and quick fixes

Despite best practices, I still encounter occasional bumps:

  • “package-lock.json is out of sync”: Quickly fixed by running npm install --package-lock-only.
  • Native add-ons rebuild loop: Mitigated by caching the entire NPM cache directory between builds.

FAQ

  • Does npm ci respect .npmrc proxies? Yes, it fully respects npm configuration files.
  • Can I add a package with npm ci? Nope, use npm install for modifying dependencies.
  • Is pnpm still faster? Usually, yes—but npm ci is plenty fast for most scenarios.

TL;DR

  • Use npm ci for speed, consistency, and CI reliability.
  • Use npm install locally for flexibility and incremental updates.
  • Always commit and maintain a clean, synced package-lock.json.

Did you like this article? Then, keep learning:

0 comments

Guest