Node.js CI Github Action

Monorepo modifications to node.js.yaml

·

2 min read

For anyone building an app where your javascript's package.json was not at the root level of your project, you might have encountered build errors when using Github's suggested Node.js CI action. For this action to be able to successfully find the package-lock.json or yarn.lock file in a subdirectory, say client, a couple modifications need to be made.

Github provides the ability to modify the working-directory of a run command. Instead of running the command in the default top level directory, setting a working-directory will allow run to be run in that directory instead. Because working-directory will not change the directory a uses command is run in, the action used must itself support a path argument via with. When using actions/setup-node , we must pass a cache-dependency-path to our package-lock.json or yarn.lock file.

# Modified Node.js.yml:


# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

defaults:
  run:
    working-directory: client

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [12.x, 14.x, 16.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"
          cache-dependency-path: client/package-lock.json
      - run: npm ci
      - run: npm run build --if-present
      - run: npm test

These modifications got me up and running via Github actions. Hopefully it helps.

Did you find this article valuable?

Support Naomi by becoming a sponsor. Any amount is appreciated!