cache: [npm] cache restored but node_modules missing

Hi I’d like to lint my react-based codebase with github actions and use this library to optimize the job duration. My yaml file for workflow configure is here:

name: ESLint
on: [push]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v2

      - name: setup node
        uses: actions/setup-node@v1
        with:
          node-version: "12"

      - name: cache node modules
        id: cache-node_modules
        uses: actions/cache@v1
        env:
          cache-name: cache-node-modules
        with:
          path: ~/.npm
          key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-${{ env.cache-name }}-

      - name: install dependencies
        if: steps.cache-node_modules.outputs.cache-hit != 'true'
        run: npm ci

      - name: run eslint
        run: npm run lint

At first the cache doesn’t hit, npm ci is triggered, this job works and the generated cache is posted to ~/.npm dir.

Post job cleanup. /bin/tar -cz -f /home/runner/work/_temp/885b6838-636b-49ea-a563-41734fd8977d/cache.tgz -C /home/runner/.npm . Cache saved successfully

Next time I run this job, github actions restore cache successfully, as I intended.

Run actions/cache@v1 with: path: ~/.npm key: Linux-cache-node-modules-1001ef79e1a0df9c0bf9013e62376251ae8ec7cb518016d4da61abfc8087539a restore-keys: Linux-cache-node-modules- env: cache-name: cache-node-modules Cache Size: ~46 MB (48000811 B) /bin/tar -xz -f /home/runner/work/_temp/b378e123-f68d-4e18-87d6-7942df1629b7/cache.tgz -C /home/runner/.npm Cache restored from key: Linux-cache-node-modules-1001ef79e1a0df9c0bf9013e62376251ae8ec7cb518016d4da61abfc8087539a

But the step of npm run lint somehow fails.

> Jinrikisha-client@0.1.0 lint /home/runner/work/Jinrikisha-client/Jinrikisha-client > eslint src/**/*.{ts,tsx} sh: 1: eslint: not found …(some npm ERRs) npm WARN Local package.json exists, but node_modules missing, did you mean to install? npm ERR! A complete log of this run can be found in: npm ERR! /home/runner/.npm/_logs/2020-05-10T05_25_36_742Z-debug.log ##[error]Process completed with exit code 1.

I used another cache by changing the key to ${{ runner.os }}-${{ env.cache-name }}-v2-${{ hashFiles('**/package-lock.json') }} and the restore key to ${{ runner.os }}-${{ env.cache-name }}-v2- but the result ends up in the same way.

This may be related to https://github.com/actions/cache/issues/281

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

Going to close this issue out.

To summarize: Using restore-keys means you’re not guaranteed to have an exact cache hit and some dependencies could be missing. Using the cache-hit output variable can work for some scenarios, but depending on your ecosystem and the contents of your cache, you may want to run the install step to ensure consistency.

This specific issue was from caching the npm cache, which won’t populate any node_modules.

Thanks all for your feedback and suggestions 😄

Well, you should definitely avoid caching node_modules.

using yarn install --frozen-lockfile has decreased the install time from ~5mins to ~2mins on a macos-latest machine for me

Example:

- name: "Get yarn cache"
          uses: actions/cache@v1
          id: yarn-cache 
          with:
            path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
            key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
            restore-keys: |
              ${{ runner.os }}-yarn-

        - name: Install Dependencies
          run: yarn install --frozen-lockfile

Yes, that’s true for yarn as well. Also, where is the warranty that GitLab is correct?

Additionally, using restore-keys means you won’t always get an exact match of your dependencies, so some will be missing.

I am experiencing the same issue when trying to use yarn workspaces. Dependencies are missing when using the yarn cache.