Skip to content

Instantly share code, notes, and snippets.

@brock
Last active March 27, 2025 16:22
Show Gist options
  • Select an option

  • Save brock/5b1b70590e1171c4ab54 to your computer and use it in GitHub Desktop.

Select an option

Save brock/5b1b70590e1171c4ab54 to your computer and use it in GitHub Desktop.

Revisions

  1. brock revised this gist Mar 11, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nodereinstall.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    # credit: http://stackoverflow.com/a/11178106/2083544

    ## program version
    VERSION="0.0.11"
    VERSION="0.0.13"

    ## path prefix
    PREFIX="${PREFIX:-/usr/local}"
  2. brock revised this gist Mar 11, 2016. No changes.
  3. brock revised this gist Sep 21, 2015. 1 changed file with 4 additions and 7 deletions.
    11 changes: 4 additions & 7 deletions nodereinstall.sh
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,6 @@
    # node-reinstall
    # credit: http://stackoverflow.com/a/11178106/2083544

    #
    # 2015-08-22: Gist updated to reflect latest changes in the git repo:
    # https://github.com/brock/node-reinstall
    #

    ## program version
    VERSION="0.0.11"

    @@ -193,11 +188,13 @@ if [[ ! -z ${GLOBAL_MODULES// } ]]; then
    else
    npm install --global $GLOBAL_MODULES
    fi

    fi

    if [[ $OSTYPE =~ "darwin" ]]; then
    echo "node-reinstall is done. 👍"
    else
    echo "node-reinstall is done."
    fi
    fi

    echo "You must restart your terminal for changes to take effect."
  4. brock revised this gist Aug 22, 2015. 1 changed file with 178 additions and 33 deletions.
    211 changes: 178 additions & 33 deletions nodereinstall.sh
    Original file line number Diff line number Diff line change
    @@ -1,58 +1,203 @@
    #!/bin/bash
    # nodereinstall
    # node-reinstall
    # credit: http://stackoverflow.com/a/11178106/2083544

    #
    # UPDATE: I've converted this gist to a repo:
    # 2015-08-22: Gist updated to reflect latest changes in the git repo:
    # https://github.com/brock/node-reinstall
    #

    ## program version
    VERSION="0.0.11"

    ## 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, and nvm."
    echo "Installing Node, npm."
    else
    echo "Completely reinstalling Node, npm, and nvm."
    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

    # NVM will think it is still installed if NVM_DIR is still set
    unset NVM_DIR
    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 /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
    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
  5. brock revised this gist Mar 9, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions nodereinstall.sh
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,11 @@
    # nodereinstall
    # credit: http://stackoverflow.com/a/11178106/2083544

    #
    # UPDATE: I've converted this gist to a repo:
    # https://github.com/brock/node-reinstall
    #

    # get sudo
    sudo -v

  6. brock revised this gist Oct 27, 2014. 1 changed file with 26 additions and 12 deletions.
    38 changes: 26 additions & 12 deletions nodereinstall.sh
    Original file line number Diff line number Diff line change
    @@ -2,23 +2,37 @@
    # nodereinstall
    # credit: http://stackoverflow.com/a/11178106/2083544

    # 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'`
    # 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
    rm -rf /usr/local/lib/node*
    rm -rf /usr/local/include/node*
    rm -rf ~/{local,lib,include,node*,npm,.npm*}
    rm -rf /usr/local/bin/{node*,npm}
    rm -rf /usr/local/bin/npm
    rm -rf /usr/local/share/man/man1/node.1
    rm -rf /usr/local/lib/dtrace/node.d
    rm -rf ~/.npm
    rm -rf ~/.nvm
    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
  7. brock revised this gist Sep 14, 2014. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions nodereinstall.sh
    Original file line number Diff line number Diff line change
    @@ -22,10 +22,7 @@ rm -rf ~/.nvm

    # go home and install NVM just because I feel safe there
    cd $HOME

    # curl https://raw.githubusercontent.com/creationix/nvm/v0.16.0/install.sh | bash
    # get the latest version - the gist I'm curling calls the above curl command
    curl https://gist.githubusercontent.com/brock/e0b10c2d9624dae694db/raw/157b067b0820967666a3d2c9b7961560bc98970f/latest.sh | bash
    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
  8. brock revised this gist Sep 14, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion nodereinstall.sh
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,10 @@ rm -rf ~/.nvm

    # go home and install NVM just because I feel safe there
    cd $HOME
    curl https://raw.githubusercontent.com/creationix/nvm/v0.16.0/install.sh | bash

    # curl https://raw.githubusercontent.com/creationix/nvm/v0.16.0/install.sh | bash
    # get the latest version - the gist I'm curling calls the above curl command
    curl https://gist.githubusercontent.com/brock/e0b10c2d9624dae694db/raw/157b067b0820967666a3d2c9b7961560bc98970f/latest.sh | bash

    # you must "source" the NVM exports - yours are most likely in ~/.zshrc or ~/.bashrc or ~/.bash_profile
    source ~/.zshrc
  9. brock revised this gist Sep 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nodereinstall.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    # get list of global npm modules to reinstall
    # omit the lib directory listing
    GLOBAL_MODULES=`npm -g list --depth 0 --parseable | xargs basename | sed s/^lib$//g`
    GLOBAL_MODULES=`npm -g list --depth 0 --parseable | xargs basename | sed -E 's/^(lib|npm)$//g'`

    # NVM will think it is still installed if NVM_DIR is still set
    unset NVM_DIR
  10. brock revised this gist Sep 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nodereinstall.sh
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ rm -rf ~/.nvm

    # go home and install NVM just because I feel safe there
    cd $HOME
    curl https://raw.githubusercontent.com/creationix/nvm/v0.15.0/install.sh | bash
    curl https://raw.githubusercontent.com/creationix/nvm/v0.16.0/install.sh | bash

    # you must "source" the NVM exports - yours are most likely in ~/.zshrc or ~/.bashrc or ~/.bash_profile
    source ~/.zshrc
  11. brock created this gist Sep 13, 2014.
    39 changes: 39 additions & 0 deletions nodereinstall.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #!/bin/bash
    # nodereinstall
    # credit: http://stackoverflow.com/a/11178106/2083544

    # get list of global npm modules to reinstall
    # omit the lib directory listing
    GLOBAL_MODULES=`npm -g list --depth 0 --parseable | xargs basename | sed s/^lib$//g`

    # NVM will think it is still installed if NVM_DIR is still set
    unset NVM_DIR

    # erase all possible install paths
    rm -rf /usr/local/lib/node*
    rm -rf /usr/local/include/node*
    rm -rf ~/{local,lib,include,node*,npm,.npm*}
    rm -rf /usr/local/bin/{node*,npm}
    rm -rf /usr/local/bin/npm
    rm -rf /usr/local/share/man/man1/node.1
    rm -rf /usr/local/lib/dtrace/node.d
    rm -rf ~/.npm
    rm -rf ~/.nvm

    # go home and install NVM just because I feel safe there
    cd $HOME
    curl https://raw.githubusercontent.com/creationix/nvm/v0.15.0/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