GitHub Actions Cache Miss — Dependencies Reinstalling Fix
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-keysfor fallback partial matches - Use
setup-nodewithcache: '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
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro