CodexBloom - Programming Q&A Platform

How to implement GitHub Actions workflow scenarios on 'Cache hit' but not using cached dependencies?

👀 Views: 0 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
github-actions node.js caching yaml

After trying multiple solutions online, I still can't figure this out. Quick question that's been bugging me - I have a GitHub Actions workflow that builds my Node.js app using the `actions/setup-node` action. It's supposed to cache `node_modules` to speed up the build process, but I've recently noticed that it always says `Cache hit` but the build time hasn't improved at all. The workflow looks like this: ```yaml name: CI on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Node.js uses: actions/setup-node@v2 with: node-version: '14' cache: 'npm' - name: Install dependencies run: npm install - name: Run tests run: npm test ``` I've verified that the cache key is correct and even tried changing it, but it still behaves the same way. The `npm install` command seems to be downloading all the dependencies from scratch every time. I checked the logs and saw this snippet: ``` Cache Hit: true Cache Key: npm-<unique-key> ``` At this point, I'm confused because it claims the cache is being hit, yet my build process takes as long as it did before I implemented caching. I've tried clearing the cache from the GitHub Actions UI and re-running the workflow, but the scenario continues. I am using Node.js version 14.x and my `package-lock.json` is committed to the repo. What can I do to ensure that caching is working correctly in this setup? What's the best practice here? My development environment is macOS. What am I doing wrong?