Created
August 18, 2026 09:55
-
-
Save nemanjam/a1f2d3f8c0c3da1472945122c68b2261 to your computer and use it in GitHub Desktop.
Install latest Node.js LTS version and remove older versions. UNTESTED
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 | |
| # Install latest Node.js LTS version and remove older versions. | |
| # UNTESTED | |
| set -e | |
| # Make sure NVM is available | |
| export NVM_DIR="${NVM_DIR:-$HOME/.nvm}" | |
| if [ ! -s "$NVM_DIR/nvm.sh" ]; then | |
| echo "Error: nvm is not installed." | |
| exit 1 | |
| fi | |
| source "$NVM_DIR/nvm.sh" | |
| echo "Installing latest Node.js LTS..." | |
| nvm install --lts | |
| # Get the installed LTS version | |
| LTS_VERSION="$(nvm version --lts)" | |
| echo "Setting Node.js $LTS_VERSION as default..." | |
| nvm alias default "$LTS_VERSION" | |
| nvm use "$LTS_VERSION" | |
| echo | |
| echo "Removing older Node.js versions..." | |
| # Get all installed versions and remove everything except the LTS | |
| nvm ls --no-colors | grep -E 'v[0-9]+\.[0-9]+\.[0-9]+' | \ | |
| sed -E 's/.*(v[0-9]+\.[0-9]+\.[0-9]+).*/\1/' | \ | |
| sort -Vu | \ | |
| while read -r version; do | |
| if [ "$version" != "$LTS_VERSION" ]; then | |
| echo "Removing Node.js $version..." | |
| nvm uninstall "$version" | |
| fi | |
| done | |
| echo | |
| echo "Done!" | |
| echo "Node.js: $(node -v)" | |
| echo "npm: $(npm -v)" | |
| echo "Default: $(nvm version default)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment