├── img └── pay-attention.gif ├── package.json ├── Makefile ├── README.md └── node-reinstall /img/pay-attention.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brock/node-reinstall/HEAD/img/pay-attention.gif -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-reinstall", 3 | "version": "0.0.17", 4 | "description": "Completely re-install Node, NPM and NVM", 5 | "scripts": [ "node-reinstall" ], 6 | "install": "make install", 7 | "global": true 8 | } 9 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | BIN ?= node-reinstall 2 | PREFIX ?= /usr/local 3 | USAGE ?= $$(./node-reinstall -h | grep "Usage:") 4 | 5 | install: 6 | cp node-reinstall $(PREFIX)/bin/$(BIN) 7 | 8 | uninstall: 9 | rm -f $(PREFIX)/bin/$(BIN) 10 | 11 | readme: 12 | perl -pi -w -e "s/Usage:.*/$(USAGE)/" README.md 13 | sed '/Commands/,$$ d' README.md > changes.md 14 | mv changes.md README.md 15 | echo "## Commands" >> README.md 16 | echo '' >> README.md 17 | ./node-reinstall -h | sed -n -e '/Commands:/,// p' | tail -n +3 >> README.md 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | By [brock](https://github.com/brock) - Featuring [jwerle](https://github.com/jwerle), [hanoii](https://github.com/hanoii), [mistergraphx](https://github.com/mistergraphx), [gitter-badger](https://github.com/gitter-badger), & [seguri](https://github.com/seguri) 3 | 4 | --- 5 | 6 | Node-Reinstall is going to delete a lot of shit, and you won't be able to recover any of it. Do yourself a favor and make sure that you read the [node-reinstall](./node-reinstall) script and completely understand what it is going to do (i.e.: *what it is going to delete*) before you proceed. Here is an expanded version of the script that shows all of the directories that will be deleted: 7 | 8 | ``` 9 | rm -rf ~/local 10 | rm -rf ~/lib 11 | rm -rf ~/include 12 | rm -rf ~/node* 13 | rm -rf ~/npm 14 | rm -rf ~/.npm* 15 | sudo rm -rf /usr/local/lib/node* 16 | sudo rm -rf /usr/local/include/node* 17 | sudo rm -rf /usr/local/bin/node 18 | sudo rm -rf /usr/local/bin/npm 19 | sudo rm -rf /usr/local/share/doc/node 20 | sudo rm -rf /usr/local/share/man/man1/node.1 21 | sudo rm -rf /usr/local/share/systemtap/tapset/node.stp 22 | sudo rm -rf /usr/local/lib/dtrace/node.d 23 | ``` 24 | 25 | At an absolute minimum, you need to go into the home directories (the ones that start with `~/`) and make sure you are okay with deleting the contents of those directories. If you are unsure if this will delete anything important, you should stop now and find another alternative for re-installing Node.js, because this approach is pretty destructive. 26 | 27 | This script assumes you are comfortable enough with UNIX to perform these actions. If you are not, I will respond to your GitHub issue with the following GIF that is titled "pay-attention.gif" 28 | 29 | ![](img/pay-attention.gif) 30 | 31 | ## Summary 32 | This is a complete (and very destructive) tool for re-installing Node.js on OSX and Linux. See the [SO article](http://stackoverflow.com/a/11178106/2083544) for reference and the related [gist that spawned this repo](https://gist.github.com/brock/5b1b70590e1171c4ab54). This deletes ~~everything, yes everything,~~ **a lot of stuff you might want** and completely removes Node.js and NPM and replaces it with the Node Version Manager called [NVM](https://github.com/creationix/nvm). It will attempt to re-install any global NPM modules already installed, and you can opt for Nave instead of NVM if you prefer. 33 | 34 | It also works as a first-time installer. 35 | 36 | ## Installation 37 | 38 | ### Clone this Repo 39 | 40 | Clone this repo somewhere. If you have SSH setup with GitHub, use this format: 41 | ``` 42 | git clone git@github.com:brock/node-reinstall.git 43 | ``` 44 | 45 | Otherwise, clone this repo using HTTPS: 46 | ``` 47 | git clone https://github.com/brock/node-reinstall.git 48 | ``` 49 | 50 | ### Run the Re-Installer 51 | Change into the directory: 52 | ``` 53 | cd node-reinstall 54 | ``` 55 | 56 | To run `node-reinstall`, you can call it directly since it is an executable file: 57 | 58 | ``` 59 | ./node-reinstall 60 | ``` 61 | 62 | Or you can run it with bash: 63 | ``` 64 | bash node-reinstall 65 | ``` 66 | --- 67 | If you decide later that you want to re-install Node.js all over again, just come back to the directory where you cloned the `node-reinstall` repo, optionally update to the latest version of `node-reinstall` by running `git pull`, then run it again: 68 | 69 | ``` 70 | git pull 71 | bash node-reinstall 72 | ``` 73 | 74 | ## Optional 75 | If you are comfortable with Bash and the command line, you can copy the `node-reinstall` file to someplace in your [$PATH](http://en.wikipedia.org/wiki/PATH_%28variable%29) 76 | ``` 77 | cp node-reinstall ~/bin/node-reinstall 78 | ``` 79 | 80 | With `node-reinstall` in your [$PATH](http://en.wikipedia.org/wiki/PATH_%28variable%29) you can execute it from any directory: 81 | ``` 82 | node-reinstall 83 | ``` 84 | 85 | 86 | ## Usage 87 | 88 | Whenever you feel like you need to completely re-install Node and NPM, simply execute `node-reinstall`. You'll be prompted for sudo privileges since this will remove all possible installation paths. Pull requests welcome. 89 | 90 | 91 | Usage: node-reinstall [--nave|--nvm|--nvm-latest] [-h|--help] [-v|--version] [NODE_VERSION] 92 | 93 | ## Commands 94 | 95 | node-reinstall re-install node and npm using nvm 96 | node-reinstall [-h|--help] show help 97 | node-reinstall [-v|--version] show the node-reinstall version number 98 | node-reinstall [-f|--force] installs defaults without user confirmation 99 | node-reinstall --nave re-install using nave 100 | node-reinstall --nvm re-install using stable nvm - the default 101 | node-reinstall --nvm-latest re-install using latest nvm - creationix/nvm:master 102 | node-reinstall 5.0.0 specify a default node version 103 | -------------------------------------------------------------------------------- /node-reinstall: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # node-reinstall 3 | # credit: http://stackoverflow.com/a/11178106/2083544 4 | 5 | ## program version 6 | VERSION="0.0.17" 7 | 8 | ## path prefix 9 | PREFIX="${PREFIX:-/usr/local}" 10 | 11 | ## version control systems 12 | USE_NAVE=0 13 | USE_NVM=1 14 | 15 | # use --force to bypass user confirmation 16 | FORCE=0 17 | 18 | ## default node version 19 | DEFAULT_NODE_VERSION="5" 20 | 21 | ## user-specified node version to install 22 | DESIRED_NODE_VERSION= 23 | 24 | ## node version to install 25 | NODE_VERSION= 26 | 27 | usage () { 28 | printf "%s\n" "node-reinstall" 29 | printf "\t%s\n" "completely re-installs Node & NPM and any global node modules." 30 | printf "\t%s\n" "It re-installs Node using NVM or Nave" 31 | printf "\n" 32 | printf "%s\t%s\n" "Usage:" "node-reinstall [--nave|--nvm|--nvm-latest] [-h|--help] [-v|--version] [NODE_VERSION]" 33 | printf "\n" 34 | printf "%s\n" "Commands:" 35 | printf "\n" 36 | printf "\t%s\t\t\t%s\n" "node-reinstall" "re-install node and npm using nvm" 37 | printf "\t%s %s\t%s\n" "node-reinstall" "[-h|--help]" "show help" 38 | printf "\t%s %s\t%s\n" "node-reinstall" "[-v|--version]" "show the node-reinstall version number" 39 | printf "\t%s %s\t%s\n" "node-reinstall" "[-f|--force]" "installs defaults without user confirmation" 40 | printf "\t%s %s\t\t%s\n" "node-reinstall" "--nave" "re-install using nave" 41 | printf "\t%s %s\t\t%s\n" "node-reinstall" "--nvm" "re-install using stable nvm - the default" 42 | printf "\t%s %s\t%s\n" "node-reinstall" "--nvm-latest" "re-install using latest nvm - creationix/nvm:master" 43 | printf "\t%s %s\t\t%s\n" "node-reinstall" "0.12" "specify a default node version - currently ${NODE_VERSION}" 44 | printf "\n" 45 | } 46 | 47 | ## parse opts 48 | { 49 | 50 | for opt in ${@}; do 51 | case $opt in 52 | --help|-h) 53 | usage 54 | exit 55 | ;; 56 | 57 | --version|-v) 58 | echo ${VERSION} 59 | exit 60 | ;; 61 | 62 | --force|-f) 63 | FORCE=1 64 | ;; 65 | 66 | --nave) 67 | USE_NAVE=1 68 | USE_NVM=0 69 | ;; 70 | 71 | --nvm) 72 | USE_NVM=1 73 | USE_NAVE=0 74 | ;; 75 | 76 | --nvm-latest) 77 | USE_NVM=1 78 | USE_NAVE=0 79 | STABLE=master 80 | ;; 81 | 82 | *) 83 | if [ "-" == "${opt:0:1}" ]; then 84 | echo >&2 "error: Unknown option \`$opt'" 85 | usage >&2 86 | exit 1 87 | fi 88 | 89 | DESIRED_NODE_VERSION="${opt}" 90 | esac 91 | done 92 | 93 | } 94 | 95 | confirm () 96 | { 97 | if [[ $FORCE == 0 ]]; then 98 | echo "" 99 | echo "Would you like to continue running node-reinstall?" 100 | echo "" 101 | select yn in "Yes" "No"; do 102 | case $yn in 103 | Yes ) break;; 104 | No ) exit;; 105 | esac 106 | done 107 | fi 108 | } 109 | 110 | # get sudo 111 | sudo -v 112 | 113 | # if node is installed, get the installed version 114 | INSTALLED_NODE_VERSION=$(node --version 2> /dev/null) 115 | 116 | if [[ ! -z $DESIRED_NODE_VERSION ]]; then 117 | NODE_VERSION=$DESIRED_NODE_VERSION 118 | elif [[ ! -z $INSTALLED_NODE_VERSION ]]; then 119 | NODE_VERSION=$INSTALLED_NODE_VERSION 120 | else 121 | NODE_VERSION=$DEFAULT_NODE_VERSION 122 | fi 123 | 124 | # confirm re-installation only if node already exists 125 | if [[ ! -z $INSTALLED_NODE_VERSION ]]; then 126 | echo "Found Node.js version $INSTALLED_NODE_VERSION already installed." 127 | echo "If you continue now, we will replace it with Node.js version $NODE_VERSION" 128 | confirm 129 | fi 130 | 131 | # check to see if npm is installed 132 | IS_NPM_MISSING=$(which npm) 133 | if [[ -z $IS_NPM_MISSING ]]; then 134 | echo "Installing Node, npm." 135 | else 136 | echo "Completely reinstalling Node, npm." 137 | # get list of global npm modules to reinstall 138 | # omit the lib directory listing 139 | GLOBAL_MODULES=`npm -g list --depth 0 --parseable | xargs basename | sed -E 's/^(lib|npm)$//g'` 140 | if [[ -n $GLOBAL_MODULES ]]; then 141 | echo "Will reinstall these global npm modules:" 142 | echo $GLOBAL_MODULES 143 | else 144 | echo "===== ALERT! =====" 145 | echo "The script did not find any global node modules (npm -g list)" 146 | echo "If you are sure you installed global node modules" 147 | echo "(by running npm install -g some-module), you might want to stop " 148 | echo "here and locate those, because they won't be re-installed," 149 | echo "and since we'll be deleting all the possible install paths " 150 | echo "that most people could use, you probably won't find them." 151 | echo "" 152 | echo "This can sometimes happen if you've installed global node modules" 153 | echo "under a different node environment (for example, using nvm or nave)." 154 | echo "It might help to run: " 155 | echo "history | grep 'npm install' and look for either -g or --global" 156 | echo "" 157 | echo "If you aren't really sure, or you are sure and don't care, " 158 | echo "you can continue; we'll re-install things the proper way, and" 159 | echo "the next time you run this script you'll see a list of " 160 | echo "any global node modules you've installed since now." 161 | fi 162 | confirm 163 | fi 164 | 165 | if (( $USE_NVM )); then 166 | # NVM will think it is still installed if NVM_DIR is still set 167 | unset NVM_DIR 168 | sudo rm -rf $HOME/.nvm 169 | elif (( $USE_NAVE )); then 170 | sudo rm -rf $HOME/.nave 171 | else 172 | echo >&2 "error: Unsupported version control system" 173 | exit 1 174 | fi 175 | 176 | # erase all local install paths 177 | DIRECTORIES=("local" "lib" "include" "node*" "npm" ".npm" ".node-gyp") 178 | for i in "${DIRECTORIES[@]}" 179 | do 180 | if [ -d $HOME/$i ]; then 181 | LEN=$(ls -A $HOME/$i | wc -l 2> /dev/null) 182 | if [[ $LEN -gt 1 ]]; then 183 | echo "${HOME}/${i} has files or directories in it. You should check to see if you need the files in ${HOME}/${i} before proceeding!" 184 | confirm 185 | fi 186 | fi 187 | # do whatever on $i 188 | done 189 | 190 | rm -rf $HOME/{local,lib,include,node*,npm,.npm*,.node-gyp} 191 | 192 | # erase globally installed node paths 193 | sudo rm -rf $PREFIX/lib/node* 194 | sudo rm -rf $PREFIX/include/node* 195 | sudo rm -rf $PREFIX/bin/{node,npm} 196 | sudo rm -rf $PREFIX/share/doc/node 197 | sudo rm -rf $PREFIX/share/man/man1/node.1 198 | sudo rm -rf $PREFIX/share/systemtap/tapset/node.stp 199 | sudo rm -rf $PREFIX/lib/dtrace/node.d 200 | 201 | if (( $USE_NVM )); then 202 | latest=$(curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/nvm-sh/nvm/releases/latest | grep tag_name | grep -oE "v\d+\.\d+\.\d+") 203 | curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/$latest/install.sh | bash 204 | elif (( $USE_NAVE )); then 205 | curl -sL https://raw.githubusercontent.com/isaacs/nave/master/nave.sh -o $PREFIX/bin/nave 206 | fi 207 | 208 | 209 | if (( $USE_NVM )); then 210 | # install the specified version of node then set it as the default 211 | nvm install $NODE_VERSION 212 | nvm alias default $NODE_VERSION 213 | elif (( $USE_NAVE )); then 214 | nave usemain $NODE_VERSION 215 | fi 216 | 217 | if [[ ! -z ${GLOBAL_MODULES// } ]]; then 218 | echo "Reinstalling your global npm modules:" 219 | echo $GLOBAL_MODULES 220 | if [[ $FORCE == 0 ]]; then 221 | echo "Continue?" 222 | select yn in "Yes" "No"; do 223 | case $yn in 224 | Yes ) npm install --global $GLOBAL_MODULES; break;; 225 | No ) exit;; 226 | esac 227 | done 228 | else 229 | npm install --global $GLOBAL_MODULES 230 | fi 231 | 232 | fi 233 | 234 | if [[ $OSTYPE =~ "darwin" ]]; then 235 | echo "node-reinstall is done. 👍" 236 | else 237 | echo "node-reinstall is done." 238 | fi 239 | 240 | echo "You must restart your terminal for changes to take effect." 241 | --------------------------------------------------------------------------------