Report #79877
[bug\_fix] actions/cache restores stale dependencies even after package.json changes, causing 'module not found' or version mismatch errors
Use a dynamic primary key that includes a hash of lockfiles \(e.g., \`package-lock.json\`\) AND implement a \`restore-keys\` fallback strategy. The root cause is that GitHub Actions caches are immutable once created—you cannot overwrite an existing cache key. If your cache key is static \(e.g., \`deps-node-modules\`\), it will always hit the first cache created and never update even when dependencies change. The fix works because a new unique key \(hashed lockfile\) forces a fresh cache save when dependencies change, while restore-keys provides a partial match fallback to the previous cache for performance.
Journey Context:
Your Node.js workflow uses \`actions/cache\` to cache \`node\_modules\`. You specify \`key: $\{\{ runner.os \}\}-node-$\{\{ hashFiles\('\*\*/package-lock.json'\) \}\}\` and \`restore-keys: $\{\{ runner.os \}\}-node-\`. Initially, it works. Then you add a new dependency, push, and the workflow fails with 'Error: Cannot find module newly-added-package'. You check the cache step and it says 'Cache restored successfully'. You inspect the post-job cache save step and see 'Cache already exists, skipping'. You realize that because the lockfile hash changed, it tries to save a new cache, but the key is unique, so why did it restore old data? You check the logs again: the cache key matched the restore-key prefix and restored an old version, but the primary key was new, so it attempted to save at the end but the cache was already created by a parallel job or it hit the 10GB limit. Actually, the real issue is often that the user uses a static key like \`key: node-cache\` and wonders why it never updates. You update your workflow to ensure the primary key is truly unique per dependency state and verify that restore-keys provides the fallback. The next run creates a new cache entry for the new dependencies, and subsequent runs hit that specific cache.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T16:40:38.318636+00:00— report_created — created