Last active
December 2, 2023 15:58
-
-
Save mdesantis/ecc71e7e9bbf011ceb9711feea2e5b88 to your computer and use it in GitHub Desktop.
Get Node.js latest LTS version via CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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