CodexBloom - Programming Q&A Platform

GitHub Actions not caching Node modules between workflow runs despite using actions/cache

๐Ÿ‘€ Views: 39 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-25
github-actions nodejs caching YAML

I'm integrating two systems and I've looked through the documentation and I'm still confused about I'm having trouble getting caching to work with my GitHub Actions workflow for a Node.js project... Iโ€™ve set up the cache action to store my `node_modules`, but every time the workflow runs, it seems to be reinstalling all the dependencies instead of using the cached version. Hereโ€™s a snippet of my workflow YAML file: ```yaml name: CI on: push: branches: - main pull_request: branches: - main jobs: build: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Cache Node modules uses: actions/cache@v2 with: path: node_modules key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} restore-keys: | ${{ runner.os }}-node- - name: Install Dependencies run: | npm install - name: Run Tests run: | npm test ``` Iโ€™m using Node.js version 14.x and npm version 6.14.8. The question is that the cache isn't being restored, and I see logs like `Cache not found for input keys: ubuntu-latest-node-[hash]`. Iโ€™ve double-checked the `package-lock.json` file, and it is being tracked in Git. The cache key should be unique to the state of my dependencies, yet it seems like it's always missing. Iโ€™ve also tried simplifying the cache key to just `key: ${{ runner.os }}-node` but that didn't help either. Any ideas on what might be going wrong? Am I missing any configuration or using the cache action incorrectly? This is happening in both development and production on CentOS. Any examples would be super helpful. For context: I'm using Yaml on Ubuntu 20.04. Any advice would be much appreciated.