Share on Social Media

Node.js is an open source JavaScript Runtime Environment. In this guide, you will learn how to install NodeJS on CentOS 8 based Linux server. #centlinux #linux #nodejs

What is Node.js? :

Node.js is an open-source, cross-platform, back-end, JavaScript runtime environment that executes JavaScript code outside a web browser.

Node.js lets developers use JavaScript to write command line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user’s web browser. Consequently, Node.js represents a “JavaScript everywhere” paradigm, unifying web-application development around a single programming language, rather than different languages for server-side and client-side scripts. (Source: Wikipedia)

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.

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 Software Packages:

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 NodeJS on CentOS 8 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

Install NodeJS on CentOS 8 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

Conclusion – Install NodeJS on CentOS 8:

In this article, you have learned how to install NodeJS on CentOS 8 server and deploy a test JavaScript on it. To start building advanced applications with Node.js, you may need to read Node.js Design Patterns, 3rd Edition (PAID LINK) by Packt Publishing.

Leave a Reply

Your email address will not be published. Required fields are marked *