Skip to content

Instantly share code, notes, and snippets.

@mdesantis
Last active December 2, 2023 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdesantis/ecc71e7e9bbf011ceb9711feea2e5b88 to your computer and use it in GitHub Desktop.
Save mdesantis/ecc71e7e9bbf011ceb9711feea2e5b88 to your computer and use it in GitHub Desktop.
Get Node.js latest LTS version via CLI
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset
# Requirememnts:
# - curl
# - jq
# - cut
# - tr
nodejs_latest_lts_major=$(
# Fetch Node.js versions metadata
curl -fsSL https://nodejs.org/dist/index.json | \
# Reject items with falsey "lts" value | sort by "lts" | get the latest item's "version" value
jq --raw-output 'map(select(.lts)) | sort_by(.lts) | .[-1].version' | \
# v20.10.0 => v20
cut -d '.' -f 1 | \
# v20 => 20
tr -d 'v'
)
echo "$nodejs_latest_lts_major" # Prints 20 (as at the time of writing the latest Node.js LTS is 20.x)
# The value may be used to install the latest LTS version, e.g.:
# curl -fsSL "https://deb.nodesource.com/setup_$nodejs_latest_lts_major.x" | sudo -E bash -
# sudo apt-get install -y nodejs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment