DevOps & CI/CD

Caching npm in GitHub Actions: Why node_modules + npm ci Doesn't Help

Installing dependencies is often the slowest step in a Node.js CI job, and caching is the obvious fix. But the most commonly copied setup, caching node_modules and then running npm ci, saves almost nothing. npm ci deletes node_modules before it installs, so the restored directory is thrown away on every run.

This guide explains what to cache instead, shows the one-line setup that covers most projects, and covers the cases where caching node_modules directly is worth the extra care. It also lists what else to check when the install is no longer the bottleneck.

What npm ci Actually Does

npm ci is the right install command for CI. It installs exactly what package-lock.json specifies and fails if the lock file and package.json disagree. Its first step is to remove any existing node_modules directory, which guarantees a clean install.

The time it spends goes into two things:

  1. Downloading package tarballs from the registry.
  2. Extracting them into node_modules and running install scripts.

npm keeps downloaded tarballs in its own cache directory (~/.npm on Linux and macOS). If that cache is warm, step 1 becomes local disk reads. That directory is what you should cache.

The Simple Setup: Let setup-node Handle It

actions/setup-node has caching built in:

YAML
# .github/workflows/ci.yml
name: CI

on:
  push:
    branches: [main]
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm            # caches ~/.npm, keyed on package-lock.json

      - run: npm ci
      - run: npm test

With cache: npm, the action restores npm's cache directory before your steps run and saves it at the end of the job. The cache key includes the runner OS and a hash of package-lock.json, so it changes when your dependencies change. For pnpm or Yarn, use cache: pnpm or cache: yarn. In a monorepo with several lock files, point it at them with cache-dependency-path.

The first run after a lock file change takes about as long as an uncached run. Later runs skip the downloads. How much that saves depends on your dependency tree and network. Compare the duration of the npm ci step before and after in the job logs rather than relying on rule-of-thumb numbers.

⚠️ Common mistake: Pinning old major versions of the cache actions. actions/cache v1–v3 were retired when GitHub moved to a new cache service in 2025, and workflows still using them fail or silently stop caching. Use actions/cache@v4 and actions/setup-node@v4 or later.

Caching node_modules Directly (and When It's Worth It)

For large dependency trees, even extracting packages from a warm npm cache takes time. You can skip the install completely on an exact cache hit by caching node_modules itself, as long as you don't run npm ci when the cache hits:

YAML
      - uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Restore node_modules
        id: modules-cache
        uses: actions/cache@v4
        with:
          path: node_modules
          # Include the Node version: native modules are compiled for a specific one.
          key: modules-${{ runner.os }}-node22-${{ hashFiles('package-lock.json') }}

      - name: Install dependencies
        if: steps.modules-cache.outputs.cache-hit != 'true'
        run: npm ci

      - run: npm test

Note what's missing: there are no restore-keys. A node_modules directory restored from an older lock file is a mix of old and new packages, and running anything but npm ci on top of it can produce a build that doesn't match your lock file. Only an exact key match is safe to use without reinstalling.

This approach has downsides. The cached directory is much larger than npm's tarball cache, so saving and restoring it takes longer, and anything that changes the build environment but not the lock file (a different Node version, a different runner image, an install script that downloads a binary) must be part of the key. Use it when you've measured that the install step is still the bottleneck after enabling cache: npm.

Other Things That Slow Down Node.js CI

Once installs are cached, look at the rest of the job:

  • Run independent work in parallel: lint, type checking, unit tests and the build often don't depend on each other and can run as separate jobs, each with its own cached install.
  • Cache build tool output: tools such as Next.js (.next/cache), Nx and Turborepo keep their own caches. Restore those directories with actions/cache, keyed on the lock file plus a hash of the relevant source files.
  • Skip unnecessary downloads: packages like Playwright, Puppeteer or Cypress download browsers in their install scripts. Cache their browser directories separately, or set the package's skip-download environment variable in jobs that don't need the browser.
  • Split slow test suites: most test runners can shard a suite across a matrix of jobs (for example --shard=1/4 in Jest, Vitest and Playwright).

💡 Tip: GitHub evicts caches that haven't been used for 7 days and caps total cache size per repository. Under Actions → Caches you can see which entries exist and how large they are. Many near-duplicate node_modules entries from feature branches are a sign the key includes something that changes too often.

Summary

  • npm ci deletes node_modules first, so caching node_modules and then running npm ci gives no benefit.
  • Cache npm's download cache instead. actions/setup-node with cache: npm does this in one line.
  • Cache node_modules only with an exact-match key (lock file hash plus Node version), no restore-keys, and skip npm ci on a hit.
  • Keep cache actions on current major versions, then parallelize jobs and cache build tool output for further gains.
Advertisement