If you're using nodebrew for Node.js version control, it's a pain to move the npm global package.
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.
$ nodebrew install latest
$ nodebrew use latest
$ 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
$ nodebrew uninstall <Old version>
$ nodebrew clean all
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}"
}
Use nodebrew to manage the version of node.js
Recommended Posts