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

Subject: Package Managers | Lesson 22 of 24 | 5 min read
Interactive lesson: https://codewithanimation.in/package-managers/npm-ci-and-ci-optimization
Previous: [Security Audits & Supply Chain Attacks](https://codewithanimation.in/package-managers/npm-audit-and-supply-chain-security.md) | Next: [Essential npm CLI Commands](https://codewithanimation.in/package-managers/npm-cli-toolkit.md)

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

## Common mistake: The Phantom Bug

When a local build differs from CI, compare the manifest/lock, runtime, platform, configuration and script environment. Do not assume npm install must have ignored a compatible existing lockfile.

## Tip: Don't Cache node_modules

Caching download artifacts is well suited to npm ci because ci recreates node_modules. Reusing installed directories requires careful runtime/platform compatibility checks, especially for native binaries.

## Try it: Interactive Demo

Explore the concept in action using the interactive visualization below.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/npm-ci-and-ci-optimization

## Check yourself

### Question 1

What does npm ci require for its dependency installation?

- A. Only a node_modules folder, with no manifest
- B. An unpublished package name on the public registry
- C. A manually deleted lockfile before every run
- D. An existing lockfile consistent with the package manifest

Correct answer: D

ci installs a project from its lock and reports disagreement rather than resolving a replacement lock. It does not add individual dependencies.

- Why not A: Installed folders do not replace the required project metadata.
- Why not B: Publication status is not the defining ci requirement.
- Why not C: ci requires a lockfile instead of generating one from scratch.

### Question 2

What happens to an existing node_modules during a normal npm ci?

- A. It is removed before the clean project installation
- B. It is blindly trusted and never changed
- C. It is uploaded as a new public npm package
- D. Only globally installed packages are removed

Correct answer: A

The clean install recreates the installation from the agreed manifest/lock. ci does not rewrite those files to fix a mismatch.

- Why not B: ci performs a clean installation rather than trusting the old tree.
- Why not C: ci does not publish installed directories.
- Why not D: The operation concerns this project's installation.

### Question 3

Which cache strategy is suitable for accelerating a clean CI install?

- A. Use one unversioned node_modules cache across every operating system
- B. Cache npm download artifacts with a key reflecting the lockfile and relevant environment
- C. Delete the lockfile to force the cache to match
- D. Cache production credentials inside package artifacts

Correct answer: B

A download cache can reduce repeated fetching while npm ci recreates node_modules. Runtime, platform and configuration assumptions still need consideration.

- Why not A: Native packages and environment differences can make that unsafe.
- Why not C: Deleting the lock defeats the controlled installation workflow.
- Why not D: Credentials should not be stored in a reusable dependency cache.

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