This guide explores how GitHub Actions invalidates and rebuilds the NPM cache when project dependencies change.
In this guide, we’ll explore how GitHub Actions invalidates and rebuilds the NPM cache when your project’s dependencies change. You’ll learn how cache keys work, how to trigger a cache rebuild, and how to verify the new cache in your repository.
GitHub Actions uses cache keys to store and restore dependencies. By hashing package-lock.json, we ensure that any update to dependencies generates a new cache key, invalidating the old cache automatically.Key points:
We cache node_modules using the hash of package-lock.json.
Changing dependencies updates the lock file, resulting in a new cache key.
A new cache is created when the key doesn’t match any existing cache.
Cache key including OS and hash of package-lock.json
uses
Action version (actions/cache@v3)
When you push a change to package-lock.json, the hashFiles function produces a new key, and the workflow cannot find an existing cache that matches it.