Skip to content

GitHub Actions Cache Miss — Dependencies Reinstalling Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about GitHub Actions Cache Miss. We cover key concepts, practical examples, and best practices.

Your GitHub Actions workflow downloads and installs npm/pip packages on every run — the cache restore step finds no match and your build times are 3x longer than they should be.

The Problem

# WRONG — cache key that changes every run
- uses: actions/cache@v4
  with:
    path: ~/.npm
    key: npm-cache-${{ hashFiles('package.json') }}
Cache not found for key: npm-cache-abc123def456

The cache key uses hashFiles('package.json') which changes every time any dependency version changes in package.json. If you update a dependency frequently, you never hit the cache.

Step-by-Step Fix

1. Use the lock file as the cache key

- uses: actions/cache@v4
  with:
    path: ~/.npm
    key: ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }}
    restore-keys: |
      ${{ runner.os }}-npm-

The lock file (package-lock.json, yarn.lock, pnpm-lock.yaml) only changes when the resolved dependency tree changes, not when you edit metadata. The restore-keys fallback provides a partial cache hit when the key doesn't match exactly.

2. Use actions/setup-node caching

- uses: actions/setup-node@v4
  with:
    node-version: '20'
    cache: 'npm'

setup-node has built-in caching that handles the key, path, and restore strategy automatically.

3. Cache multiple directories

- uses: actions/cache@v4
  with:
    path: |
      ~/.npm
      node_modules
      .next/cache
    key: ${{ runner.os }}-build-${{ hashFiles('**/package-lock.json') }}
    restore-keys: |
      ${{ runner.os }}-build-

4. Restore cache before installing dependencies

- name: Restore dependencies
  id: cache-deps
  uses: actions/cache@v4
  with:
    path: node_modules
    key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}

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

- name: Build
  run: npm run build

Expected output:

Cache restored from key: linux-node-abc789def012
Post job: Saving cache (skipped — cache already exists)
Build time: 45s (was 2m 30s without cache)

Prevention Tips

  • Use lock files for cache keys, not package.json
  • Use restore-keys for fallback partial matches
  • Use setup-node with cache: 'npm' for automatic caching
  • Add if: steps.cache.outputs.cache-hit != 'true' to skip install
  • Include OS in the cache key to avoid cross-platform corruption

Common Mistakes with actions cache miss

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. Forgetting deriving (Show, Eq) on custom data types needed for debugging

These mistakes appear frequently in real-world GITHUB code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does the cache miss even with a correct hash?

The cache is scoped to the branch. Main branch caches are not available on feature branches by default. Use restore-keys with a prefix (like linux-npm-) to match caches from other branches. Also check that the cache limit (10GB per repo) isn't full.

What's the difference between cache key and restore-keys?

The key is the primary lookup — if it matches exactly, you get a full cache hit. restore-keys are fallback prefixes — if no exact match, GitHub looks for the most recent cache whose key starts with the prefix. This gives partial matches when lock files change slightly.

Can I cache node_modules instead of ~/.npm?

Yes, but ~/.npm is safer across Node versions. Caching node_modules directly is faster (no npm install) but can cause issues when Node or npm versions differ between cache creation and restore. npm ci with cached ~/.npm is the recommended approach.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro