GitHub Actions allow you to test Node.js projects. You can define a specific Node.js version you want to run your tests against. You may also provide a list of Node.js versions allowing you to run your tests against them.
This tutorial shows you how to run your GitHub Actions with the latest
Node.js release using the actions/setup-node
package.
GitHub Actions Series Overview
- Getting Started With Node.js
- Create a Testing Matrix
- Using MongoDB
- Using Redis
- Using MongoDB Replica Sets
- Run on Pull Request
- Customize the Job Name
- Run a Workflow When Creating a Tag
- Testing Against an Exact Node.js Version
- Trigger Builds on Schedule (Cron)
- How to Test Lerna Monorepos
- How to Add Comments in YAML Files
- Clone Another Repository
- Run on Push or Pull Request, but Not Both
- Limit Concurrency and Cancel In-Progress Jobs
- Test Against the Latest Node.js Version
Use Latest Node.js Version in GitHub Actions
The actions/setup-node package allows you to define the desired Node.js versions in different formats.
You may use
- a major version identifier: 18
or 21
- a specific version: 18.10
or 20.9.0
- LTS syntax: lts
or lts/hydrogen
(stands for Node.js v18)
- the latest release: *
or latest
/current
/node
You should pick any of the version identifiers from the „latest“ section. We like to use the latest
version because it’s the most understandable identifier that you can map to the version that will run in your action’s environment. And when you’re coming back to a project after some time, you still know what latest
stands for.
Here’s a sample workflow configuration on how to use the node-version: latest
:
name: Run tests
on: push
jobs:
test:
runs-on: ubuntu-latest
name: Run project tests
steps:
- name: Setup Node.js ("latest" Version)
uses: actions/setup-node@v4
with:
node-version: latest # <-- use "latest" for the most recent Node.js version
- name: Other steps …
Latest Node.js Version in a Testing Matrix
You may also specify the latest
Node.js version in your testing matrix. Add it as a version in your list of Node.js versions:
name: Run tests
on: push
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x, 20.x, latest] # <-- add "latest" for the most recent Node.js version
name: Node ${{ matrix.node-version }}
steps:
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Other steps …
Why Testing Against the Latest Node.js Version?
To fail early on new Node.js versions.
At the time of writing this tutorial, the „current“ and „latest“ version of Node.js is v21. At some point, the Node.js project transitions the v21 release line to the stable v22 versioning. At that point, v21 sunsets, and the release line is stopped.
If you’re testing against the latest
release, you’re always testing against the newest major version. It’s a dynamic, rolling release. Keep this in mind if you don’t want to spend much time on maintenance because your code might fail for newer Node.js releases. There’s the chance you feel bad for failing tests because your code is outdated.
Should You Worry About the Testing Overhead?
It depends. If you’re paying for GitHub Actions, you should decide whether the extra seconds or minutes are worth it. The time that GitHub Action runners spend on the „latest“ Node.js version may add up to over a month. In such cases, you may want to test the most recent Node.js version only on specific branches, like main
.
For open-source packages, you don’t need to worry about the overhead. You’re not paying for the time spent on GitHub Action runners. You may enable the latest version ensuring that your code is compatible with the newest version.
Enjoy testing Node.js in GitHub Actions!
Mentioned Resources
- GitHub Actions:
actions/setup-node
repository - GitHub Actions: docs for
actions/setup-node
on Node.js version definitions in different formats