Skip to content

Instantly share code, notes, and snippets.

@rbaumbach
Forked from brock/nodereinstall.sh
Last active August 29, 2015 14:14
Show Gist options
  • Save rbaumbach/fbcceb04406e992051a0 to your computer and use it in GitHub Desktop.
Save rbaumbach/fbcceb04406e992051a0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# nodereinstall
# credit: http://stackoverflow.com/a/11178106/2083544
# 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, and nvm."
else
echo "Completely reinstalling Node, npm, and nvm."
# 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
fi
fi
# NVM will think it is still installed if NVM_DIR is still set
unset NVM_DIR
# erase all possible install paths
sudo rm -rf /usr/local/lib/node*
sudo rm -rf /usr/local/include/node*
sudo rm -rf ~/{local,lib,include,node*,npm,.npm*}
sudo rm -rf /usr/local/bin/{node*,npm}
sudo rm -rf /usr/local/bin/npm
sudo rm -rf /usr/local/share/man/man1/node.1
sudo rm -rf /usr/local/lib/dtrace/node.d
sudo rm -rf ~/.npm
sudo rm -rf ~/.nvm
# go home and install NVM just because I feel safe there
cd $HOME
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
# you must "source" the NVM exports - yours are most likely in ~/.zshrc or ~/.bashrc or ~/.bash_profile
source ~/.zshrc
# source ~/.bashrc
# source ~/.bash_profile
# install the latest 0.10 version of node then set it as the default
nvm install 0.10
nvm alias default 0.10
echo "Reinstalling your global npm modules:"
echo $GLOBAL_MODULES
npm install --global $GLOBAL_MODULES
@brock
Copy link

brock commented Mar 9, 2015

Thanks for forking this gist! I've made a few key updates here:
https://github.com/brock/node-reinstall

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment