Github Actions restored cache is not being used

Viewed 1651

I'm having hard time figuring out how to make the pipeline use the restored cache of npm modules.

Here's the manifest file:

jobs:
  setup-build-push-deploy:
    name: Setup, Build, Push, and Deploy
    runs-on: ubuntu-latest
    steps:

      # Checkout the repo
      - name: Checkout
        uses: actions/checkout@v2

      - name: Setup NodeJS
        uses: actions/setup-node@v1
        with:
          node-version: '12.14'

      # Cache the npm node modules
      - name: Cache node modules
        uses: actions/cache@v1
        id: cache-node-modules
        env:
          CACHE_NAME: cache-node-modules
        with:
          # npm cache files are stored in `~/.npm` on Linux/macOS
          path: ~/.npm
          key: ${{ env.CACHE_NAME }}-${{ hashFiles('app/package-lock.json') }}
          restore-keys: |
            ${{ env.CACHE_NAME }}-

      # Install NPM dependencies
      - name: Install Dependencies
        if: steps.cache-node-modules.outputs.cache-hit != 'true'
        run: |
          cd app/
          npm install

      # Compile Typescript to Javascript
      - name: Compile Typescript
        run: |
          cd app/
          npm run make

The "cache" step does make a successful cache hit, and therefore the "install dependencies" step is skipped. But then the "compile" step fails, because it cannot find any installed npm modules.

enter image description here

The docs here and here and the SO question, eg. here, do not specify any certain step or configuration that points towards where to pick up the cached modules from. Seems like they should be picked up automatically from ~/.npm, but that's not the case.

2 Answers

I found myself in the exact same situation. The node_modules seems to be cached but then the next job immediately fails due to missing dependencies.

I ended up using this GitHub action by Gleb Bahmutov.

Specifically this part:

  - uses: bahmutov/npm-install@v1.4.5
  - run: npm install
  - run: npm run ***

Detailed description can be found in this article: https://glebbahmutov.com/blog/do-not-let-npm-cache-snowball/

I was able to solve this by changing the path to node_modules:

Here's an example:

jobs:
  prepare-npm-cache:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 12.22.0
      - name: Cache node modules
        uses: actions/cache@v2
        id: npm_cache_id
        with:
          path: node_modules
          key: ${{ runner.os }}-npm-cache-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-npm-cache-
            ${{ runner.os }}-

      - name: Install Dependencies
        if: steps.npm_cache_id.outputs.cache-hit != 'true'
        run: npm ci
Related