Switch to Node Version Manager (nvm) on macOS using Homebrew
By Ryan Klarhölter, Fullstack Developer
I have recently switched to macOS after using Linux for a decade. The first thing that bothered me at the start: no package manager! The obvious choice was to use Homebrew. Since choosing it, I now install everything I can with Homebrew (and that includes not only command-line tools, but even applications that come with a GUI, like Visual Studio Code or a browser, which is possible thanks to Homebrew Cask).
I have also installed Node with Homebrew. When I needed a second Node version on my machine, I installed it with Homebrew (brew install node@<version>
). It is easy to switch between versions: brew unlink node && brew link --force --overwrite node@<version>
. When I started using it, it felt fragile (and because of that, unprofessional as well), and so I ran into a few problems:
- I had to always reset my Node version, when Node was being updated by
brew upgrade
. - I once had to clear some directories (that I forgot about) after switching between versions or updating Node. That was an annoying issue which cost me time.
- At some point, I updated npm and it didn’t work anymore with a specific Node version.
That is why, in the end, I decided to switch to nvm. I read on their page they do not support Homebrew installations, but I went for it anyway, and did not experience any problems. I found the following articles very helpful:
- https://jamesauble.medium.com/install-nvm-on-mac-with-brew-adb921fb92cc
- https://michael-kuehnel.de/node.js/2015/09/08/using-vm-to-switch-node-versions.html
Below my step-by-step instructions based on these articles:
- Remove globally installed npm packages (keep a list for later if you want to reinstall them:
npm list -g --depth=0
):rm -rf /usr/local/lib/node_modules
. - Uninstall Node:
brew uninstall --ignore-dependencies node && brew uninstall --force node
. - Install nvm:
brew update && brew install nvm
. - Create a directory for nvm:
mkdir ~/.nvm
. - Add the following nvm configuration to
~/.zshrc
(or.bash_profile
, depends on the shell you use):export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
source $(brew --prefix nvm)/etc/bash_completion.d/nvm - Reopen your terminal or run:
source ~/.zshrc
. - Install all the Node versions you need:
nvm install <node-version>
. (You can transfer your globally installed packages from one version to another if you want: search the web for “nvm reinstall-packages-from”.) - (Optional.) List installed Node versions:
nvm ls
. - (Optional.) Set default Node version that is used in any new shell:
nvm alias default <node-version>
. - (Optional.) Switch Node version:
nvm use <node-version>
.
That’s it! Your life is a little easier now.
You want to shape the digital future of the fashion industry? Start creating with us. The Digital Intelligence & Tech Teams at Marc O’Polo are always looking for talented and driven software engineers, data engineers, data scientists, data analysts, ML Engineers or project managers to join our team. Learn about life at Marc O’Polo and visit our career site.