How To Install NodeJS On CentOS 8 / RHEL 8

0

NodeJS is an open-source, cross-platform JavaScript runtime environment for developing network tools and web applications. Many of the NodeJS modules are written in JavaScript which makes the development of applications easier.

NodeJS is a combination of the Runtime environment and JavaScript modules. Node.js interprets JavaScript using Google’s V8 JavaScript engine, developed by Ryan Dahl in 2009.

In this post, we will see how to install NodeJS on CentOS 8 / RHEL 8.

NodeJS Versions

There are two versions of NodeJS available for users. Check out the official page to find the latest version of Node.js.

  • v12.x (Long Term Supported)
  • v13.x (Current Latest Version)

Install Build Toos (Optional)

To compile and install native addons from npm, you need to install development tools.

yum groupinstall -y 'Development Tools'

Install NodeJS on CentOS 8 / RHEL 8

Install NodeJS Using NodeSource (Recommended)

Install NodeJS 12

curl -sL https://rpm.nodesource.com/setup_12.x | bash -

yum install -y nodejs

Install NodeJS 13

curl -sL https://rpm.nodesource.com/setup_13.x | bash -

yum install -y nodejs

Install NodeJS Using OS Repository

NodeJS v10.x is available from the AppStream repository (rhel-8-for-x86_64-appstream-rpms) for RHEL 8 and AppStream for CentOS 8. So, you can simply install it using yum command.

When NodeJS installed, NPM (Node Package Manager) will also be installed along with Node.js.

yum install -y @nodejs

Validate NodeJS Installation

Check the Node.js version using the following command.

node -v

Output: (NodeJS v13)

v13.8.0

Check the npm version.

npm -v

Output:

6.13.6

Create Test Web Server

To test the Node.js installation, create a web server and then run it with Node.js.

Create a file called web_server.js.

vi web_server.js

Place the below contents into the web_server.js file.

const http = require('http');
const port = 9000;
const server = http.createServer((req, res) => {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end('Hello World\n');
});
server.listen(port, () => {
  console.log(`Server running at http://your-ip-add-ress:${port}/`);
});

Now, start the web server using the below command.

node --inspect web_server.js

When you run the web_server.js file, you will get the following message in your terminal.

Debugger listening on ws://127.0.0.1:9229/25dae7a2-afb9-4244-b827-8264791d70ff
For help, see: https://nodejs.org/en/docs/inspector
Server running at http://your-ip-add-ress:9000/

The above output confirms that the web server has been started and listening on port 9000.

Firewall

Allow the port 9000 in the firewall to access the web application from external machines.

firewall-cmd --permanent --add-port=9000/tcp

firewall-cmd --reload

Test NodeJS

Open a web browser and navigate it to the following URL.

http://your-ip-add-ress:9000

You should get the web page as below.

Install Node.js on CentOS 8
Install Node.js on CentOS 8

Conclusion

That’s All. You have successfully installed Node.js on CentOS 8 / RHEL 8. Please share your feedback in the comments section.

You might also like