How to Install Node.js on CentOS 8 Server Tutorial (Step by Step)

How to Install Node.js on CentOS 8. In this post we will introduce what Node.js is and we will move onto installation on CentOS 8. Let’s start. 

What is Node.js

Node.js is a free and open source JavaScript runtime environment released in 2009 by Ryan Dahl. It is built on Chrome’s V8 JavaScript engine and used for traditional websites and back end API services. The purpose of designing a server side platform was to help developers create dynamic web page content and build fast network applications. As a result, one could easily create a complete web app development project with the help of a single programming language. Node.js server is fast and scalable server side platform that stores various JavaScript modules helpful in web app developments. Also, using these modules, developers can run applications on OS X, Microsoft Windows, and Linux.

Node.js provides an NPM tool to manage packages for Node.js applications and has the world’s largest software registry. It is gaining popularity due to its ease of development process and delivers efficient performance. It is used by many popular companies, including Netflix, Uber, Walmart, eBay, etc.

Node.js features

  • Non-blocking thread execution that massively increases speed.
  • It is multi threaded.
  • Cross platform solution.
  • Using one coding language.
  • V8 Engine.
  • Quick deployment and microservice development.
  • Asynchronous data processing structure.
  • Event Driven.
  • Caching.
  • Seamless JSON support.
  • Server development.

Follow this post to install Node.js on CentOS 8.

How to Install Node.js on CentOS 8

Install Node.js from the CentOS AppStream Repository

The simple and easiest way to install Node.js is from the AppStream repository. You can list all Node.js versions included in the AppStream repository with the following command:

				
					dnf module list nodejs
				
			

You will get the following output:

				
					Last metadata expiration check: 0:00:01 ago on Monday 07 March 2022 04:14:58 PM UTC.
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                  
nodejs                   14                       common [d], development, minimal, s2i                   Javascript runtime                  
nodejs                   16                       common [d], development, minimal, s2i                   Javascript runtime                  
				
			

As you can see, Node.js 10 is set as the default version. So, if you want to install Node.js 14 you will need to enable it first. You can enable it using the following command:

				
					dnf module enable nodejs:14
				
			

You will get the following output:

				
					Last metadata expiration check: 0:00:45 ago on Monday 07 March 2022 04:14:58 PM UTC.
Dependencies resolved.
==============================================================================================================================================
 Package                           Architecture                     Version                           Repository                         Size
==============================================================================================================================================
Enabling module streams:
 nodejs                                                             14                                                                       

Transaction Summary
==============================================================================================================================================

Is this ok [y/N]: y
				
			

Once Node.js version 14 is enabled, you can install it by running the following command:

				
					dnf install nodejs -y
				
			

Once the Node.js is installed, verify the Node.js version with the following command:

				
					node --version
				
			

You will get the following output:

				
					v14.18.2
				
			

If you want to remove the Node.js package, run the following commands:

				
					dnf remove nodejs npm
				
			

Once you are finished, you can proceed to the next step.

Install Node.js from Source

The NodeSource is an online repository for open source Node.js packages. It allows you to install multiple Node.js versions in a single system.

First, install the curl command line utility using the following command:

				
					dnf install curl -y
				
			

Next, add the NodeSource repository for Node.js version 16, run the following command:

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

Once the NodeSource repository is added, install the Node.js package with the following command:

				
					dnf install nodejs -y
				
			

After the installation, verify the Node.js version with the following command:

				
					node --version
				
			

You will get the following output:

				
					v16.14.0
				
			

Once you are finished, you can proceed to the next step.

Install Node.js Using the Node Version Manager

NVM stands for “Node Version Manager” is a Node package manager that is used to manage and switch between different Node versions with ease. It comes with a command line interface to install different Node.js versions using a single command.

You can install the NVM using the curl command as shown below:

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

Once the NVM is installed, you will get the following output:

				
					  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 14926  100 14926    0     0  96922      0 --:--:-- --:--:-- --:--:-- 97555
=> Downloading nvm as script to 'https://net.cloudinfrastructureservices.co.uk/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

				
			

Next, 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"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" 
				
			

Next, verify the NVM version using the following command:

				
					nvm --version
				
			

You will get the following output:

				
					0.38.0
				
			

If you want to list all available Node.js versions, run the following command:

				
					nvm list-remote
				
			

You will get a list of all Node.js versions in the following output:

				
					        v16.7.0
        v16.8.0
        v16.9.0
        v16.9.1
       v16.10.0
       v16.11.0
       v16.11.1
       v16.12.0
       v16.13.0   (LTS: Gallium)
       v16.13.1   (LTS: Gallium)
       v16.13.2   (LTS: Gallium)
       v16.14.0   (Latest LTS: Gallium)
        v17.0.0
        v17.0.1
        v17.1.0
        v17.2.0
        v17.3.0
        v17.3.1
        v17.4.0
        v17.5.0
        v17.6.0

				
			

From the above list, install the Node.js version 17.6.0 using the following command:

				
					nvm install v17.6.0
				
			

You will get the following output:

				
					Downloading and installing node v17.6.0...
Downloading https://nodejs.org/dist/v17.6.0/node-v17.6.0-linux-x64.tar.xz...
####################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v17.6.0 (npm v8.5.1)
Creating default alias: default -> v17.6.0
				
			

Now, verify the Node.js version with the following output:

				
					node --version
				
			

You will get the following output:

				
					v17.6.0
				
			

To list all installed Node.js versions, run the following command:

				
					nvm ls
				
			

You will get the following output:

				
					->      v17.6.0
default -> v17.6.0
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v17.6.0) (default)
stable -> 17.6 (-> v17.6.0) (default)
lts/* -> lts/gallium (-> 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.10 (-> N/A)
lts/fermium -> v14.19.0 (-> N/A)
lts/gallium -> v16.14.0 (-> N/A)
				
			

To install the latest stable version, run the following command:

				
					nvm install --lts
				
			

You should see the following output:

				
					Installing latest LTS version.
Downloading and installing node v16.14.0...
Downloading https://nodejs.org/dist/v16.14.0/node-v16.14.0-linux-x64.tar.xz...
####################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v16.14.0 (npm v8.3.1)
				
			

To set the default Node.js version to LTS, run the following command:

				
					nvm use --lts
				
			

You should see the following output:

				
					Now using node v16.14.0 (npm v8.3.1)
				
			

To install the Node.js stable version, run the following command:

				
					nvm install stable
				
			

You will get the following output:

				
					v17.6.0 is already installed.
Now using node v17.6.0 (npm v8.5.1)
				
			

If you want to remove Node.js version 16.4.0, run the following command:

				
					nvm uninstall v16.14.0
				
			

Once you are finished, you can proceed to the next step.

Create a Simple Node.js Application

After explaining different Node.js installation methods, we will show you how to create and run a simple Node.js application.

First, create a file named app.js using the following command:

				
					nano app.js
				
			

Add the following code:

				
					var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, 'localhost');
console.log('Server running at http://localhost:9000/');
				
			

Next, set the executable permission to the app.js file using the following command:

				
					chmod +x ./app.js
				
			

Please run the Node.js application using the following command:

				
					node app.js &
				
			

You will get the following output:

				
					Server running at http://localhost:9000/
				
			

Now, open another terminal and access your Node.js application using the following command:

				
					curl http://localhost:9000
				
			

You will get the following output:

				
					Hello World
				
			

Well done you have followed all the steps to Install Node.js on CentOS 8.

How to Install Node.js on CentOS 8 Conclusion

This guide explained how to install Node.js using three different methods. You can now choose your preferred method to install Node.js on your server. You can now use Node.js to build both front end and back end applications. For more information, visit the Node.js documentation page.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x