I updated Node.js with nodebrew and scripted the routine to re-install npm install -g

If you're using nodebrew for Node.js version control, it's a pain to move the npm global package.

Ordinary procedure

1. Make a note of the list of globally installed npm packages

You do something like this.

$ npm ls -g --depth 0
/Users/kulikala/.nodebrew/node/v14.13.1/lib
├── @aws-amplify/[email protected]
├── @vue/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

Copy and paste and leave notes.

2. Install the latest version of Node.js with nodebrew

$ nodebrew install latest
$ nodebrew use latest

3. Reinstall the npm packages you wrote down

$ npm i -g @aws-amplify/cli @vue/cli aws-cdk firebase-tools nativescript npm-check-updates

nodebrew has a command called migrate-package that looks good, but it's a bit inconvenient because it's symlinked. I always do this because a clean new install will clean up the node_modules folder.

nodebrew migrate-package <version>    Install global NPM packages contained in <version> to current version

4. Say goodbye to older Node.js versions

$ nodebrew uninstall <Old version>
$ nodebrew clean all

Scripted routine

I'm using Bash, so I wrote it in Bash. If you post the following to .bash_profile, the update_node command will do all the routines for you.

.bash_profile


update_node () {
  check_env () {
    if ! command_exists nodebrew; then
      echo 'This script updates node via nodebrew.' 1>&2
      echo '    Please install nodebrew.' 1>&2
      echo '    https://github.com/hokaccha/nodebrew' 1>&2
      return 1
    fi

    if ! command_exists jq; then
      echo 'This script uses jq as JSON parser.' 1>&2
      echo '    Please install jq.' 1>&2
      echo '    https://stedolan.github.io/jq/' 1>&2
      return 1
    fi
  }

  command_exists () {
    command -v "$@" > /dev/null 2>&1
  }

  get_node_ver () {
    nodebrew ls \
      | grep current: \
      | cut -d ' ' -f 2
  }

  npm_ls_global () {
    npm ls -g --depth 0 --json \
      | jq --raw-output '.dependencies [].from | select(. != null)'
  }

  if ! check_env; then
    return 1
  fi

  echo 'Updating node...'

  local NODE_CURRENT="$(get_node_ver)"

  echo "Current node: ${NODE_CURRENT}"

  local NPM_GLOBAL="$(npm_ls_global)"

  nodebrew install latest
  nodebrew use latest

  local NODE_LATEST="$(get_node_ver)"

  if [ "${NODE_CURRENT}" = "${NODE_LATEST}" ]; then
    echo 'Already up to date.'
    return 0
  fi

  npm i -g ${NPM_GLOBAL}

  nodebrew uninstall "${NODE_CURRENT}"
  nodebrew clean all

  echo
  echo "Node is updated to: ${NODE_LATEST}"
}

About nodebrew

Use nodebrew to manage the version of node.js

Recommended Posts

I updated Node.js with nodebrew and scripted the routine to re-install npm install -g
I want to return to the previous screen with kotlin and java!
I translated the grammar of R and Java [Updated from time to time]
I tried to measure and compare the speed of GraalVM with JMH
Install the package with npm install / yarn add
I want to control the start / stop of servers and databases with Alexa
I wanted to start the AP server and debug with just the Maven command
I want to graduate from npm install properly [2020]
I tried to read and output CSV with Outsystems
I started MySQL 5.7 with docker-compose and tried to connect
What should I do to reload the updated Dockerfile?
I want to dark mode with the SWT app
I want to transition screens with kotlin and java!
How to install Gradle and Kotlin with SDKMAN (Mac)