This tutorial shows you how to install the Node.js runtime and the Node.js package manager, npm, 3 different ways — from the Node.js download page, via Homebrew (for MacOS installation), and through the Node.js Version Manager. After showing you how to install Node.js, I walk you through installing the editor, VS Code, where you can write your source code for this learning path.
This video walks you through the steps in this Node.js tutorial:
You need to install the Git client for your platform. Follow the instructions in the Git documentation to download Git for your platform.
Three approaches to installing Node.js and npm
There are several ways you can install Node.js.
The best way to install Node.js and npm is through Node.js Version Manager, or nvm. With nvm, you can install multiple versions of both the Node.js runtime and npm, and they can all coexist, though you can only specify one active version of either at a time. What I mean is, you don’t have to uninstall one version of Node.js or npm to install another, just tell nvm which one you want to use. It’s great for compatibility testing and managing your Node.js environment in general.
The easiest approach is to download the package for your platform directly from the Node.js project’s Download page and run the installation program. This approach is ideal for Windows users, but alos works for MacOS and Linux as well.
If you’re a Mac user, a better approach is to use Homebrew. Homebrew bills itself as the “missing package manager for MacOS.” You can use it to install and manage hundreds of different packages for MacOS.
I’ve summarized the pros and cons of each approach in the table below:
|Straight download from nodejs.org||Simple, Most platforms supported||Manual checksum validation|
|Homebrew||Simple, Automatic Checksum validation, Can swap between multiple versions with a little tweaking||MacOS only|
|nvm||Simple, Automatic checksum validation, Can easily swap between multiple versions||MacOS and Linux only (community contribs for other platforms are available)|
Homebrew and nvm offer automatic checksum validation (which is nice), but have limited officially supported platforms. I describe each approach in detail in the sections that follow, so you can choose the approach that is right for you.
Choosing a Node.js release
On the Downloads page, you have to choose between the long-term support (LTS) or current release. What is the difference?
The Node.js release team designates even-numbered major releases as LTS releases. The LTS releases are supported for up to 3 years from the date of its original release, during which time the release goes from current (~6 months) to active LTS (18 months, minor revisions only) to maintenance LTS (12 months, patch releases only). This means you can be sure that an LTS release will be stable for a considerable period of time.
If you want to know more about the Node.js release process, check out the Release page, which includes information about the release schedule, the release mandate, and the methodology for LTS releases (along with what goes in them).
For the Node.js learning path, I use version 10 because, at the time of this writin,g it is the current LTS release. You probably want to do the same to ensure you can follow along.
Download installation package from the Node.js project website
Go to the Downloads page, and click on the installer that matches your platform.
As I said in the previous section, I use version 10 for the Learning Path, and this is reflected in all instructions in this section, including the video. I suggest you use version 10.4.0 (or whatever is the latest 10.x.y release at the time), which will almost certainly be the LTS release when you read this (or very close).
Once the installer is downloaded to your computer, and you have verified the checksum matches, launch the installer. On my Mac, for example, I double click on the Package installer program (called node-v10.4.0.pkg) to run the package installer. I show you how to do this in the video. If you’re running Windows, double click the installer and follow the steps in the installation wizard.
For Linux, unpack the tarball to a standard location such as
/usr/local/lib/nodejs, making sure that the path to the Node.js bin directory matches your
PATH environment variable. See this link from the Node.js GitHub Wiki for more detailed instructions.
Now skip to Verify the Installation section to confirm that Node.js and npm were installed correctly.
Going forward, when you want to upgrade your version of Node.js, download the new installer and it will replace the current version.
Using this method of installation, you may have only one version of Node.js and npm installed at a time. If you want to have multiple versions installed and switch back and forth among them (for compatibility testing, let’s say), I recommend you use nvm (See Best: Use nvm for details).
Use Homebrew to install Node.js on MacOS
Homebrew is a better way to manage various packages on your Mac. Whenever I need to install a familiar Linux package, I check to see if it is available as a Homebrew “formula” first before trying to install the package another way (with rare exceptions like using nvm for managing Node.js, which I’ll show you in the next section).
To learn more about Homebrew and how to install it, check out the Installation page.
To install the current version of Node.js and npm via Homebrew, open a Terminal window on your Mac and enter this command:
brew install node
However, I highly recommend you install specific major versions of Node.js as a matter of practice, since it forces you to be aware of exactly what version you’re installing. If the specific major version you install happens to be the current version, then Homebrew will link it for you automatically (meaning, it will create symbolic links to
npm in your
PATH to the current version in the Homebrew Cellar). Plus, you can switch between installed versions by unlinking one and linking another using the
brew unlink and
brew link commands (though you should use this with caution, as I’ll show you later in this section).
To install the latest Node.js version 10, enter the
brew install node@10 command. You’ll see output like this:
$ brew install node@10 Updating Homebrew... ==> Auto-updated Homebrew! Updated 1 tap (homebrew/core). No changes to formulae. ==> Downloading https://homebrew.bintray.com/bottles/node-10.4.0.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring node-10.4.0.high_sierra.bottle.tar.gz ==> Caveats Bash completion has been installed to: /usr/local/etc/bash_completion.d ==> Summary 🍺 /usr/local/Cellar/node/10.4.0: 6,793 files, 59.6MB $
Now skip to the Verify the Installation section to confirm that Node.js and npm were installed correctly.
Note: homebrew always installs the current version of Node.js when you use the command as I’ve shown it above. If you want to install a specific major version of Node.js, you specify the major version number like this:
brew install node@8
You’ll see output like this:
$ brew install node@8 ==> Downloading https://email@example.com_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring firstname.lastname@example.org_sierra.bottle.tar.gz ==> Caveats This formula is keg-only, which means it was not symlinked into /usr/local, because this is an alternate version of another formula. If you need to have this software first in your PATH run: echo 'export PATH="/usr/local/opt/node@8/bin:$PATH"' >> ~/.bash_profile For compilers to find this software you may need to set: LDFLAGS: -L/usr/local/opt/node@8/lib CPPFLAGS: -I/usr/local/opt/node@8/include ==> Summary 🍺 /usr/local/Cellar/node@8/8.11.2: 4,430 files, 47MB
This installs the latest version of Node.js 8, which means the latest minor.patch release only. Note: if the version you install is not the current version of Node.js (even if it is the active LTS), it will be a “keg-only” install. See “What does keg-only mean?” in the Homebrew FAQ for more info.
Going forward, to upgrade Node.js use:
brew upgrade node
If there is an upgrade available, it is automatically applied for you.
Switching between multiple versions
Homebrew allows you to have multiple major versions of Node.js and npm installed at a time, but only one may be active. If you want to switch between them, you must “unlink” the actively linked version, and “link” the one you want to use.
On my Mac this means I have to jump through a few hoops (involving
--overwrite) to switch from version 10 to version 8, and more than once I’ve accidentally left my system in a weird state (as in,
-bash: /usr/local/bin/npm: No such file or directory). For example, here’s what I have to go through to switch from Node.js 10 to Node.js 8 and back to Node.js 10:
Ix:~ sperry$ brew unlink node Unlinking /usr/local/Cellar/node/10.4.0... 7 symlinks removed Ix:~ sperry$ brew link --force --overwrite node@8 Linking /usr/local/Cellar/node@8/8.11.2... 4287 symlinks created If you need to have this software first in your PATH instead consider running: echo 'export PATH="/usr/local/opt/node@8/bin:$PATH"' >> ~/.bash_profile Ix:~ sperry$ node -v v8.11.2 Ix:~ sperry$ npm -v 5.6.0 Ix:~ sperry$ brew unlink node@8 Unlinking /usr/local/Cellar/node@8/8.11.2... 4287 symlinks removed Ix:~ sperry$ brew link node Linking /usr/local/Cellar/node/10.4.0... 7 symlinks created Ix:~ sperry$ node -v v10.4.0 Ix:~ sperry$ npm -v -bash: /usr/local/bin/npm: No such file or directory Ix:~ sperry$ brew reinstall node ==> Reinstalling node ==> Downloading https://homebrew.bintray.com/bottles/node-10.4.0.high_sierra.bottle.tar.gz Already downloaded: /Users/sperry/Library/Caches/Homebrew/node-10.4.0.high_sierra.bottle.tar.gz ==> Pouring node-10.4.0.high_sierra.bottle.tar.gz ==> Caveats Bash completion has been installed to: /usr/local/etc/bash_completion.d ==> Summary 🍺 /usr/local/Cellar/node/10.4.0: 6,793 files, 59.6MB Ix:~ sperry$ npm -v 6.1.0 Ix:~ sperry$
Yikes, what a pain! However, as you can see, I got it to work. But I ended up having to reinstall Node.js 10 because the
unlink from Node.js 8 caused something to go wrong. Maybe I did something incorrectly. Maybe Homebrew just doesn’t like me. Maybe there was a full moon last night. Who knows?
If you really need to switch painlessly between versions of Node.js and npm, I highly recommend you use nvm. Also, if you want to have multiple major.minor.patch versions installed and switch back and forth among them (for compatibility testing, let’s say), you must use nvm, since Homebrew only supports installing the latest major.minor.patch release.
What about checksum validation? Homebrew does checksum validation for you automatically, which is one more reason to use it versus a straight package install.
Use nvm to install Node.js on MacOS and Linux only
Sorry, Windows users. NVM is officially only supported on MacOS and Linux, but there are two alternatives (neither of which is officially supported by the NVM team, though I got these links from the NVM GitHub site, so wink wink):
Other unsupported platforms are listed as well under Important Notes.
Installing nvm is a snap. Go to the creationix GitHub repo, and scroll down to the Installation section (or click this link. Copy the install command and paste it into a Terminal window or command prompt:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
It takes just a few seconds (less than 10 on my Mac) to download and install.
The install script makes changes to your
.bash_profile (MacOS) or
.profile (Linux), so when the script finishes, you’ll need to rerun your profile script using your favorite method for doing so. I’m lazy, so I’ll just stop the Terminal window and open a new one.
Verify that nvm installed correctly by running the
nvm --version command. You should see output like this:
$ nvm --version 0.33.11
To install Node.js and npm run the
nvm install --lts command which tells nvm to install the LTS version (which at the time of this writing is Node.js 8.11.2 and npm 5.6.0):
$ nvm install --lts Installing latest LTS version. Downloading and installing node v8.11.2... Downloading https://nodejs.org/dist/v8.11.2/node-v8.11.2-darwin-x64.tar.gz... ######################################################################## 100.0% Computing checksum with shasum -a 256 Checksums matched! Now using node v8.11.2 (npm v5.6.0)
Now verify the installation using the instructions in the next section.
Switching between multiple versions with nvm
You can install multiple versions of Node.js and npm installed (including any major.minor.patch combination that has been released) and switch back and forth between them. This is great for compatibility testing, for example. Suppose you want to install Node.js 10 (which at the time of this writing is 10.4.0):
$ nvm install 10 Downloading and installing node v10.4.0... Downloading https://nodejs.org/dist/v10.4.0/node-v10.4.0-darwin-x64.tar.gz... ######################################################################## 100.0% Computing checksum with shasum -a 256 Checksums matched! Now using node v10.4.0 (npm v6.1.0) $ node -v v10.4.0 Ix:~ sperry$ npm -v 6.1.0
Then switch back to Node.js 8 (I’ll assume you’ve been following along with this tutorial, and the version in use is 10):
Ix:~ sperry$ nvm use 8 Now using node v8.11.2 (npm v5.6.0) Ix:~ sperry$ node -v v8.11.2 Ix:~ sperry$ npm -v 5.6.0
Check it out! I can swap out the Node runtime on the PATH with a simple command. Pretty cool.
Notice that the version of npm is also upgraded to match the version of Node you downloaded. Unless you specify otherwise, Node.js and npm are sort of a package deal (and that’s usually a good thing).
To see all the options available, just type nvm and this help screen appears:
Usage: nvm --help Show this message nvm --version Print out the installed version of nvm nvm install [-s] <version> Download and install a <version>, [-s] from source. Uses .nvmrc if available --reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number> --lts When installing, only select from LTS (long-term support) versions --lts=<LTS name> When installing, only select from versions for a specific LTS line --skip-default-packages When installing, skip the default-packages file if it exists --latest-npm After installing, attempt to upgrade to the latest working npm on the given node version nvm uninstall <version> Uninstall a version nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available. nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available. nvm use [--silent] <version> Modify PATH to use <version>. Uses .nvmrc if available --lts Uses automatic LTS (long-term support) alias `lts/*`, if available. --lts=<LTS name> Uses automatic alias for provided LTS line, if available. nvm exec [--silent] <version> [<command>] Run <command> on <version>. Uses .nvmrc if available --lts Uses automatic LTS (long-term support) alias `lts/*`, if available. --lts=<LTS name> Uses automatic alias for provided LTS line, if available. nvm run [--silent] <version> [<args>] Run `node` on <version> with <args> as arguments. Uses .nvmrc if available --lts Uses automatic LTS (long-term support) alias `lts/*`, if available. --lts=<LTS name> Uses automatic alias for provided LTS line, if available. nvm current Display currently activated version nvm ls List installed versions nvm ls <version> List versions matching a given <version> nvm ls-remote List remote versions available for install --lts When listing, only show LTS (long-term support) versions nvm ls-remote <version> List remote versions available for install, matching a given <version> --lts When listing, only show LTS (long-term support) versions --lts=<LTS name> When listing, only show versions for a specific LTS line nvm version <version> Resolve the given description to a single local version nvm version-remote <version> Resolve the given description to a single remote version --lts When listing, only select from LTS (long-term support) versions --lts=<LTS name> When listing, only select from versions for a specific LTS line nvm deactivate Undo effects of `nvm` on current shell nvm alias [<pattern>] Show all aliases beginning with <pattern> nvm alias <name> <version> Set an alias named <name> pointing to <version> nvm unalias <name> Deletes the alias named <name> nvm install-latest-npm Attempt to upgrade to the latest working `npm` on the current node version nvm reinstall-packages <version> Reinstall global `npm` packages contained in <version> to current version nvm unload Unload `nvm` from shell nvm which [current | <version>] Display path to installed node version. Uses .nvmrc if available nvm cache dir Display path to the cache directory for nvm nvm cache clear Empty cache directory for nvm Example: nvm install 8.0.0 Install a specific version number nvm use 8.0 Use the latest available 8.0.x release nvm run 6.10.3 app.js Run app.js using node 6.10.3 nvm exec 4.8.3 node app.js Run `node app.js` with the PATH pointing to node 4.8.3 nvm alias default 8.1.0 Set default node version on a shell nvm alias default node Always default to the latest available node version on a shell Note: to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)
You can also check out the NVM documentation.
Verify the installation
Once Node.js and npm are installed, verify that they are installed correctly:
node -v npm -v
You should see output like this:
$ node -v v10.4.0 $ npm -v 6.1.0
Make sure that
/usr/local/bin is on your
PATH environment variable or you may see output like this:
$ node -bash: node: command not found
Get the source code from GitHub
I’ve provided source code for every example in the Learning Path, which is available in GitHub.
Open a terminal window, navigate to a directory where you want the code to land, and enter the git clone command:
git clone https://github.com/jstevenperry/IBM-Developer
Then navigate to the Node.js/Course directory. There you will find all of the examples I show throughout the course.
Install VS Code
To follow along with all the tutorials in the Learning Path, you need an editor. I have used VS Code for a couple of years now and really like it. It’s free, open source, and has an active user community with lots of extensions, which means you’re likely to find lots of different extensions for your favorite language, platform, or file format. Plus, VS Code is built on the Electron framework which uses Node.js under the hood.
Of course, you’re free to use whatever editor you like. For the tutorials in this series, I use VS Code, so all the screenshots and video captures you see are of VS Code.
To install VS Code, go to code.visualstudio.com. Your browser should auto-detect your platform and prefill the dropdown with the matching download, or use the dropdown to select any other download you like. However, I recommend you visit the downloads page where you can also obtain the SHA256 checksum for your download. To see the checksum, click the link at the bottom of the page titled “See SHA-256 Hashes”, which will expand showing a list of hashes for each file.
The file that is downloaded will vary by platform. For MacOS, the downloaded file is a ZIP archive containing a MacOS App file. Copy the file to your applications folder, so you can easily locate it later if you like.
If you’re running on Windows, the file is an installer. Instructions for installing it are here.
If you’re running Linux, you can find setup instructions here.
Once you have VS Code installed, go ahead and launch it. You should definitely checkout the Marketplace, which has TONS of plugins.
To install a plugin, click on the Marketplace icon, search for plugin you want, and click Install. Figure 4 shows some of the extensions I have installed:
Running Node.js in VS Code
VS Code plugs into the Node.js runtime, so you can run your code from within VS Code. Follow these steps:
- Navigate to the directory where you cloned the code.
- In VS Code, choose
File > Openand select the
IBM-Code/Node.js/Coursesubdirectory within that location, and click the Open button.
- Click on the File tab, expand the
Unit-2folder and click on
example1.jsto open it in the editor.
- Click the Debug tab.
- Click on the Run button.
Figure 5 – the VS Code debug window.
Now you can run all of the examples in this course either from the command line, or from within VS Code.
In this tutorial, I showed you 3 different ways you can install Node.js and npm, and the advantages and disadvantages of each. Then I showed you how to install VS Code so that you can follow the examples in this learning path.