October 2, 2022

Getting Started With Node.js - Part 1

Getting Started With Node.js - Part 1

Installing node on your dev system is very easy but it can be a bit tricky because node moves so fast, with 2 - 3 current versions of node a year. Because of this, I always recommend that you use Node Version Manager - nvm.

Contents


1. What is NVM
2. Install NVM
3. How to Check NVM is working
4. Install Node through NVM
5. Changing Node version
6. Changing the default Node version

What is NVM

Nvm is an open source version manager that lets you maintain multiple versions of node on your computer at once. Installing the newest version is quick and easy, as is returning to an older version. This is particularly useful when managing updates. Even if you are a beginner, you may not see the value now, but in 6 months time it could be very very useful.

The nvm repo can be found on github here:
https://github.com/nvm-sh/nvm

The documentation is quite long and detailed (which is good) but the tldr is this:

Install NVM

Most computers will come with either curl or wget installed. Use whichever works for you. If neither work you'll need to check how to install curl or wget on your system.

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

or

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

This will produce an output like this:

2. The installer automatically adds the correct environment variables. The easiest way to get it working is to close and reopen the terminal window.

How to Check NVM is working

To check nvm is installed correctly, use the list command in your new terminal window.

nvm ls

You should get an output that looks like this:

Thats it you have now installed nvm!

Install Node through NVM

In order to use nvm you need to install a version of node. The command below will give you a list of all the versions of node that are available to install.

nvm ls-remote

This is a long list and can be quite confusing. Usually I prefer to check the node website. They keep a link that links you to their release schedule on github https://github.com/nodejs/release#release-schedule. The release schedule will look something like this:

Node release schedule as of October 2022

The safest version of node to use is the version where status is 'Active LTS'. In the example above, that would be 16.x
If you are working on a longer project you may want to use version 18.x so that when you release your project, 18.x will be the Active LTS version.

For this demonstration we will install both:

nvm install 16

This will install the latest veresion of node 16 (currently 16.17.1) and make it the default version on your system.

Next install node 18

nvm install 18

Finally run

nvm ls

This shows that
1) Node 16.17.1 is installed
2) Node 18.10.0 is installed
3) The currently active version of node is 18.10.0
4) The default version of node is 16.17.1

We can confirm this by asking node what version it is

node --version

Changing Node version

Chaning between the 2 version of node is now super easy:

nvm use 16

You can also change the default node version by setting the default alias

nvm alias default 18

Now when you open a new terminal window, the active version of Node will be version 18

What Next

That's it! You now have node running on your computer and can easily switch between versions.
Node comes with a helpful package manager called npm.
In part 2 we'll find out how to use npm to start a new project.