Install Node.js, npm, and VS Code
Get started with Node.js installation
This tutorial shows you 3 different ways to install the Node.js runtime and the Node.js package manager, npm: 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, and 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 Node.js Downloads page and run the installation program. This approach is ideal for Windows users, but it also 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 following table:
|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 their original release, during which time the releases go from current (in the last 6 months) to active LTS (18 months, minor revisions only) to maintenance LTS (12 months, patch releases only). This means that you can be sure that an LTS release is stable for a considerable period of time.
If you want to know more about the Node.js release process, check out the Node.js 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 this tutorial, I use version 16 because, at the time of this writing, it is the current LTS release. You probably want to do the same to ensure you can follow along.
Download the installation package from the Node.js project website
Go to the Node.js Downloads page and click on the installer that matches your platform.
As I said in the previous section, I use version 16, and this is reflected in all instructions in this section, including the video. I suggest you use version 16 (or whatever is the latest 16.x.y release at the time).
Once you have downloaded the installer to your computer and you have verified the checksum matches, launch the installer. For example, on my Mac, I double-click on the Package installer program (called node-v16.13.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 the Node.js Installation help from the Node.js GitHub Wiki for more detailed instructions.
Now, skip to the 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 can 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, for example), I recommend you use nvm (See the Use nvm to install Node.js on MacOS and Linux only section 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 show you in the next section).
To learn more about Homebrew and how to install it, check out the Homebrew 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, because 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 show you later in this section).
To install the latest Node.js version 16, enter the
brew install node@16 command. You’ll see output like this:
$ brew install node@16 Updating Homebrew... ==> Auto-updated Homebrew! Updated 1 tap (homebrew/core). No changes to formulae. ==> Downloading https://homebrew.bintray.com/bottles/node-16.1.0.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring node-16.1.0.high_sierra.bottle.tar.gz ==> Caveats Bash completion has been installed to: /usr/local/etc/bash_completion.d ==> Summary 🍺 /usr/local/Cellar/node/16.1.0: 6,793 files, 59.6MB $
Now, skip to the Verify the Installation section to confirm that Node.js and npm were installed correctly.
Tip: Homebrew always installs the current version of Node.js when you use the command as I’ve shown it here. 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. 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
With Homebrew, you can have multiple major versions of Node.js and npm installed at a time, but only one can be active. If you want to switch between them, you have to “unlink” the actively linked version and “link” the one you want to use.
On my Mac, this means that 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, because 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 supported only on MacOS and Linux, but there are 2 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 nvm Important Notes.
Installing nvm is a snap. Go to the creationix GitHub repo and scroll down to the Installation section (or go to the nvm GitHub page. Copy the install command and paste it into a Terminal window or command prompt:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.39.0/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 need to rerun your profile script using your favorite method for doing so. I’m lazy, so I 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 v0.39.0
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 (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 16 (which at the time of this writing is 16.13.0):
$ nvm install 16 Downloading and installing node v16.13.0... Downloading https://nodejs.org/dist/v16.13.0/node-v16.13.0-darwin-x64.tar.gz... ######################################################################## 100.0% Computing checksum with shasum -a 256 Checksums matched! Now using node v16.13.0 (npm v6.1.0) $ node -v v16.13.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 could see output like this:
$ node -bash: node: command not found
Get the source code from GitHub
In GitHub, I have provided source code for every example in this tutorial.
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 can find all of the examples I show throughout the tutorial.
Install VS Code
To follow along with all the tutorials in this 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 the Visual Studio Code page. Your browser should auto-detect your platform and prefill the drop-down list with the matching download, or you can use the drop-down list to select any other download you like. However, I recommend you visit the Download Visual Studio Code, where you can also obtain the SHA256 checksum for your download. To see the checksum, click the link at the bottom of the “See SHA-256 Hashes” page, which will expand and show a list of hashes for each file.
The file that is downloaded varies 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 on the VS Code Setup for Windows page.
If you’re running Linux, you can find setup instructions VS Code Setup for Linux page.
Once you have VS Code installed, go ahead and launch it. You should definitely check out the Marketplace, which has TONS of plug-ins.
To install a plug-in, click the Marketplace icon, search for the plug-in you want, and click Install. Figure 1 shows some of the extensions I have installed:
Figure 1. Installed extensions
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 > Open, select the
IBM-Code/Node.js/Coursesubdirectory within that location, and click Open.
- Click the File tab, expand the
Unit-2folder and click on
example1.jsto open it in the editor.
- Click the Debug tab.
- Click Run.
Figure 2. The VS Code debug window
Now you can run all of the examples in this tutorial either from the command line, or from within VS Code.
In this tutorial, I showed you 3 different ways that you can install Node.js and npm with 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.