Skip to content
Latchkey

How to Cache the pnpm Store in GitHub Actions

Resolve the pnpm store directory with pnpm store path, cache it, and key the entry on pnpm-lock.yaml.

pnpm keeps a single content-addressable store and hard-links packages into each project. Cache that store (not node_modules) so installs become near-instant link operations. Resolve the path at runtime because it differs per OS.

Steps

  • Install pnpm with pnpm/action-setup (or Corepack).
  • Resolve the store path with pnpm store path --silent into a step output.
  • Cache that path keyed on hashFiles('**/pnpm-lock.yaml').
  • Run pnpm install --frozen-lockfile.

Workflow

.github/workflows/ci.yml
steps:
  - uses: actions/checkout@v4
  - uses: pnpm/action-setup@v4
    with:
      version: 9
  - id: pnpm
    run: echo "dir=$(pnpm store path --silent)" >> "$GITHUB_OUTPUT"
  - uses: actions/cache@v4
    with:
      path: ${{ steps.pnpm.outputs.dir }}
      key: pnpm-${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
      restore-keys: |
        pnpm-${{ runner.os }}-
  - run: pnpm install --frozen-lockfile

Gotchas

  • Do not cache node_modules for pnpm; the store plus the lockfile reconstructs it deterministically.
  • Set version in pnpm/action-setup so the store layout matches the pnpm that writes it.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →