Skip to content

Instantly share code, notes, and snippets.

@Lego2012
Forked from snandam/nvm-install.sh
Created May 19, 2018 19:09
Show Gist options
  • Save Lego2012/17ed6cbd5a8475a96ad517378b482a0c to your computer and use it in GitHub Desktop.
Save Lego2012/17ed6cbd5a8475a96ad517378b482a0c to your computer and use it in GitHub Desktop.
Remove node, npm, nvm and install again
#!/bin/bash
# node-reinstall
# credit:
# http://stackoverflow.com/a/11178106/2083544
# https://gist.github.com/brock/5b1b70590e1171c4ab54
## program version
## Update versions accordingly. https://github.com/creationix/nvm/releases
VERSION="0.31.0"
## path prefix
PREFIX="${PREFIX:-/usr/local}"
## version control systems
USE_NAVE=0
USE_NVM=1
# use --force to bypass user confirmation
FORCE=0
usage () {
printf "%s\n" "node-reinstall"
printf "\t%s\n" "completely re-installs Node & NPM and any global node modules."
printf "\t%s\n" "It re-installs Node using NVM or Nave"
printf "\n"
printf "%s\t%s\n" "Usage:" "node-reinstall [--nave|--nvm|--nvm-latest] [-h|--help] [-v|--version] [NODE_VERSION]"
printf "\n"
printf "%s\n" "Commands:"
printf "\n"
printf "\t%s\t\t\t%s\n" "node-reinstall" "re-install node and npm using nvm"
printf "\t%s %s\t%s\n" "node-reinstall" "[-h|--help]" "show help"
printf "\t%s %s\t%s\n" "node-reinstall" "[-v|--version]" "show the node-reinstall version number"
printf "\t%s %s\t%s\n" "node-reinstall" "[-f|--force]" "installs defaults without user confirmation"
printf "\t%s %s\t\t%s\n" "node-reinstall" "--nave" "re-install using nave"
printf "\t%s %s\t\t%s\n" "node-reinstall" "--nvm" "re-install using stable nvm - the default"
printf "\t%s %s\t%s\n" "node-reinstall" "--nvm-latest" "re-install using latest nvm - creationix/nvm:master"
printf "\t%s %s\t\t%s\n" "node-reinstall" "0.12" "specify a default node version - currently ${NODE_VERSION}"
printf "\n"
}
## parse opts
{
for opt in ${@}; do
case $opt in
--help|-h)
usage
exit
;;
--version|-v)
echo ${VERSION}
exit
;;
--force|-f)
FORCE=1
;;
--nave)
USE_NAVE=1
USE_NVM=0
;;
--nvm)
USE_NVM=1
USE_NAVE=0
;;
--nvm-latest)
USE_NVM=1
USE_NAVE=0
STABLE=master
;;
*)
if [ "-" == "${opt:0:1}" ]; then
echo >&2 "error: Unknown option \`$opt'"
usage >&2
exit 1
fi
NODE_VERSION="${opt}"
esac
done
}
# get sudo
sudo -v
# check to see if npm is installed
IS_NPM_MISSING=$(which npm)
if [[ -z $IS_NPM_MISSING ]]; then
echo "Installing Node, npm."
else
echo "Completely reinstalling Node, npm."
# get list of global npm modules to reinstall
# omit the lib directory listing
GLOBAL_MODULES=`npm -g list --depth 0 --parseable | xargs basename | sed -E 's/^(lib|npm)$//g'`
if [[ -n $GLOBAL_MODULES ]]; then
echo "Will reinstall these global npm modules:"
echo $GLOBAL_MODULES
else
echo "===== ALERT! ====="
echo "The script did not find any global node modules (npm -g list)"
echo "If you are sure you installed global node modules"
echo "(by running npm install -g some-module), you might want to stop "
echo "here and locate those, because they won't be re-installed,"
echo "and since we'll be deleting all the possible install paths "
echo "that most people could use, you probably won't find them."
echo ""
echo "This can sometimes happen if you've installed global node modules"
echo "under a different node environment (for example, using nvm or nave)."
echo "It might help to run: "
echo "history | grep 'npm install' and look for either -g or --global"
echo ""
echo "If you aren't really sure, or you are sure and don't care, "
echo "you can continue; we'll re-install things the proper way, and"
echo "the next time you run this script you'll see a list of "
echo "any global node modules you've installed since now."
fi
if [[ $FORCE == 0 ]]; then
echo ""
echo "Would you like to continue running node-reinstall?"
echo ""
select yn in "Yes" "No"; do
case $yn in
Yes ) break;;
No ) exit;;
esac
done
fi
fi
# erase all possible install paths
sudo rm -rf $HOME/{local,lib,include,node*,npm,.npm*,.nvm*,.jspm*,.node-gpy*}
sudo rm -rf $PREFIX/lib/node*
sudo rm -rf $PREFIX/include/node*
sudo rm -rf $PREFIX/bin/{node,npm}
sudo rm -rf $PREFIX/share/man/man1/node.1
sudo rm -rf $PREFIX/lib/dtrace/node.d
if [[ $OSTYPE =~ "darwin" ]]; then
echo "npm, node is removed 👍"
else
echo "npm, node is removed "
fi
echo "brew uninstall nvm"
brew uninstall nvm
echo "install nvm"
# https://github.com/creationix/nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v${VERSION}/install.sh | bash
echo "nvm is installed"
echo "====================================="
echo "PLEASE READ AND FOLLOW BELOW INSTRUCTIONS"
echo "====================================="
echo "Make sure .zshrc, .bashrc and .bash_profile has the below contents"
echo "====================================="
echo ".zshrc"
echo "====================================="
echo "export NVM_DIR="/Users/nithyans/.nvm""
echo "[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm"
echo "====================================="
echo ".bash_profile"
echo "====================================="
echo "export PATH=/usr/local/bin:$PATH"
echo "export NVM_DIR="/Users/nithyans/.nvm""
echo "[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm"
echo "====================================="
echo "You must restart your terminal for changes to take effect."
echo "====================================="
echo "====================================="
echo "install node"
echo "====================================="
echo "Run - nvm install 0.12.5"
echo "Run - nvm alias default 0.12.5"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment