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

How to get GIT repository URL from source folder

There are several ways to get the link to a GIT repository from the directory... Read more
·1 min. read

How to use PM2 with Node.js

PM2 is a tool for managing Node.js processes, allowing for application... Read more
·2 min. read

Warcraft 3 Cheat Codes

Found this in old files... September 24, 2006 - kodu warcraft... Read more
·1 min. read

© geekrainian.com.