Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Complete Node Reinstall. I've moved this to a repo at http://git.io/node-reinstall
#!/bin/bash
# node-reinstall
# credit: http://stackoverflow.com/a/11178106/2083544
## program version
VERSION="0.0.13"
## path prefix
PREFIX="${PREFIX:-/usr/local}"
## version control systems
USE_NAVE=0
USE_NVM=1
# use --force to bypass user confirmation
FORCE=0
## default node version
NODE_VERSION="0.10"
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
if (( $USE_NVM )); then
# NVM will think it is still installed if NVM_DIR is still set
unset NVM_DIR
sudo rm -rf $HOME/.nvm
elif (( $USE_NAVE )); then
sudo rm -rf $HOME/.nave
else
echo >&2 "error: Unsupported version control system"
exit 1
fi
# erase all possible install paths
sudo rm -rf $HOME/{local,lib,include,node*,npm,.npm*}
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 (( $USE_NVM )); then
# go home and install NVM just because I feel safe there
cd $HOME
# get the latest stable version number of nvm from the repo's homepage
[ "$STABLE" == "" ] && STABLE=$(curl -s -k https://github.com/creationix/nvm/ | grep "curl https://raw.githubusercontent.com/creationix/nvm/" | grep -oE "v\d+\.\d+\.\d+")
[[ $STABLE =~ ^v[0-9]+.[0-9]+.[0-9]+$ ]] || STABLE="v0.25.1"
curl -sL https://raw.githubusercontent.com/creationix/nvm/$STABLE/install.sh | bash
source $HOME/.nvm/nvm.sh
elif (( $USE_NAVE )); then
curl -sL https://raw.githubusercontent.com/isaacs/nave/master/nave.sh -o $PREFIX/bin/nave
fi
if (( $USE_NVM )); then
# install the latest 0.10 version of node then set it as the default
nvm install $NODE_VERSION
nvm alias default $NODE_VERSION
elif (( $USE_NAVE )); then
nave usemain $NODE_VERSION
fi
if [[ ! -z ${GLOBAL_MODULES// } ]]; then
echo "Reinstalling your global npm modules:"
echo $GLOBAL_MODULES
if [[ $FORCE == 0 ]]; then
echo "Continue?"
select yn in "Yes" "No"; do
case $yn in
Yes ) npm install --global $GLOBAL_MODULES; break;;
No ) exit;;
esac
done
else
npm install --global $GLOBAL_MODULES
fi
fi
if [[ $OSTYPE =~ "darwin" ]]; then
echo "node-reinstall is done. 👍"
else
echo "node-reinstall is done."
fi
echo "You must restart your terminal for changes to take effect."
@ccsouthard

This comment has been minimized.

Copy link

ccsouthard commented Dec 20, 2014

Where can I find the SO article? & I'm not a shell guy; Do I cut and paste each line separately?

@dhruska

This comment has been minimized.

Copy link

dhruska commented Jan 13, 2015

I believe he's referring to this article.

edit: Yep, it's in a comment up top in the script. To execute, save the file to your desktop, and then type "bash ~/Desktop/nodereinstall.sh"

@BrianJLennon

This comment has been minimized.

Copy link

BrianJLennon commented Feb 11, 2015

This is insanely good!!! It needs it's own webpage! NodeJS/NPM need to endorse it!

Spent so long trying to get node working properly and this nailed it!

You guys deserve a medal!

Re: edit: Yep, it's in a comment up top in the script. To execute, save the file to your desktop, and then type "bash ~/Desktop/nodereinstall.sh" - cant see it in the comment but it needs to be in there!!!

@twome

This comment has been minimized.

Copy link

twome commented Feb 24, 2015

Very appreciative of this

@cwahlfeldt

This comment has been minimized.

Copy link

cwahlfeldt commented Feb 25, 2015

Thanks for this! 😄

@vetri02

This comment has been minimized.

Copy link

vetri02 commented Feb 26, 2015

For the latest version, it should be

nvm install 0.12
nvm alias default 0.12

right?

@brock

This comment has been minimized.

Copy link
Owner Author

brock commented Mar 5, 2015

Sorry to all who have commented here. I submitted a help desk request to Github asking that we get notified when folks comment on gists. Sorry about that.

I'll update this to the latest stable node. Thanks @vetri02

@brock

This comment has been minimized.

Copy link
Owner Author

brock commented Mar 5, 2015

@vetri02 I just checked and according to creationix/nvm, 0.10 is the latest stable

@tjfwalker

This comment has been minimized.

Copy link

tjfwalker commented Mar 6, 2015

@brock I got these error:

=> Appending source string to /Users/TJ/.bashrc
=> Close and reopen your terminal to start using nvm
/Users/TJ/.oh-my-zsh/oh-my-zsh.sh: line 26: syntax error near unexpected token `('
/Users/TJ/.oh-my-zsh/oh-my-zsh.sh: line 26: `for config_file ($ZSH/lib/*.zsh); do'
/Users/TJ/Desktop/nodereinstall.sh: line 48: nvm: command not found
/Users/TJ/Desktop/nodereinstall.sh: line 49: nvm: command not found
Reinstalling your global npm modules:
git-it grunt-cli keybase keybase-installer learnyounode roots sails stylus
/Users/TJ/Desktop/nodereinstall.sh: line 54: npm: command not found

Any thoughts?

@PierBover

This comment has been minimized.

Copy link

PierBover commented Mar 7, 2015

I'm getting the same error than @tjfwalker

/Users/pier/Desktop/nodereinstall.sh: line 47: nvm: command not found
/Users/pier/Desktop/nodereinstall.sh: line 48: nvm: command not found
Reinstalling your global npm modules:

/Users/pier/Desktop/nodereinstall.sh: line 53: npm: command not found
@brock

This comment has been minimized.

Copy link
Owner Author

brock commented Mar 9, 2015

@tjfwalker and @PierBover that is probably happening because this script, by default, was only sourcing the profile for ZSH (not Bash). I've since make this into a proper repo, and have made updates that check if you are using Bash or ZSH. Check it out here: https://github.com/brock/node-reinstall

@beckah

This comment has been minimized.

Copy link

beckah commented May 11, 2015

Beautiful.

@ayofish

This comment has been minimized.

Copy link

ayofish commented Aug 21, 2015

Thank you very much for this. I changed nvm install to stable :)

@vaibhavthapliyal

This comment has been minimized.

Copy link

vaibhavthapliyal commented Jan 6, 2016

Thanks a lot!! This saved my whole day. You deserve a Medal Of Honour for this!

@alexmgrant

This comment has been minimized.

Copy link

alexmgrant commented Feb 4, 2016

🍺

@trodicaro

This comment has been minimized.

Copy link

trodicaro commented Apr 8, 2016

Thank you! This is a lifesaver. I changed the NODE_VERSION to 5.10

@JosCline

This comment has been minimized.

Copy link

JosCline commented Jun 3, 2016

Thank you so much. I was in a downward spiral of installing/uninstalling and your script pulled me out!

@ksobkowicz

This comment has been minimized.

Copy link

ksobkowicz commented Aug 17, 2016

Thank you. It really helped a lot. Very good idea.

@joelpturman

This comment has been minimized.

Copy link

joelpturman commented Nov 6, 2016

Awesome. upgraded to 7.0 with this and everything appears to be working fine.

@neil-coutinho

This comment has been minimized.

Copy link

neil-coutinho commented Jan 24, 2018

You sir are the bomb! thank you for saving me from so much pain :)

@spedley

This comment has been minimized.

Copy link

spedley commented Jul 15, 2018

🍺

@Deacs

This comment has been minimized.

Copy link

Deacs commented Jul 19, 2018

Amazing! Thanks so much!

@emirbalic

This comment has been minimized.

Copy link

emirbalic commented Sep 20, 2018

Sir, you deserve a diamond medal of honour. Thanks forever!

@NortonGun

This comment has been minimized.

Copy link

NortonGun commented Jan 31, 2019

I wish I could buy you a Beer. Thanks a lot.

@lorepieri8

This comment has been minimized.

Copy link

lorepieri8 commented Jun 2, 2019

It didn't work for me. Not sure what went wrong honestly, but following this manual instructions worked: https://stackoverflow.com/questions/11177954/how-do-i-completely-uninstall-node-js-and-reinstall-from-beginning-mac-os-x

@usanin

This comment has been minimized.

Copy link

usanin commented Aug 8, 2019

You save my mind! Thanks

@barelabs

This comment has been minimized.

Copy link

barelabs commented Sep 22, 2019

Thank you, amazing!

@Moondancer83

This comment has been minimized.

Copy link

Moondancer83 commented Dec 30, 2019

This script is awesome!
Could you please update it to most up-to-date node version? or maybe with a line asking for desired version?
Thanks!

@brock

This comment has been minimized.

Copy link
Owner Author

brock commented Jan 7, 2020

@Moondancer83 See the repo for some of those updates: https://github.com/brock/node-reinstall

@roshnet

This comment has been minimized.

Copy link

roshnet commented Jan 18, 2020

This script definitely is battle-tested!

Thanks! It saved my day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.