$ brew install node
Alternatively, go to nodejs.org and install it manually.
Go to nodejs.org and install it manually.
You should now have two new commands available,
npm. Try them out:
$ node --version v5.0.0 $ npm --version 3.3.12
(It's OK if your version numbers are higher.)
You can simply install the latest version over your old one. Better still, install a version manager (see below) so you can switch between different versions easily.
npm is the package manager for Node. It comes bundled with Node itself, but you might sometimes want to upgrade to a newer version if one is available. Because npm is itself an npm package, it can upgrade itself like this:
npm install --global npm
npm --version should show that you're on the latest available version.
These extra steps are not essential, but are recommended.
Follow these steps to relocate your global
node_modules directory so it's under your home directory.
Doing this means you'll be able to install global modules without
sudo, which is easier and safer.
Sometimes you have to open an old project that requires an old version of Node, and it's a pain to have to uninstall and reinstall Node just for this.
A version manager solves this problem: it lets you easily switch between Node versions with a simple command. It also makes it easier to get the latest Node when they release a new version.
There are two popular version managers available: n and nvm.
To install n (recommended):
npm install --global n
Now try switching to a different Node version (try
node --version after each one, to verify that the version actually changed):
n 0.12to switch to version 0.12
n 4.2to switch to version 4.2
n stableto switch to the latest stable version (this is the one you should generally stick to)