How to Install Node.js and npm on CentOS 7

How to install Node.js and npm on CentOS 7
How to install Node.js and npm on CentOS 7
centos install nodejs

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 tutorial below.

1. Login to your VPS via SSH

ssh user@vps_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 depends 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 add-ons 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:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
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.

Follow this tutorial on installing Node.js and npm on Ubuntu 20.04


installing Node.js and npm on CentOs 7

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 on installing Node.js and npm on CentOs 7,  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.

10 thoughts on “How to Install Node.js and npm on CentOS 7”

    • 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’.

      Reply
    • You can install NVM using the following command:
      curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

      Reply
  1. Awesome article, but I have a problem when installing NodeJS on a Centos7 VPS server via SSH.

    Trying to install the latest stable version of nodejs using:
    curl —silent —location https://rpm.nodesource.com/setup_10.x | sudo bash –

    …produces the instruction Run ‘sudo yum install -y nodejs’ to install Node.js 10.x and npm

    If I run the above command it says nodejs-10.16.3-1nodesource.x86_64 already installed and latest version

    But if I then check the installed versions it isn’t found:

    [root@server ~]# node -v
    v10.16.3
    [root@server ~]# npm -v
    6.9.0
    [root@server ~]# nodejs -v
    -bash: nodejs: command not found
    There any followup tests like nano also fail

    Any suggestions would be greatly appreciated!

    Reply

Leave a Comment