Learning navigation

Code with Animation

Deterministic CI Builds & Cache Optimization

Use npm ci for a clean installation from an agreed manifest and lockfile. It refuses mismatches without rewriting the lock; complete build output still depends on the environment and scripts.

Package Managers5 min readConcept 22 of 24

What is it?

npm ci stands for Clean Install. Unlike npm install, which attempts to resolve dependencies and can update package-lock.json, npm ci explicitly reads from the lockfile and fails if the package.json and lockfile are out of sync.

It also completely deletes the existing node_modules folder before installing, ensuring there are no orphaned packages from previous builds.

Why use it?

npm install can reconcile changed requirements and update a lockfile. npm ci makes disagreement an error, keeping automated installation separate from deliberate dependency updates.

An agreed lockfile controls resolved dependency choices. Platform-specific packages, runtime versions, configuration and install/build scripts can still make resulting files or complete builds differ.

How it works

In your GitHub Actions or GitLab CI pipelines, replace npm install with npm ci.

Cache npm download artifacts to reduce repeated fetching. Include the lockfile and relevant runtime/platform/configuration assumptions in the cache strategy; a cache hit does not guarantee that no network request will be needed.

Interactive Demo

Explore the concept in action using the interactive visualization below.

CI Build Pipeline
npm ci
Deterministic
Deletes node_modules Strict lockfile read
npm install
Mutable
Resolves graph May mutate lockfile

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1What does npm ci require for its dependency installation?
  2. 2What happens to an existing node_modules during a normal npm ci?
  3. 3Which cache strategy is suitable for accelerating a clean CI install?

Remember this

  • Use npm ci when an automated installation must strictly follow an existing matching lockfile.
  • npm ci reproduces locked dependency choices; complete build output can still depend on the environment and scripts.
  • npm ci deletes the node_modules folder before starting.
  • Cache npm download artifacts with a key reflecting the lockfile and relevant runtime/platform/configuration assumptions.

Done with this concept?

Mark it complete to track your progress. No login needed.