Migrate From nodebrew to nvm
I have been using nodebrew
as the node
version manager, however, I recently decided to switch to nvm
as it is more popular. This article is a note of what I needed to do.
My Environment
- macOS Catalina: Version 10.15.4
- Zsh with
.zprofile
1. Generate a list of global packages installed via the current version of node
First, as current global npm
packages belong to the specific version of node
installed via nodebrew
, you need to take a note of it so you can re-install everything later on. The following command lets you make a JSON file of the list of your global packages. The right side of >
is the destination of the output file. You can change it as you want.
npm list --depth=0 -g > ~/Desktop/global-package-list.json
There might be a better way to create the output file without unnecessary information so you can install the packages easier later. In my case, I only had 16 packages, so this command was good enough.
2. Uninstall node brew
As of now, there is no specific command to uninstall nodebrew
. Therefore, we just need to remove the package manually. Usually, nodebrew
is installed into your home directory, so the following command should work.
rm -rf ~/.nodebrew
Then, you need to also delete the source line from your profile file. In my case, it is .zprofile
. Just delete or comment out the following line.
# Remove the line in your profile file
export PATH=$HOME/.nodebrew/current/bin:$PATH
3. Install nvm
Next, install nvm
into your machine. The following command is to install the current latest version of nvm
. Go to check the official GitHub repository to know what the latest version is at the time when you are reading this article.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
Then, you need to add the source line below to the correct profile file. In my case, it was .zprofile
. Again, there is a slight chance that the line is different at the time when you are reading this article. In that case, you can always go to check the official documentation.
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
4. Install some node versions
Now, you should have nvm
installed on your computer. It’s about time to install some node
versions that you want to have on your machine. You can refer to the following steps to install them. --latest-npm
is a handy option to install the latest version of npm
along with node
.
# List downloadable node versions
nvm ls-remote# Install LTS version with the latest npm
nvm install --lts --latest-npm
# Install latest version with the latest npm
nvm install node --latest-npm
# Install a specific version of node with the latest npm
nvm install v13.14.0 --latest-npm# Set default node version
nvm alias default v14.5.0# Switch node version during the current session
nvm use v13.14.0
5. Install the global packages referring to the output file
Finally, install the packages from the list that you created in Step 1.
npm install list-your-packages-separated-by-space...
That’s everything for this article. Now you can simply enjoy your coding life with nvm
.
References
- nvm-sh / nvm — Official GitHub Repository:
https://github.com/nvm-sh/nvm