GitHub Actions Cache: Test the Current Lockfile

Configure a GitHub Actions cache that keeps CI on the current lockfile. Tell exact hits from fallback restores and run the right install before tests.

A cache decision chart shows an exact hit at version 1.0.0, a fallback with a skipped install still at 1.0.0, and the corrected install at 2.0.0.

A GitHub Actions cache can make a job faster while leaving it on old dependencies. Cache npm's package downloads and run npm ci before testing the current lockfile. If your workflow restores node_modules and skips installation, skip only after a compatible exact hit. A fallback can restore files while returning cache-hit as the string false.

We modeled the difference with two local npm lockfiles. The same test imported version 1.0.0 after an install step was skipped, even though the lockfile called for 2.0.0. With the gate changed to run on every non-exact restore, the import returned 2.0.0. The test ran offline. The GitHub cache outputs were fixture inputs.

What does a GitHub Actions cache hit mean?

The actions/cache action looks for an eligible cache using your requested key. It can also restore a fallback based on matching rules and your restore-keys list. In the normal restore path, an exact match sets the output to the string true. A fallback can restore files and set it to false. With no matching entry, the current action usually leaves the output unset, which reads as an empty string in an expression.

Lookup result Files restored in normal restore mode cache-hit If caching installed dependencies
Exact Yes true Skip installation only if the key covers every compatibility requirement.
Partial or fallback Yes false Install from the current lockfile.
No eligible entry No Empty Install from the current lockfile.

The cache action output reference describes the empty result for an ordinary miss. The restore implementation shows where the output is unset. Disabled caching, lookup-only mode and a configured failure on a miss can produce other behavior. In lookup-only mode, an exact key can be found without downloading files.

Cache matching also depends on more than the string key. GitHub's dependency caching reference covers branch scope and cache version, which reflects details such as paths and compression. Before changing permissions, confirm which branch or pull request produced the run and whether the cache is eligible for that run. Keep the job within its intended GitHub Actions permissions.

Cache downloads and install before tests

For npm, start with actions/setup-node and cache npm downloads. The setup action does not restore node_modules through its standard npm cache. Install dependencies before tests. The setup-node reference describes the cache behavior, and the npm ci reference explains how the command installs the lockfile in a clean CI setup.

This workflow fragment assumes a root package-lock.json. Use the approved versions or commit pins for your repository.

steps:
  - uses: actions/checkout@v6
  - uses: actions/setup-node@v6
    with:
      node-version: '22'
      cache: npm
      cache-dependency-path: package-lock.json
  - name: Install the locked dependencies
    run: npm ci
  - name: Run tests
    run: npm test

For a package in a subdirectory, set the actual lockfile path for the cache. Run the installation and tests from the directory that owns it. In a workspace with a shared lockfile, use the workspace root if that is where the package manager expects the file. The cache setup and install should agree on what defines dependency state.

Use installation flags your project already requires. If your lockfile depends on an npm flag such as legacy-peer-deps, align the CI configuration with the configuration used to produce it. Keep the intended Node version under review. Then a warm download cache can save downloading time while npm ci recreates the installed tree from the committed manifest and lockfile.

Fix the install gate for a cached dependency tree

If the workflow caches installed dependencies, inspect the conditions on the install step. A condition that checks only for an empty cache result runs on a total miss and skips on a partial restore. The log can correctly say it restored a cache while the dependency tree represents a previous lockfile. An exact cache hit normally looks like the string true, which is different from the restored path existing on disk.

The example below shows a repair for an existing workflow caching node_modules. It assumes you set the Node runtime separately and uses a namespace for Linux or another runner OS, the architecture and the Node version. Adjust the namespace to your compatibility rules. For a new npm workflow, start with the downloads-only setup above.

- name: Restore installed dependencies
  id: deps
  uses: actions/cache@v6
  with:
    path: node_modules
    key: deps-v1-${{ runner.os }}-${{ runner.arch }}-node22-${{ hashFiles('package-lock.json') }}
    restore-keys: |
      deps-v1-${{ runner.os }}-${{ runner.arch }}-node22-
- name: Install this lockfile after any non-exact restore
  if: steps.deps.outputs.cache-hit != 'true'
  run: npm ci

Use the string 'true' deliberately. The condition reads as “install unless the output records an exact hit.” It handles both a fallback and a total miss. If the job can share a cache across runtime or architecture combinations, reflect that in the key before skipping install. A major version alone may be insufficient for native modules or builds sensitive to a specific runtime, toolchain or system library. A change to configuration that affects installation may require its own fingerprint.

Make the key dependable before optimizing the gate. The checkout must happen before the lockfile is hashed. In a pull request where an agent moved a package, verify that the expression still points to the right file. If hashFiles has no match, it can return an empty value. That can quietly leave you with the same key across multiple dependency states. Use an explicit lockfile path and check it exists. The GitHub Actions expressions reference explains how hashFiles handles workspace paths and missing files.

If you are investigating a current job, read the restored key and the scheduled step result together. The requested key records what you hoped to get. The restored key records what was found, when one was restored. The step result tells you whether the package manager actually reconciled that state with the current lockfile. The tests can supply another cross-check by showing the version they resolved.

What did the local cache test show?

Using Node v22.22.1 and npm 9.2.0, we installed a local tarball with version 1.0.0 and copied its installed tree. Then we generated a second lockfile for a 2.0.0 tarball. Each case imported the package with Node. The fixture used actual npm ci and a local module. GitHub cache outputs were modeled using the documented action behavior.

Case Modeled output Installed before decision Install ran Test loaded
Matching 1.0.0 lockfile and tree true 1.0.0 No 1.0.0
2.0.0 lockfile with fallback and empty-output-only gate false 1.0.0 No 1.0.0
2.0.0 lockfile with fallback and corrected gate false 1.0.0 Yes 2.0.0
2.0.0 lockfile on complete miss Empty None Yes 2.0.0
Downloads-only policy with an exact-hit input true None Yes 2.0.0

Both fallback cases started with the same stale tree. Only the install decision changed. The old module could load and return a value, so a check that only required the import could pass. A useful negative test checks which version the test job actually resolved. That is the same distinction we make in the guide to testing AI-generated changes. The last row illustrates the install policy on a clean local tree. It does not benchmark a warmed download cache. No hosted runner, GitHub cache backend or external npm registry was involved.

Review a dependency-changing pull request

In a matrix workflow, two jobs can read the same lockfile and still need different installed trees. Give them distinct cache namespaces when their runtime, operating system or architecture differs. If the job runs from a subdirectory, confirm its test command uses the same dependency root as the install. That prevents a successful check in one workspace from standing in for an untested change in another.

  1. Locate the path the cache restores. Ask whether it holds downloads, installed dependencies or reusable build artifacts. Changing that path can change what a successful cache step tells you.
  2. Check what determines the key. Compare the lockfile path, install options, runtime and architecture with the job configuration. Confirm checkout precedes hashing and that the lockfile is visible before the cache step runs.
  3. Read the outcomes for exact hits, fallback restores and misses. If the job restores downloads, install from the lockfile on each run. For an installed tree, reconcile any non-exact result with the checked-out lockfile.
  4. Compare a package version the tests resolve with the branch's lockfile after a dependency change. In a monorepo, check from the same workspace as the test.
  5. Record the checked commit, restore type, whether installation ran and what the test resolved. That gives the next reviewer a repeatable check, especially when a merge queue runs its own validation.

Your repository may have another caching layer for task outputs. It might also respond to repeated misses by warming the default branch cache. Those decisions can make CI faster. Keep the test evidence focused on the dependency version in the pull request. For the next agent's lockfile change, hand the reviewer the resolved version alongside the requested key and the install result.

Frequently asked questions

What does cache-hit mean in GitHub Actions?

For a normal actions/cache restore, true means the requested key matched exactly. A fallback can restore files while reporting false. If no eligible cache entry exists, the current action leaves the result empty in the usual miss path. Check the restored key and the contents expected at that path as well.

Should I run npm ci when the npm cache hits?

Yes when the cache holds npm's downloads, such as the standard npm cache provided by actions/setup-node. Restoring that data can help the package manager obtain packages, but it does not install them. Run npm ci from the directory that owns the committed lockfile, then run the tests.

Should I cache node_modules in GitHub Actions?

Prefer the package manager's download cache for a typical npm job, and install from the lockfile each time. If you restore an installed tree, it must match the lockfile and all important runtime and installation requirements. Reconcile any fallback or miss with npm ci before running tests.

What should a GitHub Actions cache key include?

Start with a namespace for the kind of cache and the relevant operating system, architecture and runtime. Include the hash of the lockfile your install actually uses. For installed outputs, account for any other toolchain or package-manager configuration that can make an otherwise matching cache incompatible.

How can I tell if a test ran with old dependencies?

From the same directory and runtime as the failing or relevant test, inspect the actual package version that its module resolution loads. Compare it with the committed lockfile and check the job logs for the restored key and result of the install step. Repeat after a lockfile change.