How to download and install Node.js

350
0

Installing Node.js is by no means complicated, however, if you’re keen on learning to code Node.js applications using either JavaScript or TypeScript, there is one preferred way of installing Node.

Node evolves rapidly. So much so that there are various major versions of Node.js which are being actively developed, supported and maintained. Check this list out for a quick summary of the different roadmaps for the major versions of Node: https://nodejs.org/en/about/releases/

The fact that there are so many different versions to chose from means that if you follow a tutorial for any given project, chances are each tutorial may use different versions of node and you many run into issues because the code uses libraries or methods which are not supported by the version of node you installed.

How do we fix this? Enter NVM.

NVM (Node Version Manager) is a tool which allows you to install multiple versions of Node at the same time on your computer. Then, it’s simply a matter to activating a certain version the project uses, and you’re ready to go.

This greatly simplifies managing node versions by project, as in addition, you can specify a node version to be used by default per project, so switching versions is done automatically by NVM.

How does NVM work?

NVM is a tool that manages the different versions of Node.js installed on your computer. This means that we must first install NVM, and then use NVM to install the different versions of Node.js you need.

Installing NVM

Install NVM on Windows:

Download the nvm-setup.exe from the official repository: https://github.com/coreybutler/nvm-windows/releases

Use the installer to guide you through the process:

Once installation is complete, open the PowerShell and type nvm ls to check if it’s working.

Once you’ve completed this, go to the Install Node step.

Installing NVM on Mac:

If you have homebrew on your Mac, the process is as easy as typing the following command into your terminal:

brew install nvm

If, on the other hand, you don’t have homebrew installed, you have 2 options:

  • Install homebrew first, and then use homebew to install NVM with the above command
  • Or, install nvm without homebrew

If you choose to install homebrew, just paste this line into your terminal:

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

If you want to install NVM without homebrew, use this command instead:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install.sh | bash

Once you’re done with either option, type in nvm ls to check it’s working:

Installing NVM on Linux:

The process is similar to a Mac, however, some versions of Linux come with different pre-built libraries so you may need to do a little trial and error see which command works for you.

To install NVM on linux, try which one of these 2 commands works for you:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

You will need to restart your terminal for the nvm ls command to work.

Installing Node.js

Once NVM is installed, installing Node.js is a breeze. By default, I’d always install the latest LTS (Long Term Support) version of Node.js with this simple command:

nvm install --lts

If you need a specific version of node, you can simply tell NVM which version like this:

# Installs NodeJS version 16
nvm install 16

# Installs NodeJS version 18
nvm install 18

You can install as many as you want, and they will not conflict each other. This is because while NVM installs different node versions, only 1 is always “active”. You can change the version you want to use in 2 different ways.

Using an NVM command:

nvm use 16

Or, you can make NVM automatically change the node version per project. You do this by creating a .nvmrc file inside of the root directory of your project and simply adding the version number you want to use in the file.

# Create a .nvmrc file with the version number as the content of the file (16 in this case)
echo '16' > .nvmrc

Now when you cd into the project, NVM will automatically change the node version to 16.

Finally, if you want to check what versions you have installed ready to use on your computer, you can run this command:

nvm ls

Leave a Reply

Your email address will not be published. Required fields are marked *