Debian

How to Install and Use Node.js on Debian 11

If you are looking for an open-source Java Script environment to run on your Debian OS, then Node.js can be a good choice. Node.js is a cross-platform environment used to develop online applications and is very efficient for both frontend and backend programming. It is used to create server-side applications and is ideal for data-intensive applications. Node.js can create dynamic content for the page, open and modify the files on the server, can delete and edit data in the database.

This article is a detailed guide about installing and using Node.js on Debian.

How to Install Node.js on Debian 11

To install Node.js on Debian 11, follow the below-written steps:

Step 1: Firstly update/upgrade the repository by using the below-written command:

sudo apt update

sudo apt upgrade

Step 2: Then finally install Node.js by using the below-mentioned command:

sudo apt install nodejs

The installation process will take some time, once the installation is finished you can verify it by using the below-mentioned version command:

node --version

Use Node.js on Debian

After installing Node.js on Debian you can create and interpret .js (JavaScript) files; examplefile.js is a node.js server file that is created in the below-mentioned steps:

Step 1: Using a nano editor create a .js file, by following the below-written command:

nano examplefile.js

Then inside the file, write the JavaScript code, here I wrote the code to display the message “Hello followers!”:

const http = require('http');

const host = '<localhost/IP>';

const port = 3000;

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

res.statusCode = 200;

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

res.end('Hello followers!');

});

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

console.log('The web server is running at http://%s:%s',host,port );

});

Save this file using “Ctrl+X”, add “Y” and press Enter.

Then finally run the .js file by using the below-mentioned node command:

node examplefile.js

To display the message, go to the browser and type the below-written text:

<IP-address>:3000

Or you can copy the above command output address and paste it into the browser the output will display the message on port 3000.

Note: You can find the Debian IP address from the hostname -I command.

Remove Node.js from Debian

To remove Node.js from Debian, run the below-written command:

sudo apt remove nodejs

Conclusion

To install Node.js on Debian, update the system and install it from the official Linux repository by using the “apt install” command. Once Node.js is installed, you can use it to create multiple JavaScript applications and run them on the browser via port 3000. To run the Node.js file just type the node command followed by the file name.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.