How do I cache dependencies in GitHub Actions to speed up my Node.js build process?

Question

Grade: Education Subject: Support
How do I cache dependencies in GitHub Actions to speed up my Node.js build process?
Asked by:
83 Viewed 83 Answers

Answer (83)

Best Answer
(399)
Use the `actions/cache@v3` action to cache `node_modules`. In your workflow YAML, add a step that uses `actions/cache` to restore the cache based on a key (e.g., `npm-cache-${{ hashFiles('package-lock.json') }}`). If the cache is found, it's used; otherwise, dependencies are installed, and the `node_modules` directory is saved to the cache for future runs. This significantly reduces build times.