Cyberithub

Install NPM and Node.js in 6 Easy Steps on CentOS 7

In this article, I will take you through the steps to install NPM and Node.js in 6 Easy Steps on CentOS 7. Node.js is a platform for interpreting JavaScript code and running applications. Java-Script has been around for a couple of decades with every improvement, it has shifted further from being a client-side scripting language to a full-fledged server-side programming language for managing data.

Because Node.js is built with Google Chrome’s JavaScript engine (a tool used to interpret the JavaScript language into meaningful computer commands), it’s considered to be powerful and able to support JavaScript as a server-side language.

JavaScript can be used to both assist in web-page (client-side) interactions and handle incoming application data and database communications. (The latter jobs have often been reserved for languages such as C, Java, Python, and Ruby, among others). Developers can now commit to mastering JavaScript to build a complete web application instead of having to master multiple languages to accomplish the same task.

Install NPM and Node.js in 6 Easy Steps on CentOS 7 1

Install NPM and Node.js On CentOS 7

Also Read: Install Node.js in 6 Easy Steps on Ubuntu 18.04

Step 1: Prerequisites

a)You need to have a running CentOS 7 System.

b)You need to have root user access or user with sudo access to run all privilege commands. In this article, I am running all the commands through root user. You can check How to add User into Sudoers to provide sudo access to User.

Step 2: Update Your System

Before going through the steps to setup and install npm and node.js on CentOS, it is important to update all the packages in your System with the latest version by running yum update command.

[root@localhost ~]# yum update
Resolving Dependencies
--> Running transaction check
---> Package java-11-openjdk.x86_64 1:11.0.5.10-0.el7_7 will be updated
---> Package java-11-openjdk.x86_64 1:11.0.6.10-1.el7_7 will be an update
---> Package java-11-openjdk-headless.x86_64 1:11.0.5.10-0.el7_7 will be updated
---> Package java-11-openjdk-headless.x86_64 1:11.0.6.10-1.el7_7 will be an update
---> Package kubeadm.x86_64 0:1.17.0-0 will be updated
---> Package kubeadm.x86_64 0:1.17.2-0 will be an update
---> Package kubectl.x86_64 0:1.17.0-0 will be updated
---> Package kubectl.x86_64 0:1.17.2-0 will be an update
---> Package kubelet.x86_64 0:1.17.0-0 will be updated
---> Package kubelet.x86_64 0:1.17.2-0 will be an update
--> Finished Dependency Resolution

...................................................................................................

Step 3: Setup Node.js Repository 

To install node.js and npm, you need to set up the repository first to download the packages. As per GITHUB Page, you can setup your repository by using either of the 2 methods:-

You can setup your repo through root access by running below command.

[root@localhost ~]# curl -sL https://rpm.nodesource.com/setup_13.x | bash -

You can also set up your repository through sudo User by running below command.

[root@localhost ~]# curl -sL https://rpm.nodesource.com/setup_13.x | sudo bash -

Step 4: Install NPM and Node.js 13.X 

Once NPM and Node.js repository is added, you can use yum install nodejs -y command to install npm and node.js from repository.

[root@localhost ~]# yum install nodejs -y
Resolving Dependencies
--> Running transaction check
---> Package nodejs.x86_64 2:13.7.0-1nodesource will be installed
--> Finished Dependency Resolution

...................................................................................................................................................

NOTE:

Please note that at the time of writing this article, 13.X is the latest version available to install. You might see another version at the time of your installation. You can always download and use the latest version.

You may also need development tools to build native addons. Below command will install gcc-c++ and make tools which you will need to configure, compile and install from source code.

[root@localhost ~]# yum install gcc-c++ make
Package 1:make-3.82-24.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package gcc-c++.x86_64 0:4.8.5-39.el7 will be installed
--> Processing Dependency: libstdc++-devel = 4.8.5-39.el7 for package: gcc-c++-4.8.5-39.el7.x86_64
--> Processing Dependency: gcc = 4.8.5-39.el7 for package: gcc-c++-4.8.5-39.el7.x86_64
--> Running transaction check
---> Package gcc.x86_64 0:4.8.5-39.el7 will be installed
---> Package libstdc++-devel.x86_64 0:4.8.5-39.el7 will be installed
--> Finished Dependency Resolution

........................................................................................................................................................................

Step 5: Check Node.js and NPM Version

You can check node version by using node -v command.

[root@localhost ~]# node -v
v13.7.0

You can check npm version by using npm -v command.

[root@localhost ~]# npm -v
6.13.6

Step 6: Run Your First Node.js Program

Now that your Node.js package is installed, let’s write one simple program to create a Server and Listen Client Connections on Port 8080. Here I have used my local system IP Address(192.168.0.110) to create a Server. You can also use localhost or FQDN of your server here. It depends on you how you want to use.

So basically below Program says that whenever any client try to request port 8080 on Server 192.168.0.110, he will get response Hello World! from the Server. This tells that Server is up and listening for Client Requests.

[root@localhost ~]# cat example.js
var http = require('http');

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!\n');
}).listen(8080,"192.168.0.110");

Run Your example.js program by running node example.js command as shown below.

[root@localhost ~]# node example.js

Open another duplicate Terminal and run below command to get the Response from Server.

[root@localhost ~]# curl -XGET http://192.168.0.110:8080
Hello World!

 

Also Read: 10 find exec multiple command examples in Linux/Unix

 

Popular Searches

  • install node linux
  • centos install nodejs
  • nodejs centos

1 thought on “Install NPM and Node.js in 6 Easy Steps on CentOS 7”

  1. DEPRECATION WARNING

    Node.js 13.x is no longer actively supported!

    You will not receive security or critical stability updates for this version.

    You should migrate to a supported version of Node.js as soon as possible.
    Use the installation script that corresponds to the version of Node.js you
    wish to install. e.g.

    * https://deb.nodesource.com/setup_10.x — Node.js v10 LTS “Dubnium”
    * https://deb.nodesource.com/setup_12.x — Node.js v12 LTS “Erbium” (recommended)
    * https://deb.nodesource.com/setup_14.x — Node.js v14 LTS “Fermium”
    * https://deb.nodesource.com/setup_15.x — Node.js v15 “Fifteen”

    Reply

Leave a Comment