Skip to content

Instantly share code, notes, and snippets.

@pensierinmusica
Last active March 27, 2016 17:58
Show Gist options
  • Save pensierinmusica/7ae3a7529f28b358071f to your computer and use it in GitHub Desktop.
Save pensierinmusica/7ae3a7529f28b358071f to your computer and use it in GitHub Desktop.
Node & npm updater: updates Node JS and all global packages to latest
#!/bin/bash
# This script makes keeping nvm managed Node JS and its global packages up to date a breeze.
# Requires nvm, the Node version manager (https://github.com/creationix/nvm).
source ~/.nvm/nvm.sh # Change according to where your nvm executable is located
export NODE_CURRENT=$(nvm_ls_current)
export NODE_LATEST=$(nvm_remote_versions | grep '^v' | tail -1)
# Update global NodeJS to 'latest'. Migrates all installed global packages into the new Version.
echo "Updating global NodeJS installation from ${NODE_CURRENT} to ${NODE_LATEST}."
nvm install ${NODE_LATEST} --reinstall-packages-from=${NODE_CURRENT} && echo "Upgrade of global NodeJS installation complete."
# Update "default" alias and switch to it
nvm alias default ${NODE_LATEST}
nvm use default
# Update global NodeJS packages to 'latest'
for PACKAGE in $(npm --global outdated --parseable --depth=0 --loglevel silent | cut -d: -f3 | cut -f1 -d'@');
do npm --global install --loglevel silent ${PACKAGE} && echo "Updated global package '${PACKAGE}' to 'latest'.";
done
# Uncomment to delete previous NodeJS installations after update
#find ${NVM_DIR}/versions/node -type d -mindepth 1 -maxdepth 1 -not -name ${NODE_LATEST} -exec sh -c 'printf "Removed NodeJS version: "' \; -exec basename {} \; -exec rm -r {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment