Installing NVM with Homebrew
If you're reading this, I'm going to assume that you already know what NVM is, if not, in short, NVM (or Node Version Manager) is a command-line script to manage multiple active node.js versions. Since I started using NVM, it's made my local development environment setup (when using node.js) a hell of a lot simpler purely because of how quick and easy it is to switch between various different versions of Node depending on the project requirements.
As for Homebrew (again, I’m going to assume you already know what Homebrew is)... I’ve been a longtime advocate and user of Homebrew. I use to control pretty much all of my local development environment and as of late, I’ve been using it as the crux of my dot files installation sequence too. Since I wanted to use NVM for managing my local development versions of node, I thought I’d document how you can install NVM via brew despite the fact that it’s not officially supported as of yet.
First up, install NVM via brew:
$ brew install nvm
Then we need to create a folder called .nvm for the current user where the various versions of node.js will reside.
$ mkdir ~/.nvm
Next up, you’ll want to execute NVM in every shell session. To do that, we need add the code below to your `~/.bashrc` or `~/.zshrc` files depending on what you’re using.
Note: NVM isn’t supported by the Fish shell (amongst others), you can read about which ones aren’t supported NVM Notes.
export NVM_DIR="$HOME/.nvm" . "$(brew --prefix nvm)/nvm.sh"
Now we can install a node version. There are various different methods of installing node versions depending on project spec or your preference:
The latest version of Node
$ nvm install node
Node with long term support
$ nvm install --lts
A specific version of node
$ nvm install 6.15.1
After you’ve installed the version of Node that you were looking for, it will be active after running this command. This is also useful if you want to change from different Node.js versions too.
$ nvm use node
To check if everything with NVM is now working as expected, the command below will output the current Node version:
$ nvm run node --version
What’s great about NVM is the ability to declare a different version of node.js on a project by project basis simply by adding a .nvmrc file to the project directory. For ease, i put mine in the same directory as the package.json file. Once you’ve created this file, all you need to do is add the version of node.js into the file.
If that’s too complicated, you can even generate this file automatically based on the version of Node currently being used by NVM.
$ node -v > .nvmrc
The use command earlier to switch versions of node is also used with the .nvmrc file except that you don’t have to remember which version of Node you’re wanting to use. Once you’re happy you’ve got your project's setup with different versions of node declared in .nvmrc files, you can just run a simplified use command which will switch the working version of node to whatever is declared.
$ node use
Note: This is how I installed NVM running High Sierra. Your mileage may vary depending on your system setup.
🡐 Older NotePurrp - A purple VS Code theme!
Newer Note 🡒Tabs vs Spaces