Configuring Node.js and Yarn Versions in GitHub Workflows: A Step-by-Step Guide

Ensure Consistent and Reliable Workflow Execution with Proper Node.js and Yarn Version Configuration

Configuring Node.js and Yarn Versions in GitHub Workflows: A Step-by-Step Guide

Introduction

Configuring Node.js and Yarn versions in a GitHub workflow is essential for maintaining consistent and reliable execution across various environments. By explicitly specifying the versions of Node.js and Yarn in your workflow, you guarantee that the same versions of these tools are utilized regardless of the runner environment.

This practice becomes particularly valuable when sharing workflows with others or ensuring consistent execution in the future. In this article, we will explore how to configure Node.js and Yarn versions in your GitHub workflow, providing step-by-step guidance and code examples.

Prerequisites

Before proceeding, make sure you have a basic understanding of GitHub workflows. If you need further information, refer to the official GitHub workflows documentation.

Additionally, familiarity with Node.js and Yarn package managers is assumed.

Let's Dive In!

1. Configuring Node.js Version for GitHub Workflow

To configure the Node.js version in your GitHub workflow, we will leverage the actions/setup-node@v2 action. This action not only facilitates Node.js version configuration but also offers additional functionalities such as:

  • Downloading and caching the requested Node.js version, adding it to the PATH

  • Optional caching of npm/yarn/pnpm dependencies

  • Registering problem matchers for error output

  • Configuring authentication for GPR or npm

Refer to the documentation for actions/setup-node@v2 to explore its latest versions and features in detail.

Here's an example code snippet demonstrating how to configure the desired Node.js version in your workflow:

name: CI Pipeline

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        # node-version: 14.15.1  # Exact version.
        # node-version: 14.x     # Latest patch version within 14.x
        # node-version: 14       # Major version.
        # node-version: latest   # Latest version.
        with:
          node-version: <node-version>

2. Configuring Yarn Version for GitHub Workflow

To configure the Yarn version in your workflow, we will utilize the Corepack package. Corepack serves as an intermediary between you and Yarn, enabling the use of different package manager versions across multiple projects without the need to check-in the Yarn binary.

While Corepack comes preinstalled with Node.js versions >=16.10.0, it currently requires explicit activation. For Node.js versions <16.10.0, Corepack must be installed explicitly before enabling it.

Execute the following command to install Corepack globally:

npm i -g corepack

Here's an example code snippet showcasing how to configure the desired Yarn version in your workflow:

name: CI Pipeline

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      ...
      ...
      - name: Install Yarn
        run: |
          sudo npm i -g corepack  # For Node.js versions <16.10.0
          corepack enable  # Enable Corepack
          corepack prepare yarn@<version> --activate  # Add yarn@stable to install the latest stable version of Yarn

Conclusion

In addition to the benefits mentioned above, specifying the Node.js and Yarn versions in your GitHub workflow ensures compatibility with your project's dependencies. By employing the same tool versions used during development and testing, you minimize potential compatibility issues and ensure a smooth workflow execution.

If you have any questions or require further assistance, feel free to contact me or leave a comment below.

I hope you found this article valuable. Happy learning and thank you for reading!