Programming in layman’s language can be defined as giving a computer a set of instructions to work on. These instructions are exact in terms of where and how to work on a process. However, computers do not understand the human language (English) but communicate via a machine code known as binary language composed of 0’s and 1’s. It s difficult to translate English to such a Language and this is where Programming Languages come in.

Programming Languages act as interpreters for converting the human language to machine code, this helps a developer to properly send instructions to a computer. With Programming Languages, you can develop a Web, Mobile application, or Game. There are various types of Programming languages that differ in terms of level which translates to how similar they are to the machine code. Low-level languages include the C programming language while high-level languages include, Java and Python programming languages.

Javascript is an object-oriented programming language that works on browsers. It is the most popular scripting language for web pages and can also be used in other non-browser environments like a desktop applications. It is primarily used to make interactive elements that are used on web pages and other applications. It is a text-based programming language that is fast, easy to learn and execute, and enhances the user experience on the client side.
However, Javascript has to be interpreted into machine code by use of a program known as Javascript Engine that executes the Javascript code to be understood by a computer. Different browsers use different interpreters. Common Javascript engines include Google’s V8, Spider Monkey, Chakra for Microsoft Edge and Javascript core for Firefox.

Node.js

Node.js is an open-source, cross-platform runtime environment built on V8 Javascript Engine that is used to execute javascript code. It is designed to build scalable data-intensive and real-time applications like gaming. It is essentially used to build backend services that power client applications like web and mobile applications. Node is easy to get started and can be used in testing and production environments.
An application built with Node.js has a fast response time with fewer lines of code and fewer files compared to other tools and frameworks like Django. Node.js has a node package manager (npm) that contains ready-made packages, modules, and libraries to build applications. Frameworks built for Node.js include; Express JS, Sails, Meteor, etc. Companies that use Node.js include; Netflix, eBay, Uber, etc.

Features of Node.js

  • Uses Javascript everywhere both at the backend and front end increasing productivity.
  • Large ecosystem of open-source libraries which you do not have to build from scratch.
  • Cleaner and more consistent codebase.
  • Uses Asynchronous programming that helps handle large amounts of requests in a short period of time.
  • Fast – Node.js runs on Chrome’s V8 engine that compiles javascript into machine code.
  • Highly scalable and lightweight.

This guide will show you how to Install Node.js CentOS 9|AlmaLinux 9|RHEL 9.

Install Node.js on CentOS 9|AlmaLinux 9|RHEL 9

Update your system packages

sudo dnf update -y

Install build tools required to install native addons from npm.

sudo yum groupinstall 'Development Tools'

We will install releases of Node.js versions 18, 16 (LTS), and 14.

Node.js v18.x

Use the following command to configure and install Node.js.

curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
sudo yum install -y nodejs

Check for the version after installation.

$ node -v
v18.6.0

$ npm -v
8.13.2

Node.js v16.x (LTS)

Use the following command to configure and install Node.js alongside npm.

curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo bash -
sudo yum install -y nodejs

Check for the version after installation.

$ node -v
v16.16.0

$ npm -v
8.11.0

Node.js v14.x

Use the following command to configure and install Node.js with npm

curl -fsSL https://rpm.nodesource.com/setup_14.x | sudo bash -
sudo yum install -y nodejs

Check for the version after installation

$ node -v
v14.20.0

$ npm -v
6.14.17

Test Node.js on CentOS 9|AlmaLinux 9|RHEL 9

To test node.js we are going to implement a simple web server. First, create a javascript file.

vi hello.js

Input the following content.

const http = require('http');
const port = 8080;
const server = http.createServer((req, res) => {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end('Hello from Technixleo!\n');
});
server.listen(port, () => {
  console.log(`Server running at http://127.0.0.1:${port}/`);
});

Save and exit the file. Configure the firewall to accept the Listening port.

sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

Then start the Webserver with the following command

$ node --inspect hello.js
Debugger listening on ws://127.0.0.1:9229/7bf72a9c-3e53-4433-a958-c6059d1c2214
For help, see: https://nodejs.org/en/docs/inspector
Server running at http://127.0.0.1:8080/

Open another tab on the terminal and run the program with the curl while the server is still running.

$ curl http://localhost:8080/
Hello from Technixleo!

You can also test it on a browser by going to http://localhost:8080/

Using PM2 to Manage Node Application

PM2 short for Process Manager 2 is a management tool for Node.js applications with a built-in load balancer. It helps with the basic system administration tasks that involve starting and stopping the applications.

If you don’t have PM2 installed on your system, use the following command to install it.

sudo npm install pm2 -g

To make pm2 auto-boot at server restart:

$ pm2 startup
[PM2] Init System found: systemd
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u technixleo --hp /home/technixleo

The script will differ from yours, Hence copy the appropriate script to enable the pm2 service to start on boot

$ sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u technixleo --hp /home/technixleo
.......
.......
Target path
/etc/systemd/system/pm2-technixleo.service
Command list
[ 'systemctl enable pm2-technixleo' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-technixleo.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-technixleo...
Created symlink /etc/systemd/system/multi-user.target.wants/pm2-technixleo.service → /etc/systemd/system/pm2-technixleo.service.
[PM2] [v] Command successfully executed.

You can list running applications with the following command

pm2 list

To start an application, run the following command.

$ pm2 start hello.js
[PM2] Starting /home/technixleo/hello.js in fork_mode (1 instance)
[PM2] Done.
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 0  │ hello              │ fork     │ 0    │ online    │ 0%       │ 40.9mb   │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘

To stop an application

$ pm2 stop hello.js
[PM2] Applying action stopProcessId on app [hello.js](ids: [ 0 ])
[PM2] [hello.js](0) ✓
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 0  │ hello              │ fork     │ 15   │ stopped   │ 0%       │ 0b       │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘

To restart an application:

$ pm2 restart hello.js
[PM2] Applying action restartProcessId on app [hello.js](ids: [ 0 ])
[PM2] [hello](0) ✓
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 0  │ hello              │ fork     │ 30   │ online    │ 0%       │ 13.2mb   │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘

To delete an application;

$ pm2 delete hello.js
[PM2] Applying action deleteProcessId on app [hello.js](ids: [ 0 ])
[PM2] [hello](0) ✓
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘

To monitor an application in production.

$ pm2 monit

┌─ Process List ───────┐┌──  hello Logs  ──────────────────────────────────────┐
│[ 0] hello     Mem:   ││                                                      │
│                      ││                                                      │
│                      ││                                                      │
│                      ││                                                      │
│                      ││                                                      │
│                      ││                                                      │
│                      ││                                                      │
│                      ││                                                      │
│                      ││                                                      │
│                      ││                                                      │
│                      ││                                                      │
│                      ││                                                      │
│                      ││                                                      │
│                      ││                                                      │
└──────────────────────┘└──────────────────────────────────────────────────────┘
┌─ Custom Metrics ─────┐┌─ Metadata ───────────────────────────────────────────┐
│ Used Heap Size       ││ App Name              hello                          │
│ Heap Usage           ││ Namespace             default                        │
│ Heap Size     8.69   ││ Version               N/A                            │
│ Event Loop Latency   ││ Restarts              0                              │
└──────────────────────┘└──────────────────────────────────────────────────────┘
 left/right: switch boards | up/down/mouse: scroll | Ctrl-C: exit         To go

Uninstall Node.js

To uninstall Node.js, use the following commands. This will remove node.js and the repository. You will have to delete the files manually.

sudo yum remove nodejs
sudo rm -r /etc/yum.repos.d/nodesource*.repo
sudo yum clean all

Conclusion

From this guide, we have successfully installed Node.js on CentOS 9|AlmaLinux 9|RHEL 9. Node.js is an Open-source runtime environment for executing Javascript code. Node.js allows your programs written in Javascript to be executed on the server to create dynamic elements.

Also check out below guides:

LEAVE A REPLY

Please enter your comment!
Please enter your name here