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 --silentinto 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-lockfileGotchas
- Do not cache
node_modulesfor pnpm; the store plus the lockfile reconstructs it deterministically. - Set
versioninpnpm/action-setupso the store layout matches the pnpm that writes it.
Related guides
How to Cache npm Dependencies in GitHub ActionsCache the npm download cache in GitHub Actions with actions/cache keyed on the package-lock.json hash, plus a…
How to Cache Yarn Classic Dependencies in GitHub ActionsCache the Yarn 1.x global cache in GitHub Actions by resolving yarn cache dir and keying the cache on yarn.lo…