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 upgrade
Step 2: Then finally install Node.js by using the below-mentioned command:
The installation process will take some time, once the installation is finished you can verify it by using the below-mentioned version command:
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:
Then inside the file, write the JavaScript code, here I wrote the code to display the message “Hello followers!”:
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:
To display the message, go to the browser and type the below-written text:
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:
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.