Skip to content

Instantly share code, notes, and snippets.

@shameen
Last active June 14, 2022 15:51
Show Gist options
  • Save shameen/80d4c0cf59340912be587e4bfc04f4ce to your computer and use it in GitHub Desktop.
Save shameen/80d4c0cf59340912be587e4bfc04f4ce to your computer and use it in GitHub Desktop.
NVM Upgrade/Reinstall latest 2 LTS versions, uninstall everything else. (Doesn't touch system install)
# Please note this script has a chance to break code that depends on a specific version of Node (especially if it is a non-LTS or end-of-life version).
#
# This script will:
# 1. Reinstall and Upgrade the current LTS versions of Node to their latest versions
# 2. Uninstall ALL other nvm versions of Node (except the 'system' version)
# 3. Set the current nvm version to the latest LTS
# output current installed versions of nvm (for reference in case an old version needs installing again)
nvm ls
# Set to system version of npm so we can uninstall the rest
nvm use system
# Uninstall all nvm installed versions (without affecting 'system'):
# 1. List versions without alias + color (best for pipes)
# 2. strip whitespace + remove ' *' from the end
# 3. filter out current version + versions without a dot/period (eg. 'system')
# 4. prefix everything with 'nvm uninstall '
# 5. add 'source .../nvm.sh' as the first item in the piped data (so nvm is available for the 'sh' command)
# 6. pipe everything to `sh` to run the commands
nvm ls --no-alias --no-colors \
| sed 's/[ *]//g' \
| awk '$1~/^[^\-].*\./' \
| xargs -L1 -I{} echo 'nvm uninstall {}' \
| awk '!x {print "source $HOME/.nvm/nvm.sh";x=1}1' \
| sh -s
# Clear cache
nvm cache clear
# Install Node 14 LTS 'fermium' (Remove this after 30 April 2023)
nvm install --lts --latest-npm 14
# Install Node 16 LTS 'gallium'
nvm install --lts --latest-npm 16
# Install latest Node LTS (does nothing until Node 18 LTS 'hydrogen' is released in late 2022)
nvm install --lts --latest-npm
# Ensure we're using the latest lts, just in case install didnt set it
nvm use --lts
echo "If you were using a non-LTS or End-of-life version of Node, you will need to reinstall that version again manually with eg. `nvm install --lts 12` (or `nvm ls-remote 18` and `nvm install 18.3.0`)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment