How to Install Node.js and npm on CentOS 7

Choose a different version or distribution

Introduction

Before we begin talking about how to install Node.js and npm on CentOS 7, let's briefly understand – What is Node.js?

Node.js is a cross-platform JavaScript run-time environment that enables server-side execution of JavaScript code. Node.js is most commonly used on the back end, although it is also widely used as a full-stack and front-end solution.

npm, or Node Package Manager, is the default package manager for Node.js and the world's largest software repository for open-source Node.js package publishing.

In the first section of this tutorial, we will install Node.js and npm from the NodeSource repository using the yum package manager. Node.js & npm empower devs, enhance productivity & manage dependencies. You will learn how to install Node.js and npm using the nvm script in the second section.

If you simply require Node.js to deploy Node.js applications, the simplest method is to use yum to install the Node.js packages from the NodeSource repository.

In this tutorial, you will install Node.js and npm on CentOS 7. We will also address a few FAQs on how to install Node.js and npm on CentOS 7.

Advantages of Node.js

  1. Scalability: Node.js utilizes an event-driven, non-blocking I/O model that allows for high scalability and efficient handling of concurrent requests.
  2. Fast Performance: It has a lightweight runtime and uses JavaScript's V8 engine, which enables speedy processing and improved performance.
  3. Vast Ecosystem: Node.js has a rich library of packages and modules available through npm, enabling rapid development and easy integration of third-party functionalities.
  4. Real-time Applications: With its ability to handle many simultaneous connections, Node.js is ideal for building real-time applications like chat, gaming, and collaboration tools.
  5. Code Sharing: Developers can use JavaScript for both client-side and server-side coding, allowing for code sharing and increased productivity across the full stack.

Prerequisites

Make sure you are logged in as a user with sudo privileges before proceeding with this tutorial.

Installing Node.js and npm on CentOS 7

In order to provide enterprise-grade Node support, NodeSource runs a regularly updated Node.js repository for Linux distributions.

On your CentOS 7 system, use the following steps to install Node.js and npm from the NodeSource repositories:

1. Add NodeSource yum repository

Node.js 10.x is the latest LTS version. To install version 8, simply replace setup_10.x with setup_8.x in the command below.

To add the NodeSource yum repository to your machine, use the curl command:

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

2. Install Node.js and npm

Install Node.js and npm when the NodeSource repository has been enabled by typing:

sudo yum install nodejs

Type y and press Enter when prompted to import the repository's GPG key.

3. Verify the Node.js and npm Installation

To ensure that the installation was successful, run the following commands, which will print the Node.js and npm versions.

Print Node.js version:

node --version
Output

v10.13.0

Print npm version:

npm --version
Output

6.4.1

How to install Node.js and npm using NVM

NVM (Node Version Manager) is a bash script that allows you to handle multiple Node.js versions at the same time. We can install and remove any specific Node.js version using NVM, thus we can use or test as many Node.js versions as we like.

Follow these steps to install Node.js and npm on your CentOS system using NVM:

1. Install NVM (Node Version Manager)

Execute the following command to download the nvm install script:

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

The script will clone GitHub's nvm repository to ~/.nvm and add the script Path to your Bash or ZSH profile.

Output

=> 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

As indicated by the output, you should either close and restart your terminal or run the commands to add the path to the nvm script to your current session.

To ensure that nvm was properly installed, run:

nvm --version
Output

0.33.11

2. Install Node.js using NVM

Now that the nvm tool has been installed, we can install the most recent version of Node.js by typing:

nvm install node
Output

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

You may check the Node.js version by typing:

node --version
Output

v10.1.0

3. Install multiple Node.js versions using NVM

Let us install two more versions: the most recent LTS version and version 8.12.0.

nvm install --lts
nvm install 8.12.0

Once LTS and 8.12.0 are installed, use the following command to list all Node.js instances that have been installed:

nvm ls
Output

->      v8.12.0                         # ACTIVE VERSION
       v10.13.0
        v11.0.0
default -> node (-> v11.0.0)           # DEFAULT VERSION
node -> stable (-> v11.0.0) (default)
stable -> 11.0 (-> v11.0.0) (default)
iojs -> N/A (default)
lts/* -> lts/dubnium (-> v10.13.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.14.4 (-> N/A)
lts/carbon -> v8.12.0
lts/dubnium -> v10.13.0

The output indicates that the entry with an arrow on the left (→ v8.12.0) is the version used in the current shell session, and the default version is set to v11.0.0.

The following command can be used to change the currently active version:

nvm use 10.13.0

The output will appear something similar to this:

Output

Now using node v10.13.0 (npm v6.4.1)

To change the default Node.js version, type do the following:

nvm alias default 10.13.0
Output

default -> 10.13.0 (-> v10.13.0)

Install development tools

We must install the necessary development tools and libraries in order to build native modules from npm:

sudo yum install gcc-c++ make

FAQs to Install Node.js and npm on CentOS 7

How can I verify if npm is installed correctly on CentOS 7? 

Open the terminal and run npm -v. If npm is installed, it will display the installed version.

How can I verify if Node.js is installed correctly on CentOS 7? 

Open the terminal and run node -v. If Node.js is installed, it will display the installed version.

How can I update Node.js and npm on CentOS 7? 

To update Node.js, use a package manager like npm or nvm. To update npm, run sudo npm install -g npm.

Where is Node.js installed on CentOS 7? 

Node.js is typically installed in the /usr/bin/ directory on CentOS 7.

Can I have multiple Node.js versions installed on CentOS 7? 

Yes, you can use a version manager like nvm (Node Version Manager) to manage multiple Node.js versions on CentOS 7.

How can I install a specific Node.js version using nvm on CentOS 7? 

Install nvm using curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash, then open a new terminal. Finally, run nvm install <version> to install a specific Node.js version.

Conclusion

We have shown you two different ways for installing Node.js and npm on your CentOS 7 server. The approach you select is determined on your needs and preferences. The nvm technique allows you to add and remove various Node.js versions on a per-user basis, whereas the packaged version from the NodeSource repository is easier to install.

You can also learn about how to install and use yarn on CentOS 7 in this tutorial.

If you have any queries, feel free to drop a comment below, and we'll be happy to help.