This tutorial will show you how to install Node with Homebrew, and how to manage Node versions with n.
Homebrew, the self described "missing package manager for macOS and Linux," is and excellent all-in-one solution for installing, updating, and deleting packages that didn't come with your Mac.
Install Node with Homebrew
Open terminal and paste this into the prompt:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Now install Node by simply running
brew install node.
Pro tip: if you want to view the Node homepage, or any other package you have installed with Homebrew, you can do this easily by using the
brew home nodecommand.
Switching Node versions with n
Sometimes a project you are working on requires an older version of Node. Luckily installing and switching between different versions is easy with the NPM package n.
Since you already have Node installed, install n globally with NPM (Node's Package Manager):
npm install -g n
Switching between Node Versions with n
Now that you have n installed you can switch between Node versions easily with the following commands:
n lslists all versions of Node that are available.
n 10.15.1installs a specific version of Node.
nlists all locally installed versions of Node. If you want to switch to a different version use the
kkeys to select a version, then press enter to select that version. If you don't want to switch versions press
control + cto exit.
node -vreveals the version of Node you are currently using.
n latestinstalls and switches to the latest, bleeding edge release.
n stableinstalls and switches to the newest stable release.
n ltsinstalls and switches to the Long Term Supported (LTS) version.
n - 10.15.1will remove that version of Node.
n prunewill remove all versions except the one you are currently using.