How to install Node on Mac using NVM and Homebrew

Published

Node Version Manager makes it really easy to install multiple versions of Node on a Mac. You install NMV using Homebrew and if you don't have Homebrew installed yet, read this article.

Step 1: Install NVM

Install node version manager.

brew install nvm

brew-install-nvm

Step 2: Create a directory for NVM

mkdir ~/.nvm/

Step 3: Configure your environmental variables

Either create or open your .bash_profile.

vim ~/.bash_profile

Paste this.

export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh

Execute the content we just pasted.

source ~/.bash_profile

Step 4: Double check your work

If you've done everything correctly, you should be able to run the nvm command and ask it to list which versions node are available.

nvm ls

nvm-ls

Step 5: Install Node

You can see a list of previously released versions of node from the NodeJS release page

Install Node 6.x.x.

nvm install 6.x.x

nvm-install-6.11.4

Install Node 8.x.x

nvm install 8.x.x

Double check your installation.

nvm ls

nvm-ls-again

Step 6: Set Node Globally

You can set a specific version of Node as your default.

nvm alias default 8.x.x

Troubleshooting

Error Example #1

If you are having issues with npm, then you might want to try this.

rm -rf /usr/local/lib/node_modules
brew uninstall node
brew install node --without-npm
echo prefix=~/.npm-packages >> ~/.npmrc
curl -L https://www.npmjs.com/install.sh | sh

Add this to your local variables so that npm and global npm are usable.

export PATH="$HOME/.npm-packages/bin:$PATH"

*Source

Error Example #2

fetching: https://registry.npmjs.org/npm/-/npm-6.4.1.tgz
up to date in 0.027s
sh: rimraf: command not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! npm@6.4.1 prepare: `node bin/npm-cli.js --no-audit --no-timing prune --prefix=. --no-global && rimraf test/*/*/node_modules && make -j4 doc`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the npm@6.4.1 prepare script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/myaccount/.npm/_logs/*-debug.log
npm ERR! code E404
npm ERR! 404 Not Found: 0.99s@latest

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/myaccount/.npm/_logs/*-debug.log
It failed

If you see an error like this, then checkout this StackOverflow solution

Comments