How to install Node.js & NPM on Debian 11

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:

update ubuntu

update ubuntu

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:

add PPA nodejs

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:

nstall nodejs ubuntu

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:

check nodejs version

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:

Download Node Version Manager

Run the following command to check nvm version

nvm --version

The output should be like this:

nvm --version

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

nvm list-remote

The output should be like this:

check all available node versions

You can install any version using the following command :

nvm install <version>

In this guide we will go for v16.7.0:

nvm install v16.7.0

The output should be like this:

nvm install node js

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:

nvm list

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

nvm use 16.6.2

nvm use

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.

welcome html page nodejs

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.

Share this page:

1 Comment(s)