Node.js is an open-source, server-side runtime environment built on Chrome’s V8 JavaScript engine. It allows developers to execute JavaScript code outside of a web browser, enabling them to create powerful and scalable web applications. Here are some key benefits of Node.js:

  • JavaScript ecosystem integration: It seamlessly integrates with various front-end JavaScript frameworks and libraries such as React, Angular, or Vue.js. This facilitates code sharing, enables isomorphic applications (where code can run on both the server and client), and promotes efficient full-stack JavaScript development.
  • Extensive package ecosystem: Node.js has a vast ecosystem of reusable modules and packages available through its package manager, npm (Node Package Manager). This expansive collection of modules enables developers to easily integrate various functionalities into their applications, saving development time and effort.
  • Scalability: Node.js’s non-blocking, event-driven architecture makes it highly scalable. It can handle many concurrent connections with low resource consumption, making it suitable for applications that require high scalability, such as social media platforms or real-time collaboration tools.
  • Asynchronous and event-driven: Node.js operates on a non-blocking, asynchronous model. This means it can handle concurrent connections and execute multiple tasks without waiting for each one to complete. As a result, Node.js is highly efficient for handling a large number of concurrent requests, making it ideal for building real-time applications, chat systems, or streaming applications.
  • JavaScript on the server side: Node.js brings JavaScript to the server side, allowing developers to use a single programming language (JavaScript) for both client-side and server-side development. This streamlines the development process and facilitates code sharing between the front end and back end.

What’s new in Node.js 20 LTS

Node.js 20 is the latest stable version that was made available on April 18, 2023. This new version comes after Node.JS 19 a non-LTS version which has been available for the last 6 months. Node.js 20 will be supported and will be supported until April 2026.

Node.js 20 ships nifty features and improvements that include:

  • Node.js Permission Model: Rafael Gonzaga has contributed the Permission Model feature to Node.js, specifically in version 20. This experimental mechanism offers developers the ability to control and limit access to various resources during the execution of their programs. By leveraging this feature, developers can impose restrictions on activities like file operations, child process spawning, system operations, and worker thread creation. This allows for enhanced security and more granular control over the execution environment within Node.js.
  • Custom ESM Loader Hooks are More Stable: In this latest version of Node.js, a notable enhancement has been introduced for custom hooks used with loaders (–experimental-loader=./foo.mjs). These custom hooks now operate in their own dedicated thread, which is separate from the main thread. This separation provides a distinct scope for loaders, ensuring that there is no mixing or contamination between the loaders and the application code. This improvement brings additional isolation and ensures a more secure and reliable execution environment for loaders and their associated functionality.
  • Synchronous import.meta.resolve(): In order to align with the behaviour seen in browsers, a change has been made to the import.meta.resolve function in the latest version of Node.js. This function now operates synchronously, allowing for immediate resolution of module imports. However, it’s important to note that users still have the flexibility to define their own loader resolve hooks as either asynchronous or synchronous functions, based on their personal preferences and requirements. Even if asynchronous resolve hooks are utilized, the import.meta.resolve function will continue to return synchronously within the application code. This ensures a consistent and predictable experience for developers, enabling them to choose the resolution method that best suits their needs while maintaining synchronous behaviour within the application code.
  • V8 Upgraded To 11.3: Thanks to the contribution of Michaël Zasso, Node.js 20 incorporates an updated version of the V8 engine. Specifically, the V8 engine has been upgraded to version 11.3, which is a component of Chromium 113. This update introduces several improvements and new language features, including:
    • WebAssembly Tail Call
    • RegExp v flag with set notation + properties of strings
    • Resizable ArrayBuffer and growable SharedArrayBuffer
    • String.prototype.isWellFormed and toWellFormed
    • Methods that change Array and TypedArray by copy
  • Performance Enhancements: One notable achievement in Node.js 20 is the significant reduction in the cost of initializing the EventTarget. The performance team has managed to optimize the initialization process, resulting in a 50% reduction in the associated cost. Additionally, accessing all subsystems that utilize EventTarget is now faster, further improving overall performance.

Installing Node.js 20 LTS on Rocky 8 / AlmaLinux 8

Now let’s learn how we can install and use Node.js 20 LTS on Rocky 8 / AlmaLinux 8.

Step 1: Install Node.js 20 LTS

There are two methods of installing Node.js 20 LTS on Rocky 8 / AlmaLinux 8, these are:

  • Using YUM repositories
  • Using NVM

Option 1: Install Node.js 20 LTS using YUM

Node.js 20 LTS can be installed on Rocky 8 / AlmaLinux 8 using YUM repositories. This involves adding the repositories to the system as the default repos only provide Node.js 10. To view that use:

$ dnf provides nodejs
ocky Linux 8 - AppStream                                      16 MB/s | 9.1 MB     00:00    
Rocky Linux 8 - BaseOS                                        6.9 MB/s | 2.8 MB     00:00    
Rocky Linux 8 - Extras                                         43 kB/s |  13 kB     00:00    
nodejs-1:10.24.0-1.module+el8.3.0+101+f84c7154.x86_64 : JavaScript runtime
Repo        : appstream
Matched from:
Provide    : nodejs = 1:10.24.0-1.module+el8.3.0+101+f84c7154

To be able to install Node.js 20 LTS, add the repositories:

curl -sL https://rpm.nodesource.com/setup_20.x | sudo -E bash -

Remove any NodeJS installations:

sudo dnf remove nodejs -y 

Install Node.js 20 LTS can be installed on Rocky 8 / AlmaLinux 8 using the command

sudo dnf install  nodejs

Dependency Tree:

....
Transaction Summary
==============================================================================================
Install  1 Package

Total download size: 36 M
Installed size: 104 M
Is this ok [y/N]: y

Once installed, verify:

$ node -v
v20.2.0

You can also check the NPM version:

$ npm -v
9.6.6

Option 2: Install Node.js 20 LTS using NVM

Node Version Management, often known as NVM can also be used to install a desired version of Node.js. First, ensure it is added to your system.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash

Source your profile:

source ~/.bashrc

Also, enable TAB completion for NVM:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

Verify NVM installation:

$ nvm --version
0.39.3

You can then install Node.JS 20 LTS with the command:

$ nvm install 20
Downloading and installing node v20.2.0...
Downloading https://nodejs.org/dist/v20.2.0/node-v20.2.0-linux-x64.tar.xz...
####################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v20.2.0 (npm v9.6.6)
Creating default alias: default -> 20 (-> v20.2.0)

To install the latest LTS version, use:

nvm install --lts

Verify the installation:

$ node --version
v20.2.0

Step 2: Set Default Node.JS version

NVM can also be used to manage multiple Node.JS versions installed on your system. To list the installed and available versions, use:

$ nvm ls
       v16.20.0
->      v20.2.0
         system
default -> 20 (-> v20.2.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v20.2.0) (default)
stable -> 20.2 (-> v20.2.0) (default)
lts/* -> lts/hydrogen (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.0
lts/hydrogen -> v18.16.0 (-> N/A)

To change to another desired Node.JS version, say Node.JS 16, use:

$ nvm use 16
Now using node v16.20.0 (npm v8.19.4)

To set it as the default version, use:

$ nvm alias default 16
default -> 16 (-> v16.20.0)

Now verify the changes:

$ node -v
v16.20.0

Step 3: Running Node.JS Application

To test if Node.JS 20 LTS is working, we can run a simple HelloWorld app. First, we need to install all the required applications:

sudo dnf install gcc-c++ make -y 

Now we will create the app directory:

sudo mkdir /helloworld && cd /helloworld
sudo chmod 777  /helloworld

Create the simple app:

$ vim Helloworld.js
const http = require('http');

const hostname = '0.0.0.0';
const port = 8080;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World! Node.JS 20 LTS is working Perfectly!');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Now save the file and allow the port through the firewall:

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

To run the application, use:

node Helloworld.js

If all goes well, you should see the below output:

Server running at http://0.0.0.0:8080/

You can then access the app via a web browser with the URL http://IP_Address:8080

Node.js 20 LTS on Rocky 8AlmaLinux 8

To stop the instance use CTRL+C

Step 4: Using PM2 for Node.JS applications

Node.JS applications are so hard to manage at times, PM2 comes in handy to enable the Node.JS apps to be run in the background and enabled automatically on boot.

First, install it with the command:

sudo npm install -g pm2

To be able to manage an app, we need o create an ecosystem file:/

vim /helloworld/ecosystem.config.js

In the file, we need to define our application name and path:

module.exports = {
  apps : [{
    name   : "Helloworld",
    script : "./Helloworld.js"
  }]
}

Start the application with the command:

pm2 start ecosystem.config.js

The application will now run in the background and can be seen here:

                       -------------

__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
 _\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
  _\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
   _\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
    _\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
     _\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
      _\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
       _\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
        _\///______________\///______________\///__\///////////////__


                          Runtime Edition

        PM2 is a Production Process Manager for Node.js applications
                     with a built-in Load Balancer.

                Start and Daemonize any application:
                $ pm2 start app.js

                Load Balance 4 instances of api.js:
                $ pm2 start api.js -i 4

                Monitor in production:
                $ pm2 monitor

                Make pm2 auto-boot at server restart:
                $ pm2 startup

                To go further checkout:
                http://pm2.io/


                        -------------

[PM2] Spawning PM2 daemon with pm2_home=/home/rocky8/.pm2
[PM2] PM2 Successfully daemonized
[PM2][WARN] Applications Helloworld not running, starting...
[PM2] App [Helloworld] launched (1 instances)
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 0  │ Helloworld         │ fork     │ 0    │ online    │ 0%       │ 46.8mb   │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘

To enable automatic start for the app after a system reboot, use:

$ 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 rocky8 --hp /home/rocky8

Run the provided commands:

sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u rocky8 --hp /home/rocky8

Save the changes:

pm2 save

We also need to configure the SELinux context, first switch to the App directory:

cd /helloworld

Install the required SELinux tools:

sudo dnf install policycoreutils policycoreutils-python-utils setools setroubleshoot setroubleshoot-server

Switch to the root user and set SELinux in permissive mode

sudo su
setenforce 0

Set the below SELinux policy module for the application:

setsebool -P domain_can_mmap_files 1

Now switch SELinux back to enforcing mode:

setenforce 1

Now your application will be able to start automatically on system boot.

You can now manage your application with the commands:

 ##Start all applications
pm2 start ecosystem.config.js

## Stop all
pm2 stop ecosystem.config.js

## Restart all
pm2 restart ecosystem.config.js

## Reload all
pm2 reload ecosystem.config.js

## Delete all
pm2 delete ecosystem.config.js

##Delete init script
pm2 unstartup systemd

Closing Thoughts

That is it! We have learned how to install and get started with Node.js 20 LTS on Rocky 8 / AlmaLinux 8. I hope that this was significant to you.

Books For Learning Node.js programming:

See more guides on our page:

LEAVE A REPLY

Please enter your comment!
Please enter your name here