How to install Node.js and npm on CentOS 7


How to install Node.js and npm on CentOS 7

In this guide, we will show you how to install Node.js and npm on a CentOS 7. Node.js is an open source JavaScript runtime built on Chrome’s V8 JavaScript engine and can be used to build different types of server-side applications. npm is a package manager for JavaScript, with hundreds of thousands of packages it is the world’s largest software registry. This guide should work on other other Red Hat distributions as well but was tested and written for CentOS 7 OS. Installing Node.js and npm on CentOS 7 is an easy task, if you carefully follow our step by step tutorial.

1. Login to your VPS via SSH

ssh [email protected]_IP

2. Update the system and install necessary packages

yum install curl sudo

3. Install Node.js and npm from the NodeSource repository

We will install Node.js v6 LTS and npm from the NodeSource repository which depend on the EPEL repository being available.

To enable the EPEL repository on your CentOS 7 VPS, issue the following command:

sudo yum install epel-release

Once the EPEL repository is enabled run the following command to add the Node.js v6 LTS repository:

curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash -

If you want to enable the Node.js v8 repository instead of the command above run the following command:

curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -

Once the NodeSource repository is enabled we can proceed with the Node.js v6 LTS and npm installation:

sudo yum install nodejs

4. Install build tools

To compile and install native addons from the npm repository we also need to install build tools:

sudo yum install gcc-c++ make

To verify if the Node.js installation was successful, issue the following command:

node -v

The output should be like the following:

v6.11.5

5. Verify npm installation

To verify if the npm installation was successful, issue the following command:

npm -v

The output should be like the following:

3.10.10

6. Test the installation

If you want to test the installation, create a test file:

nano hello_world.js

and then add the following content:


const http = require('http');
const port = 3000;
const ip = '0.0.0.0';

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World');
}).listen(port, ip);

console.log(`server is running on ${ip}:${port}`);

Start the node web server by issuing the following command:

node hello_world.js

the output should be like the following:

server is running on 0.0.0.0:3000

7. Test the installation

If you now visit http://your_server_IP:3000 from your browser, you will see ‘Hello World’.

 


That’s it. You have successfully learned how to install both Node.js and npm on your CentOS 7 VPS. For more information about Node.js and npm, please refer to the official Node.js  and npm documentation.


If you have a Java Hosting with us, there is no need to install Node.js and npm on CentOS 7 yourself. Instead, you can ask our Linux Administrators to install Node.js and npm on CentOS 7 for you. They are available 24/7, and can help you out with any issues or requests that you have.
PS. If you like our blog posts, we’d appreciate you sharing this post on social networks by using the shortcuts below, or you can post a comment under the share shortcuts. Thanks.

Tutorials
How to install EspoCRM with Nginx on an Ubuntu VPS
Tutorials
Install OSSEC on Ubuntu 14.04
Tutorials
Install and run WordPress cached with Redis on a Linux VPS
  • Alex

    AlexAlex

    Author Reply

    Good article,
    Unfortunately it finishes very strange, how you can run localhost:3000 on centos 7


    • admin

      adminadmin

      Author Reply

      Thanks Alex. We updated the post. You can use 0.0.0.0 as const ip instead of 127.0.0.1 and then visit http://your_server_IP:3000 from your favorite web browser. You should see ‘Hello World’.


 
Free Migration to RoseHosting. Award Winning Linux VPS Hosting with Fully Managed Support. Coupon RHBLOG50 for 50% offOrder Now
+