Automate Github Actions Node Version

I use GitHub Actions for all my CI, and one annoying thing I hit early on was having to keep my GitHub Actions Node version in sync with the Node version I use for development.

To solve that issue I’ve switched to using Volta for Node version management. It’s nice because it can switch Node versions on the fly based on what’s configured in package.json, like this:

1
2
3
4
5
6
7
{
  //...package.json

  "volta": {
    "node": "18.10.0"
  }
}

Then in GitHub Actions I use these couple of steps to load my Volta version of Node:

1
2
3
4
5
# set NODE_VERSION to be Volta's current version
- run: echo NODE_VERSION=$(jq -r .volta.node package.json) >> $GITHUB_ENV
- uses: actions/setup-node@v3
  with:
    node-version: ${{ env.NODE_VERSION }}

Love it? Hate it? Have something to say? Let me know at comments@nalanj.dev.