How to Use the Built-in cache Input of setup-node
setup-node caches the package manager store for you when you set cache: npm, yarn, or pnpm.
Instead of a separate actions/cache step, set cache: on actions/setup-node. It locates the lockfile, picks the right store path, and keys the cache on the lockfile hash automatically.
Steps
- Add
cache: npm(oryarn,pnpm) to the setup-nodewithblock. - Point
cache-dependency-pathat the lockfile in a monorepo subdir if needed. - Run your normal clean install after the setup step.
Workflow
.github/workflows/ci.yml
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
cache-dependency-path: apps/web/package-lock.json
- run: npm ciGotchas
- For pnpm, run
pnpm/action-setupbefore setup-node so the binary exists. - The built-in cache keys only on the lockfile; use raw actions/cache for custom paths.
Related guides
How to Choose Between Caching node_modules and the Package Manager StoreDecide whether to cache node_modules or the npm, yarn, or pnpm download store in GitHub Actions, and why cach…
How to Use the cache-hit Output to Skip Work in GitHub ActionsRead the cache-hit output of actions/cache in GitHub Actions to conditionally skip a rebuild or reinstall ste…