Node.js, which was developed by Ryan Dahl in 2009 is an open-source runtime environment that reshapes JavaScript’s characteristics and upgrades its functionality. As a result, you can use JavaScript for frontend and backend development, enabling full-stack development solely using JavaScript which helps in developing server-side and networking applications more easily.

Node.js is a platform built on Chrome’s JavaScript runtime for

easily building fast and scalable network applications. Node.js uses an

event-driven, non-blocking I/O model that makes it lightweight and

efficient, perfect for data-intensive real-time applications that run across

distributed devices.

Advantages of Using Node.js

Multiple factors should be considered when you look for the advantages of using a particular platform. Things like the learning curve, development speed, community, and scale can alter the overall balance of benefits.

Here are the main advantages of using Node.js:

● Simplesyntax
● Easylearningcurve
● Abilitytoscalequickly
● Opensourceandflexible
● Cross-platformdevelopment
● Single-languagefull-stackdevelopment ● Real-timecommunication
● Vastandactivecommunity

Node Version Manager (NVM)

Node.js is an open-source project and there are times when we need one version over the other. Suppose we have a stable Production project, which is using the old LTS version. At the same time, we are working on a side-project, which needs the latest Node JS Current version. In this case we must keep on deleting one Node version, working on the project, and returning to the other version for the other project.

This is a very common problem and the best solution is to use nvm (Node.js Version Manager). It lets us switch between different versions of Node without much effort. We will now understand through examples, how easy it is to switch, install and remove node versions on a system.

Option 1

we will show you three different ways of getting Node.js installed on an

Ubuntu

This guide assumes that you are using Ubuntu 22.04. Before you begin,

you should have a non-root user account with

local package index first by typing:

sudo

your system.

To get this version, you can use the

apt package manager. Refresh your

sudo apt update

Then install Node.js:

sudo apt install nodejs -y

node -v

npm npm

sudo apt install npm -y

Check that the install was successful by querying

node

for its version

number:

You’ll also want to install

, the Node.js package manager. You can

do this by installing the

package

This will allow you to install modules and packages to use with Node.js.

Option 2

From your home directory, use

curl

to retrieve the installation script for

your preferred version, making sure to replace

18.x

privileges set up on

with your preferred

version string (if different).

curl -sL https://deb.nodesource.com/setup_18.x -o

nodesource_setup.sh

You can inspect the contents of the downloaded script with

preferred text editor):

vi nodesource_setup.sh

:

sudo apt install nodejs -y

.

vi (or your

Running third party shell scripts is not always considered a best practice,

but in this case, NodeSource implements their own logic in order to

ensure the correct commands are being passed to your package

manager based on distro and version requirements. If you are satisfied

that the script is safe to run, exit your editor, then run the script with

sudo

sudo bash nodesource_setup.sh

The PPA will be added to your configuration and your local package

cache will be updated automatically. You can now install the Node.js

package in the same way you did in the previous section. It may be a

good idea to fully remove your older Node.js packages before installing

the new version, by using

sudo apt remove nodejs npm

Verify that you’ve installed the new version by running:

node -v

npm npm

The NodeSource

nodejs

package contains both the

Option 3 — Installing Node Using the Node Version Manager

NVM (Node Version Manager) is a bash script that allows you to manage multiple Node.js versions per user. NVM lets you install and uninstall any Node.js version.

node

binary and

, so you don’t need to install

separately.

Once wget is installed give the below command to install nvm on the Linux system.

After the command is complete, we will get further commands to properly install nvm on our Linux system.

export NVM_DIR=”$HOME/.nvm”

[ -s “$NVM_DIR/nvm.sh” ] && \. “$NVM_DIR/nvm.sh”

# This loads nvm

[ -s “$NVM_DIR/bash_completion” ] && \. “$NVM_DIR/bash_completion”

# This loads nvm bash_completion

We will run above three commands separately from the terminal.

Once the script is in your PATH, verify that nvm was properly installed by typing:

nvm –version

Next, we will install a node.js version through nvm,To get a list of all Node.js versions that can be installed with nvm, run:

nvm list-remote

The command will print a vast list of all available Node.js versions:

wget -qO-

https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install.sh |

bash

v14.19.2 (LTS: Fermium) v14.19.3 (Latest LTS: Fermium)

v16.14.2 v16.15.0 v17.0.0 v17.0.1

v18.9.1 v18.10.0

(LTS: Gallium)
(Latest LTS: Gallium)

To install the latest available version of Node.js, run:

nvm install node

Once the installation is completed, verify it by printing the Node.js version:

node -v

You can list the installed Node.js versions by typing:

nvm ls

Conclusion

We have shown you three ways to install Node.js and npm on your Ubuntu 22.04 machine. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the Ubuntu or NodeSource repository is easier, the nvm method gives you more flexibility for adding and removing different Node.js versions on a per-user basis.