NodeJS

Installing Node.js on Debian 10

Node.js is a server side JavaScript runtime. Node.js is open source and cross platform. Node.js runs on Linux, Windows and macOS. It is mainly used to develop software APIs and networking applications.  In this article, I am going to show you how to install Node.js on Debian 10 and how to run a simple Node.js program on Debian 10. So, let’s get started.

Installing Node.js 10 LTS:

Node.js 10.x is the latest LTS version of Node.js at the time of this writing. Luckily, it is available in the official package repository of Debian 10. So, you can easily install it using the APT package manager on your Debian 10 machine.

First, update the APT package repository cache with the following command:

$ sudo apt update

The APT package repository cache should be updated.

Now, install Node.js from the official Debian 10 package repository with the following command:

$ sudo apt install nodejs

Now, to confirm the installation, press Y and then press <Enter>.

The APT package manager will download and install all the required packages.

Node.js 10.x should be installed.

As you can see, the Node.js version installed from the official package repository is v10.15.2.

$ node --version

Node.js has its own package repository to help you out in your work. Luckily, Debian 10 packages a lot of common and stable Node.js packages. You can easily download them from the official package repository of Debian 10. The Node.js Debian 10 package names start with node-*

For example, I searched for express.js Node.js package on the official Debian 10 package repository. As you can see, the package exists. The express-generator package exists as well. The package names are node-express and node-express-generator in Debian 10. You can easily use the APT package manager to install these packages and use them in Node.js 10.

I also searched for the Node.js package bluebird. It exists as well.

If you rather want to install Node.js packages using NPM, then you have to install NPM from the official package repository of Debian 10 with the following command:

$ sudo apt install npm

Now, confirm the installation by press Y followed by <Enter>.

The APT package manager will download and install all the required packages.

At this point, NPM should be installed.

As you can see, the NPM version installed from the Debian 10 package repository is 5.8.0.

The Node.js packages that are in the Debian 10 package repository are very stable and well tested. You can use them if you want.

Installing Node.js 12:

At the time of this writing, the latest version of Node.js is version 12.x. But, it is not available in the official package repository of Debian 10. You have to install it manually from the official package repository of Node.js.

Before you install Node.js 12.x, you have to install some dependency packages from the Debian 10 package repository.

First, update the APT package repository cache with the following command:

$ sudo apt update

The APT package repository should be updated.

Now, install the dependency packages build-essential and curl with the following command:

$ sudo apt install build-essential curl

Now, press Y and then press <Enter> to confirm the installation.

The dependency packages should be installed.

Now, add the official Node.js 12.x package repository with the following command:

$ curl -sL https://deb.nodesource.com/setup_12.x</a> | sudo bash -

The Node.js 12.x package repository should be added and the APT package repository cache should be updated.

Now, install Node.js 12.x with the following command:

$ sudo apt install nodejs

The APT package manager should download and install all the required packages.

Node.js 12.x should be installed.

As you can see, I am running Node.js 12.7.0.

$ node --version

Node.js installed from the official Node.js package repository installs NPM by default. As you can see, I am running NPM 6.10.0.

Writing Your First Node.js Program:

In this section, I am going to show you how to write your first Node.js program.

First, create a project directory (let’s call it ~/hello-node) as follows:

$ mkdir ~/hello-node

Now, navigate to the project directory ~/hello-node as follows:

$ cd ~/hello-node

Now, create a new file welcome.js in the project directory ~/hello-node and type in the following lines of code in the welcome.js file.

let http = require('http');
const PORT = 8080;
 
let server = http.createServer((req, res, next) => {
res.writeHead(200, {
'Content-Type': 'text/html'
});
 
res.end('<h1>Welcome to LinuxHint!</h1>');
});
 
server.listen(PORT, () => {
console.log("Visit http://localhost:" + PORT + " from your web browser.");
});

The final welcome.js program looks as follows:

Now, to run the Node.js program welcome.js, run the following command:

$ node welcome.js

As you can see, the welcome.js program is running.

Now, visit http://localhost:8080 from your web browser and you should see a welcome message as shown in the screenshot below.

So, that’s how you install Node.js on Debian 10 and run your first Node.js program. Thanks for reading this article.

About the author

Shahriar Shovon

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.