{"id":139142,"date":"2020-12-29T16:42:16","date_gmt":"2020-12-29T16:42:16","guid":{"rendered":"https:\/\/onet.com.vn\/installing-node-js-on-debian-10.html"},"modified":"2020-12-29T16:42:16","modified_gmt":"2020-12-29T16:42:16","slug":"installing-node-js-on-debian-10","status":"publish","type":"post","link":"https:\/\/onet.com.vn\/installing-node-js-on-debian-10\/","title":{"rendered":"Installing Node.js on Debian 10"},"content":{"rendered":"\n
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.\u00a0 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\u2019s get started.<\/p>\n

Installing Node.js 10 LTS:<\/h2>\n

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.<\/p>\n

First, update the APT package repository cache with the following command:<\/p>\n

\n
$ <\/span>sudo<\/span> apt update<\/div>\n<\/div>\n

<\/p>\n

The APT package repository cache should be updated.<\/p>\n

<\/p>\n

Now, install Node.js from the official Debian 10 package repository with the following command:<\/p>\n

\n
$ <\/span>sudo<\/span> apt install<\/span> nodejs<\/div>\n<\/div>\n

<\/p>\n

Now, to confirm the installation, press Y<\/strong> and then press <Enter><\/strong>.<\/p>\n

<\/p>\n

The APT package manager will download and install all the required packages.<\/p>\n

<\/p>\n

Node.js 10.x should be installed.<\/p>\n

<\/p>\n

As you can see, the Node.js version installed from the official package repository is v10.15.2.<\/p>\n

\n
$ <\/span>node –version<\/span><\/div>\n<\/div>\n

<\/p>\n

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-*<\/strong><\/p>\n

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<\/strong> and node-express-generator<\/strong> in Debian 10. You can easily use the APT package manager to install these packages and use them in Node.js 10.<\/p>\n

<\/p>\n

I also searched for the Node.js package bluebird. It exists as well.<\/p>\n

<\/p>\n

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:<\/p>\n

\n
$ <\/span>sudo<\/span> apt install<\/span> npm<\/div>\n<\/div>\n

<\/p>\n

Now, confirm the installation by press Y<\/strong> followed by <Enter><\/strong>.<\/p>\n

<\/p>\n

The APT package manager will download and install all the required packages.<\/p>\n

<\/p>\n

At this point, NPM should be installed.<\/p>\n

<\/p>\n

As you can see, the NPM version installed from the Debian 10 package repository is 5.8.0.<\/p>\n

<\/p>\n

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.<\/p>\n

Installing Node.js 12:<\/h3>\n

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.<\/p>\n

Before you install Node.js 12.x, you have to install some dependency packages from the Debian 10 package repository.<\/p>\n

First, update the APT package repository cache with the following command:<\/p>\n

\n
$ <\/span>sudo<\/span> apt update<\/div>\n<\/div>\n

<\/p>\n

The APT package repository should be updated.<\/p>\n

<\/p>\n

Now, install the dependency packages build-essential<\/strong> and curl<\/strong> with the following command:<\/p>\n

\n
$ <\/span>sudo<\/span> apt install<\/span> build-essential curl<\/div>\n<\/div>\n

<\/p>\n

Now, press Y<\/strong> and then press <Enter><\/strong> to confirm the installation.<\/p>\n

<\/p>\n

The dependency packages should be installed.<\/p>\n

<\/p>\n

Now, add the official Node.js 12.x package repository with the following command:<\/p>\n

\n
$ <\/span>curl -sL<\/span> https:\/\/<\/span>deb.nodesource.com\/<\/span>setup_12.x<\/<\/span>a><\/span> |<\/span> sudo<\/span> bash<\/span> –<\/div>\n<\/div>\n

<\/p>\n

The Node.js 12.x package repository should be added and the APT package repository cache should be updated.<\/p>\n

<\/p>\n

Now, install Node.js 12.x with the following command:<\/p>\n

\n
$ <\/span>sudo<\/span> apt install<\/span> nodejs<\/div>\n<\/div>\n

<\/p>\n

The APT package manager should download and install all the required packages.<\/p>\n

<\/p>\n

Node.js 12.x should be installed.<\/p>\n

<\/p>\n

As you can see, I am running Node.js 12.7.0.<\/p>\n

\n
$ <\/span>node –version<\/span><\/div>\n<\/div>\n

<\/p>\n

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.<\/p>\n

<\/p>\n

Writing Your First Node.js Program:<\/h3>\n

In this section, I am going to show you how to write your first Node.js program.<\/p>\n

First, create a project directory (let\u2019s call it ~\/hello-node<\/strong>) as follows:<\/p>\n

\n
$ <\/span>mkdir<\/span> ~\/<\/span>hello-node<\/div>\n<\/div>\n

<\/strong><\/p>\n

Now, navigate to the project directory ~\/hello-node<\/strong> as follows:<\/p>\n

\n
$ <\/span>cd<\/span> ~\/<\/span>hello-node<\/div>\n<\/div>\n

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

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

The final welcome.js<\/strong> program looks as follows:<\/p>\n

<\/p>\n

Now, to run the Node.js program welcome.js<\/strong>, run the following command:<\/p>\n

\n
$ <\/span>node welcome.js<\/div>\n<\/div>\n

<\/p>\n

As you can see, the welcome.js<\/strong> program is running.<\/p>\n

<\/p>\n

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

<\/p>\n

So, that\u2019s how you install Node.js on Debian 10 and run your first Node.js program. Thanks for reading this article.<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"

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.\u00a0 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 […]<\/p>\n","protected":false},"author":1,"featured_media":139143,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[156],"tags":[],"_links":{"self":[{"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/posts\/139142"}],"collection":[{"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/comments?post=139142"}],"version-history":[{"count":0,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/posts\/139142\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/media\/139143"}],"wp:attachment":[{"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/media?parent=139142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/categories?post=139142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/tags?post=139142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}