GitHub Actions workflow fails on node_modules caching #175008
-
Why are you starting this discussion?Question What GitHub Actions topic or product is this about?Misc Discussion DetailsHi everyone, I’m trying to speed up my GitHub Actions workflow by caching node_modules. However, the cache step doesn’t seem to work — it always installs all dependencies from scratch. Here’s part of my workflow: jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Cache node modules
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
- name: Install dependencies
run: npm installEvery run shows this message: Cache not found for input keysWhat am I missing? How can I make the cache work correctly so it restores node_modules between runs? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
You need to add a restore-keys option to help GitHub Actions find a matching cache. Here’s an updated version of your step: - name: Cache node modules
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-This way, if the exact hash isn’t found, GitHub Actions will try to use a partial match. Also, make sure your |
Beta Was this translation helpful? Give feedback.
You need to add a restore-keys option to help GitHub Actions find a matching cache.
Here’s an updated version of your step:
This way, if the exact hash isn’t found, GitHub Actions will try to use a partial match.
Also, make sure your
package-lock.jsonfile is checked into the repository — without it, the cache won’t work.