Learn Node.js with real applications

Installation and Configuration

Installation

Typically, to install any technology, the first step is to visit its official website, go to the download section, select the operating system, and follow the installation instructions. This will work as long as only one version of the technology or programming language is used on the system for all projects. However, it is likely that this will not always be the case, as over time, new versions are released, and you may need to support a project that runs on a specific or older version of Node.js. Therefore, in this first section, we will install it in the traditional way.

To install Node.js, follow these steps:

  1. Visit the official Node.js website.
  2. The website will automatically detect the operating system.
  3. Click on the “Download Node.js (LTS)” link.
  4. Follow the installer’s instructions.

To check the installed version:

  1. Open the Terminal.
  2. Run the command:
node -v

It is very tedious to have to install and uninstall different versions, therefore the most recommended strategy is to install a version manager, specifically for Node.js.

Node.js Version Manager

A version manager, as its name suggests, allows you to manage different versions of the technology or programming language on the system. Among the operations you can then perform are: installing new versions, uninstalling existing versions, selecting which version to use, and setting the default version on the system.

If you already have a version of Node.js installed on the system, it is not necessary to uninstall it to use the version manager, as the version manager will include it among the options that can be selected.

Installing the Version Manager

There are different version managers for Node.js. One of the most popular is nvm, a utility that allows you to manage different versions of Node.js on the system, providing the ease of selecting the necessary version for each project.

Installation on Mac OS

Installing Homebrew

Mac OS has a popular program for managing different packages called brew. To install it, open the Mac OS terminal and run the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

During the installation process, if necessary, the installer will ask for your password to establish the necessary permissions to complete the installation successfully.

Once the installation is complete, you can check the installed version in the terminal with the following command:

brew -v

Detailed information about the process and potential issues can be found on the official Homebrew page.

Installing nvm

Once brew is installed, proceed to install nvm:

brew install nvm

After the installation, check the installed version in the terminal with the following command:

nvm -v

It is likely that nvm will require additional configuration, so you should follow the recommendations provided once the installation process is complete.

Additionally, it is recommended to restart the terminal session.

Detailed information about the installation process and potential issues can be found on the official nvm repository.

Installation on Windows

Installing cmder

Versions prior to Windows 10 offer a very basic terminal; however, the cmder project offers a terminal with many options, including most of the Linux system commands, git, and many other tools.

To install it, follow these steps:

  • Visit the official cmder page.
  • Click on the “Download Full” link.
  • Unzip the downloaded file to a known location such as Program Files.
  • Create a shortcut on the Desktop for the cmder.exe file.
  • It is best to run the program with Administrator permissions.

Detailed information about the installation process and potential issues can be found on the official cmder repository.

Installing nvm for Windows

The nvm utility is not available for Windows, but there is a very similar project that fulfills the same purpose.

To install it, follow these steps:

  • Visit the official nvm for Windows project page.
  • Click on the “Download Now” link.
  • Then, in the list, click on the “nvm-setup.zip” link for the latest version.
  • Unzip the downloaded file.
  • Run the nvm-setup.exe installer.

Detailed information about the installation process and potential issues can be found on the official nvm for Windows repository.

Managing Versions

Once nvm is installed, install the stable version of Node.js by running the following command in the terminal:

nvm install stable

This is the output of the previous command:

Downloading and installing node v22.1.0...
Downloading https://nodejs.org/dist/v22.1.0/node-v22.1.0-darwin-arm64.tar.xz...
####################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v22.1.0 (npm v10.7.0)

After the installation process is complete, verify the installed and active version on the system with the following command:

node -v

This is the output of the previous command:

v22.1.0

Node.js Versions and Long Term Support (LTS)

Different Node.js versions can be verified on the following page: Node JS Release; there you can see the lifecycle of each version and the status of Long Term Support (LTS), indicating when maintenance and security updates are performed for each version.

As an example, install a previous version:

nvm install 20

After the installation process, verify the installed version in the terminal with the following command:

node -v

As observed, the recently installed version is now the active version in this terminal session. If a new terminal session is opened, the version set as default in the system will be selected. This means that different Node.js versions can be selected, each in a different terminal session. This is a huge advantage when running multiple projects simultaneously that need different versions.

To list all the versions installed locally by nvm, run the following command:

nvm ls

This is the output of the previous command:

       v18.19.1
       v20.11.1
->      v22.1.0
         system
default -> 22 (-> v22.1.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v22.1.0) (default)
stable -> 22.1 (-> v22.1.0) (default)
lts/* -> lts/iron (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.20.2 (-> N/A)
lts/iron -> v20.12.2 (-> N/A)

Here are some notes about the above listing:

  • The -> symbol indicates which version is currently selected in the terminal session.
  • The version selected by default with a new terminal session is marked with default.
  • The stable version is stable.
  • LTS support versions are marked with the lts prefix.

iojs was once a fork of the Node.js project, but fortunately, the developers reunited and released version 4 of Node.js; hence the jump from version 0.12 to version 4.

Once different versions are installed via nvm, you can select the version you want to work with. In this case, select the stable version with the following command:

nvm use stable

The parameter after the use command is the version you want to select.

It is possible to select the system-installed version with nvm use system.

To set a version as the default for each new terminal session, for example, the stable version, run the following command:

nvm alias default stable

It is possible to create more aliases with nvm for different installations.

To list all the versions that nvm can install on the system, run the following:

nvm ls-remote

Automatically Selecting the Corresponding Version for Each Project

It would be very tedious to select the necessary version every time you start working on a project (if it is not the default), and it is possible to confuse or forget which version is needed.

For this, nvm has a solution: create a file named .nvmrc in the root directory of the project, and in its content, place only the version number of Node.js that corresponds to that project. Thus, when you navigate to the project directory through the terminal, nvm will automatically select the version specified in the .nvmrc file, for example:

22

The content of the file only has the version number of Node.js to be used in that project; in this case, it is the version prior to the default-installed version.