N Kaushik

Fix - Github CI for Nextjs/ Nodejs/ Gatsbyjs/Reactjs Dependencies lock file is not found error

April 25, 2022

Github CI for Nextjs/ Nodejs/ Gatsbyjs/Reactjs Dependencies lock file is not found error

I faced this error with Github CI. I was using Nextjs, but this might occur in any Nodejs project, Gatsbyjs, Reactjs, Nodejs, Nestjs or any other project that uses npm.

This issue is not related to your project. This issue comes if the package-lock.json file or yarn.lock file is not found in the root of the project. In this post, I will show you how to remove this error and how to use caching in Github CI.

Issue details:

Below is the complete error:

Error: Dependencies lock file is not found in /home/runner/work/MyProject/TestProject. Supported file patterns: package-lock.json,yarn.lock

Github Action used:

I was using the below github action that throws this error:

name: Build CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js 16.x
      uses: actions/setup-node@v2
      with:
        node-version: 16.x
        cache: 'npm'
    - run: npm install
    - run: npm run lint
    - run: npm run test --if-present
    - run: npm run build --if-present

Fix 1: Remove cache:

You can simply remove the cache part while node is setting up.

Remove this line and rerun it:

cache: 'npm'

It should work.

Fix 2: Use cache-dependency-path:

If you don’t want to remove the cache, you can provide a dependency path:

cache: 'npm'
cache-dependency-path: subdir/package-lock.json

Make sure to provide the correct path of package-lock.json.

Fix 2: Using a cache action:

We can also use an alternate action for cache. This will save and restore the node_modules cache and make the process faster. You can change the CI to something as like below to add a cache action:

name: Build CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js 16.x
      uses: actions/setup-node@v2
      with:
        node-version: 16.x
    - name: Cache node modules
      uses: actions/cache@v3
      env:
        cache-name: cache-node-modules
      with:
        path: ~/.npm
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-
    - run: npm install
    - run: npm run lint
    - run: npm run test --if-present
    - run: npm run build --if-present

We added the following snippet to the CI:

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

We are using the actions/cache to cache the node modules folder.

Here,

  • key is the key used to create the cache and search for the cache on restore.
  • path is the file path to restore. We can give a relative or absolute path to the workspace directory. It can be a path to a directory or path to a single file.
  • restore-keys are optional values. These are alternative restore keys, with each key written on a new line. These are used sequentially to find and restore a cache.

Subscribe to my Newsletter