How to install Node.js on Ubuntu 20.04

·2 min. read·

As an example, I use VPS hosting with pre-installed Ubuntu 20.04.3 LTS. Keep in mind that there should be no significant difference for Node.js between the recent versions of Ubuntu.

Download a dist package for 16.x version of Node.js:

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -

Install the package:

sudo apt install -y nodejs

Check if installation were successful:

node --version
npm --version

v16.20.1
8.19.4

Create a new file and make it executable:

sudo nano /home/index.js
sudo chmod 755 /home/index.js

Insert the following code to this file and save:

var http = require('http');
var port = 8080;
http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello world!');
}).listen(port);
console.log(`Server is running on port ${port}`);

To make sure that the app is working properly, use the following command to run it:

node /home/index.js

The console output will be:

Server is running on port 8080

To open the app in the browser, you have to explicitly specify port:

http://127.0.0.1:8080/

Note: If you're using a remote server replace 127.0.0.1 with your server's IP. Also make sure that the port 8080 is free and available.

If everything were okay, you would notice this text in the browser:

Hello world!

Consider using PM2 to run Node.js applications in a more reliable way. Read more in this article.

You may also like

Lineage 2 game disc boxes look and feel
Since the original boxes with Lineage 2 game discs never appeared in the CIS... Read more
·2 min. read
Bitbucket password to clone a repo
Let's assume you already have a repository on Bitbucket, and you need to clone... Read more
·2 min. read
Lineage 2 client update history
Lineage 2 is an online role-playing game developed by NCSOFT. The game... Read more
·3 min. read

© geekrainian.com