Node.js is an open-source, cross-platform runtime environment for developing server-side and networking applications built on Chrome’s V8 JavaScript engine. It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.

NPM(Node Package Manager) is the default package manager for Node.js. It comes installed when you install Node.js. You can do almost everything with it since it provides access to thousands of packages that can be downloaded and installed in your application’s project directory through the command-line interface.

In this article, we will learn what Nodejs is and how to install it on a Linux machine using a non-root user account.

Prerequisites

This post assumes that you have a basic understanding of Linux, know how to use the shell, can log in and query your machine using either SSH or Terminal, and most importantly that your computer has a non-root user with sudo rights.

Update your system

It’s important to make sure your system is up to date by running the following apt commands. This will update and upgrade your system, install required tools for compiling source code and packages under the Linux environment.

sudo apt update
sudo apt upgrade -y
sudo apt install build-essential -y

The output should look like this:

<img alt="update ubuntu" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/09/echo/update_ubuntu.png614891e7d322d.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="386" loading="lazy" src="data:image/svg xml,” width=”659″>

<img alt="update ubuntu" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/09/echo/update_ubuntu_2.png614891e7f3c44.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="386" loading="lazy" src="data:image/svg xml,” width=”659″>

Install Node.js on Debian 11.

Install Node.js using NodeSource PPA

You can install Node.js through the official Debian repositories but the version might be quite old for your project’s requirements. Therefore, you should consider using the PPA (personal package archive) for node source, maintained by Nodesource. This has a lot more versions of Nodejs as compared to the official Ubuntu repositories.

First, we will need to install the PPA in order to install Node.js 16. From your home directory, use the cURL command:

cd
curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -

The output should look like this:

<img alt="add PPA nodejs" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/09/echo/add_ppa_nodejs.png614891e825346.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="386" loading="lazy" src="data:image/svg xml,” width=”659″>

Next, run the apt-get update and then install nodejs as follows:

sudo apt-get update
sudo apt install nodejs -y

The output should look like this:

<img alt="nstall nodejs ubuntu" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/09/echo/install_nodejs_ubuntu.png614891e848b95.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="343" loading="lazy" src="data:image/svg xml,” width=”750″>

This will also install npm. By default, both of them will be installed under /usr/bin . To check the installed version of node and npm, run the following command:

node -v
npm -v

The output should be like this:

<img alt="check nodejs version" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/09/echo/check_nodejs_version.png614891e87532b.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="343" loading="lazy" src="data:image/svg xml,” width=”750″>

Install Node.js using NVM

An alternative method of installing Nodejs is through NVM. It stands for “Node Version Manager”. The idea behind it is that you have a command-line tool that installs and manages multiple releases of Node.js on your system. This way, if one version has an issue with your project, you can simply switch to another without worrying about compatibility issues.

You can download NVM for your system. Since we are using Debian 11, the command would be like this:

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

The output should be like this:

<img alt="Download Node Version Manager" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/09/echo/download_node_version_manager.png614891e8a579c.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="343" loading="lazy" src="data:image/svg xml,” width=”750″>

Run the following command to check nvm version

nvm --version

The output should be like this:

<img alt="nvm –version" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/09/echo/nvm_–version.png614891e8e0dcc.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="386" loading="lazy" src="data:image/svg xml,” width=”659″>

You can check all available node versions using the following command:

nvm list-remote

The output should be like this:

<img alt="check all available node versions " data-ezsrc="https://kirelos.com/wp-content/uploads/2021/09/echo/check_all_available_node_versions.png614891e914a2b.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="386" loading="lazy" src="data:image/svg xml,” width=”659″>

You can install any version using the following command :

nvm install 

In this guide we will go for v16.7.0:

nvm install v16.7.0

The output should be like this:

<img alt="nvm install node js" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/09/echo/nvm_install_node_js.png614891e9431d1.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="386" loading="lazy" src="data:image/svg xml,” width=”659″>

If you have installed multiple versions of Node.js, list them like this:

nvm ls

This command will list all the installed node versions with their respective version numbers. The output should be something like this:

<img alt="nvm list" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/09/echo/nvm_list.png614891e96cc9f.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="386" loading="lazy" src="data:image/svg xml,” width=”659″>

To activate a specific node version, run the following command:

nvm use 16.6.2

<img alt="nvm use " data-ezsrc="https://kirelos.com/wp-content/uploads/2021/09/echo/nvm_use.png614891e9951b3.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="386" loading="lazy" src="data:image/svg xml,” width=”659″>

Test the Node.js server

Let us create a simple web server using Node.js.

Create a file (server.js) in the directory where you want to keep your application’s code

sudo nano server.js

Copy-paste the following code into it:

const http = require('http');

const hostname = '0.0.0.0';

const port = 3000;

const server = http.createServer((req, res) => {

res.statusCode = 200;

res.setHeader('Content-Type', 'text/plain');

res.end('Hello World, howtoforge');

});

server.listen(port, hostname, () => {

console.log(`Server running at http://${hostname}:${port}/`);

});

Save this file when you are done.

On the command line, go to the directory in which you saved your file (server.js) and run this command:

node server.js

Now open any browser of your choice and type http://your_server_ip:3000. You will get an HTML page as your website’s welcome page, which is nothing but a simple web server created using Node.js.

<img alt="welcome html page nodejs" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/09/echo/welcome_html_page_nodejs_1.png614891e9c86d9.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="84" loading="lazy" src="data:image/svg xml,” width=”565″>

That’s it! You have successfully installed Node.js on Debian 11 and successfully written a simple web server using it. You can learn more about Node.js from its official documentation page.

Conclusion

In this article, we explained how to install Node.js on Debian 11 and using different methods available. We also created a simple web server using Node.js and checked if it’s working fine. If you have any questions, feel free to leave a comment below.