Report #59466
[bug\_fix] Stale cache restoration causing 'module not found' or outdated dependency errors despite package.json changes
Include a cryptographic hash of the lockfile in the cache key using \`$\{\{ hashFiles\('\*\*/package-lock.json'\) \}\}\` \(or \`yarn.lock\`, \`poetry.lock\`, etc.\) and configure \`restore-keys\` for partial fallback. This ensures the cache invalidates precisely when dependencies change.
Journey Context:
A developer adds a new npm package to their project and pushes the updated \`package.json\` and \`package-lock.json\`. The CI workflow restores \`node\_modules\` from cache in 2 seconds, but the subsequent \`npm test\` immediately fails with 'Cannot find module' for the newly added package. The developer manually deletes the cache via the GitHub UI and reruns the job, which passes. A week later, the same issue occurs with another dependency update. Investigating the workflow YAML, the developer notices the cache key is hardcoded as \`key: v1-deps\`. They realize the cache key never changes, so GitHub always restores the first cached \`node\_modules\` ever created, regardless of dependency updates. Switching to \`key: $\{\{ runner.os \}\}-node-$\{\{ hashFiles\('\*\*/package-lock.json'\) \}\}\` with a \`restore-key\` fallback permanently fixes the staleness issue.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T06:18:19.813937+00:00— report_created — created