Share on Social Media

Learn how to install Node.js on CentOS 8 with this comprehensive guide. Step-by-step instructions to set up a Node.js server, ensuring a smooth installation process for your Linux environment. #centlinux #linux #nodejs

What is Node.js?

Node.js is a powerful and versatile runtime environment that allows developers to execute JavaScript code outside of a web browser. This opens up a wide array of possibilities for server-side development, enabling the creation of dynamic and interactive web applications. Key features of Node.js include:

  1. Asynchronous and Event-Driven: Node.js is designed around an event-driven, non-blocking I/O model. This means that operations, such as reading from the network or filesystem, do not block the execution of the program, allowing it to handle multiple operations concurrently. This makes Node.js particularly efficient and scalable for real-time applications.
  2. Single Programming Language: By using JavaScript for both client-side and server-side code, Node.js simplifies the development process. Developers can work in a single language across the entire application stack, improving code consistency and reducing the learning curve.
  3. Rich Ecosystem: The Node Package Manager (npm) is one of the largest ecosystems of open-source libraries and modules available for any programming language. With npm, developers can easily find and integrate third-party packages into their applications, speeding up development and enabling code reuse.
  4. High Performance: Built on the V8 JavaScript engine, Node.js is highly optimized for performance. It compiles JavaScript directly to machine code, allowing for fast and efficient execution of scripts.
  5. Community and Support: Node.js has a large and active community that contributes to its growth and improvement. There are numerous resources, tutorials, and forums available to help developers learn and troubleshoot.
  6. Cross-Platform Compatibility: Node.js runs on various platforms, including Windows, Linux, and macOS, making it a flexible choice for development teams working in diverse environments.
  7. Microservices and API Development: Node.js is well-suited for building microservices and RESTful APIs. Its lightweight nature and ability to handle asynchronous operations make it an ideal choice for services that need to handle numerous simultaneous requests efficiently.

In summary, Node.js is a robust runtime environment that leverages JavaScript’s capabilities on the server side, offering high performance, scalability, and a vast ecosystem of modules and libraries. It is a popular choice for developers looking to build modern, efficient, and scalable web applications.

Node.js is written in C, C++ and JavaScript. It is distributed under MIT License and can be download from their official website or Node.js Github Repository.

What is Node.js used for?

Node.js is used for a wide range of applications due to its efficiency and versatility. Some of the most common use cases include:

  1. Web Servers: Node.js is frequently used to build web servers that can handle HTTP requests and serve dynamic content. Its non-blocking I/O model allows it to efficiently manage multiple simultaneous connections.
  2. API Development: Node.js is ideal for building RESTful APIs and microservices. Its asynchronous nature helps in handling numerous API requests simultaneously without performance degradation.
  3. Real-Time Applications: Node.js excels in developing real-time applications such as chat applications, online gaming, live streaming services, and collaborative tools. Its event-driven architecture is perfect for applications that require real-time interaction.
  4. Single-Page Applications (SPAs): With Node.js, developers can build SPAs where the server handles the logic, while the client side renders the content dynamically. This approach enhances the user experience by reducing load times and providing seamless interaction.
  5. Internet of Things (IoT): Node.js is used in IoT projects for collecting and processing data from various devices. Its ability to handle numerous concurrent connections makes it suitable for managing communications between devices in real-time.
  6. Server-Side Rendering (SSR): Node.js can be used for server-side rendering of web pages, which improves the performance and SEO of web applications. Frameworks like Next.js leverage Node.js for this purpose.
  7. Microservices Architecture: Node.js supports the development of microservices, allowing developers to build and deploy small, independently running services. This modular approach simplifies scaling and maintenance.
  8. Command Line Tools: Developers use Node.js to create command-line tools and utilities. Its package manager, npm, makes it easy to publish and distribute these tools.
  9. Data Streaming Applications: Node.js handles data streams efficiently, making it suitable for applications that process real-time data streams, such as video streaming services and file uploading platforms.
  10. Proxies and Load Balancers: Node.js can act as a proxy server to handle requests to other servers, load balancing, or even caching data to improve performance.
  11. Cross-Platform Desktop Applications: Tools like Electron use Node.js to build cross-platform desktop applications with web technologies, enabling developers to create applications that run on Windows, macOS, and Linux.
  12. Automation and Scripting: Node.js is used for writing scripts to automate repetitive tasks, manage system operations, and streamline workflows.

In summary, Node.js is a versatile tool used in web development, real-time applications, IoT, microservices, and more. Its event-driven, non-blocking architecture makes it suitable for applications requiring high performance and scalability.

Read Also: How to install Nodejs in Docker

Node.js vs Express.js

Node.js and Express.js are related but serve different purposes in web development:

Node.js

  • Node.js is a runtime environment that allows developers to run JavaScript code on the server side.
  • It provides a platform for building scalable and efficient network applications using JavaScript.
  • Node.js includes libraries and tools for handling HTTP requests, file system operations, and other server-side tasks.
  • It has a non-blocking, event-driven architecture, making it well-suited for real-time applications and handling concurrent connections efficiently.

Express.js

  • Express.js is a web application framework for Node.js, built on top of it.
  • It provides a set of features and utilities for building web applications and APIs more easily and efficiently.
  • Express.js simplifies common tasks such as routing, middleware integration, and handling HTTP requests and responses.
  • It follows a minimalist design philosophy, allowing developers to add only the features they need, making it flexible and lightweight.
  • Express.js is widely used in the Node.js ecosystem and is known for its simplicity, performance, and robustness.

In essence, Node.js is the runtime environment that allows JavaScript to run on the server side, while Express.js is a framework that simplifies and streamlines the process of building web applications and APIs using Node.js. Developers often use them together to create powerful and scalable server-side applications with JavaScript.

Read Also: How to install OpenAI on Rocky Linux 9

Recommended Online Training: OpenAI API with Node. JS Bootcamp: ChatGPT API, GPT-4, DALL·E

5657688 d083show?id=oLRJ54lcVEg&offerid=1606991.5657688&bids=1606991

Environment Specification

We have provisioned a minimal CentOS 8 KVM machine with following specifications.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 2 GB
  • Storage – 20 GB
  • Operating System – CentOS 8.2
  • Hostname – nodejs-01.centlinux.com
  • IP Address – 192.168.116.230 /24

Update Linux Operating System

Connect with nodejs-01.centlinux.com as root user by using a ssh client.

As per the best practice, update your installed software packages on Linux operating system by using dnf command.

# dnf update -y
Last metadata expiration check: 0:00:20 ago on Wed 18 Nov 2020 09:43:56 PM PKT.
Dependencies resolved.
Nothing to do.
Complete!

Our operating system is already up-to-date. Therefore, no package was updated. The output may vary on your Linux server.

Verify the Linux operating system and Kernel version that are being used in this installation guide.

# uname -r
4.18.0-193.28.1.el8_2.x86_64

# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)

Install Node.js Server on CentOS 8

There can be two ways to install Node.js on CentOS 8. According to your requirement, you can follow any one of these methods.

1. Install Node.js from Yum Repo

The easiest method to install Node.js on CentOS 8 is by using the Linux yum repository.

In CentOS 8 yum repositories, the two stable versions of the Node.js are already available. These are the most commonly used versions and may work in most situations.

To get a list of available versions of Node.js in Linux yum repository, you can use following dnf command.

# dnf module list nodejs
Last metadata expiration check: 0:07:05 ago on Wed 18 Nov 2020 09:43:56 PM PKT.
CentOS-8 - AppStream
Name      Stream    Profiles                                Summary
nodejs    10 [d]    common [d], development, minimal, s2i   Javascript runtime
nodejs    12        common [d], development, minimal, s2i   Javascript runtime

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

Install your required version of Node.js using dnf command, we are installing the version 12 on our Linux server.

# dnf module install -y nodejs:12...
Installed:
  nodejs-1:12.18.4-2.module_el8.2.0+530+cb1b9c8b.x86_64
  nodejs-docs-1:12.18.4-2.module_el8.2.0+530+cb1b9c8b.noarch
  nodejs-full-i18n-1:12.18.4-2.module_el8.2.0+530+cb1b9c8b.x86_64
  npm-1:6.14.6-1.12.18.4.2.module_el8.2.0+530+cb1b9c8b.x86_64

Complete!

After successful installation, check the version of Node.js and Node Package Manager (NPM) versions.

# node -v
v12.18.4

# npm -v
6.14.6

2. Install Node.js using NVM

Although it is quiet convenient to install Node.js from Linux yum repository. But you may not found the latest versions of Node.js therein.

For Example, Node.js version 14 LTS and 15 are already released, but they are not available in Linux yum repository.

Therefore, if you wish to install latest version of Node.js then you have to use the Node Version Manager (NVM).

By using NVM you can easily install/ uninstall different versions of Node.js on/from your Linux server.

NVM is available at GitHub. You can see complete project details at NVM Github repository.

You can install NVM by using the script provided in the NVM documentation.

# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.0/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13527  100 13527    0     0   7056      0  0:00:01  0:00:01 --:--:--  7052
=> Downloading nvm as script to '/root/.nvm'

=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

NVM has been installed, you can now use nvm command it to install Node.js on your Linux server.

But first, list down the available versions of Node.js.

# nvm list-remote
...
       v14.14.0
       v14.15.0   (LTS: Fermium)
       v14.15.1   (Latest LTS: Fermium)
        v15.0.0
...

Although, Node.js v15 is available, but it is a better approach to install the LTS (Long Term Support) version especially if your are installing Node.js on a production machine.

You can install the latest LTS version of Node.js by using nvm command.

# nvm install --lts
Installing latest LTS version.
Downloading and installing node v14.15.1...
Downloading https://nodejs.org/dist/v14.15.1/node-v14.15.1-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v14.15.1 (npm v6.14.8)
Creating default alias: default -> lts/* (-> v14.15.1)

Verify the installed versions of Node.js and Node Package Manager (NPM).

# node -v
v14.15.1

# npm -v
6.14.8

Similarly, you can switch to another version of Node.js by using the NVM as follows.

# nvm install v13.6.0
Downloading and installing node v13.6.0...
Downloading https://nodejs.org/dist/v13.6.0/node-v13.6.0-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v13.6.0 (npm v6.13.4)

Again check the installed versions of Node.js and NPM.

# node -v
v13.6.0

# npm -v
6.13.4

Create & Deploy a Simple Node.js Application

You can test your Node.js server by writing a simple JavaScript.

Create a text file in vim editor.

# vi Node.js_test.js

Add following lines of code in this file.

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

Temporarily allow the service port 9000/tcp in Linux firewall.

# firewall-cmd --add-port=9000/tcp
success

Start a Node.js debugger process by using following command.

# node --inspect Node.js_test.js
Debugger listening on ws://127.0.0.1:9229/d25c2fe9-b50e-4bfb-aa45-1d6988d5d470
For help, see: https://nodejs.org/en/docs/inspector
Server running at http://your-ip-address:9000/

Use a web browser or following Linux command to test your Node.js application.

# curl http://nodejs-01.centlinux.com:9000
Hello World

To start building advanced applications with Node.js, you may need to read Node.js Design Patterns, 3rd Edition (PAID LINK) by Packt Publishing.

Final Thoughts

Setting up a Node.js server on CentOS 8 can significantly enhance your web development projects by providing a robust and efficient environment. Following the steps outlined in this guide will ensure a smooth installation process.

If you need any further assistance or professional help with your Node.js installation or any other server-related tasks, feel free to check out my Fiverr profile. I offer a range of services to help you with your technical needs, ensuring that your systems run optimally and securely.

Leave a Reply