How to Install Node.js and npm on CentOS 7

Try this guide with our instant dedicated server for as low as 40 Euros

Install Node.js and npm on CentOS

When it comes to modern web development, Node.js is an indispensable tool that developers use as the preferred method of running JavaScript on the server side. 

Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to execute JavaScript code server-side. 

Node Package Manager (npm) is the default package manager for Node.js that helps install, manage, and share reusable code components called packages or modules.

Whether you’re setting up a development environment or preparing a production server, understanding how to install Node.js and npm on CentOS 7 is crucial. It ensures you have the necessary tools (and knowledge) to build, deploy, and manage powerful web applications.

In this tutorial, we will discuss the steps to install Node.js and its package manager, npm, on CentOS 7. 

Let’s start with a quick overview of Node.js and npm and their features.

Table Of Contents

  1. What is Node.js
    1. Key Features of Node.js
  2. What is npm
  3. The Relation Between Node.js and npm
  4. How to Install Node.js and npm
    1. The Prerequisites
    2. Method #1: Use NodeSource
    3. Method #2: Use NVM
  5. Install Multiple Versions of Node.js Using NVM
  6. Install Node.js Development Tools
  7. Conclusion
  8. FAQs

What is Node.js

Node.js is a JavaScript platform for server-side programming. It allows users to create networked applications that require backend functionality. Built on the V8 JavaScript engine, Node.js is extremely fast and efficient.

Key Features of Node.js

The following are some of the popular features of Node.js:

Asynchronous and Event-Driven

Node.js uses a non-blocking, event-driven architecture, where operations like reading from a database or file system are performed asynchronously. As a result, applications can handle multiple tasks simultaneously without waiting for one task to complete before starting another.

Single Programming Language For Frontend and Backend

Since Node.js is JavaScript-based, developers can write client-side and server-side scripts in JavaScript and avoid the issues common in multi-language architecture. 

npm (Node Package Manager)

Node.js comes with a package manager, npm, the largest ecosystem of open-source libraries. Developers use npm to easily install, share, and manage dependencies for projects.

Scalability

Node.js is designed to build scalable network applications. It is particularly well-suited for developing real-time applications, like chat servers, live streaming platforms, and online gaming servers, due to its non-blocking I/O operations and efficient handling of concurrent connections.

Support for Cross-Platform Apps

Node.js applications can run on various operating systems, including Windows, Linux, and macOS, offering cross-platform flexibility. 

What is npm

npm is the default package manager for Node.js, used to install, manage, and share reusable packages and modules.

At the moment, it is the world’s largest software repository for publishing open-source Node.js packages. These packages provide pre-written code for various functionalities, saving time and effort from developing from scratch.

The Relation Between Node.js and npm

Node.js acts as the foundation, providing the environment to execute JavaScript code on the server. npm is typically installed alongside Node.js. 

This npm helps find and integrate pre-built code modules (packages) into Node.js projects. These packages offer functionalities like database access, web frameworks, and image processing.

How to Install Node.js and npm 

The Prerequisites

Before diving into the installation of Node.js, ensure you have the following.

  • A CentOS 7 Server
  • yum package manager installed
  • A user account with root or sudo privileges
  • Access to the terminal/command line.

In a CentOS 7 environment, you can install Node.js and npm through one of the following two methods. 

Method #1: Use NodeSource

The NodeSource repository provides an efficient and straightforward method for installing Node.js and npm on CentOS 7.

Step #1: Update System Package Index

Refresh your system’s local package index to ensure access to the latest version of installed packages and software. 

Start by running the following command to update the system repository:

# sudo yum update

Step #2: Add NodeSource Repository

Next, add the NodeSource repository into your system to download Node.js directly. 

The important benefit of this approach is that the NodeSource repository contains the latest Node.js versions that might not be available in the default CentOS repositories.

We recommend using the following cURL command to add NodeSource repository:

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

Step #3: Install Node.js and npm 

Once the repository is added, you can use the yum package manager to install Node.js and npm.

To install Node.js and npm run this command:

# sudo yum install -y nodejs

Here, 

-y: Instructs yum to accept any prompts during installation.

Step #4: Verify Installation

Once you have installed Node.js and npm, verify if it’s successfully installed by printing the version information through the following command:

# node -version

node -version

Similarly, to check the installed npm version, run:

# npm –version

npm –version

Method #2: Use NVM

If you are interested in installing and managing multiple versions of Node.js, we recommend installing Node.js and npm through the Node Version Manager (NVM). 

Step #1: Install cURL

It is necessary to have cURL installed to fetch the NVM installation script. Therefore, if cURL is not already installed, you can install it on your system by executing this command:

# sudo yum install curl

Confirm the installation when prompted by pressing Y and then Enter.

Step #2: Update System Package Index

It is necessary to keep the system package repository updated. For this, we suggest the following command:

# sudo yum update

Step #3: Download the NVM Installation Script

You can now run the following cURL command to download the NVM installation script:

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

This step initializes the setup of NVM, allowing for flexible management of Node.js versions.

Once NVM is set up, you will receive the following message:

message after nvm setup

Next, activate NVM on your system.

You can restart the terminal or execute the command mentioned in the message.

Step #4: Verification

Next, verify if the NVM was successfully installed by executing the following command in the terminal:

# nvm --version

The output confirms that NVM version 0.34.0, the most current stable release, is installed on your system.

nvm --version

Step #5: Install Node.js and npm

Once you have set up NVM, you can now download Node.js and npm

Execute the following NVM command to initiate the installation:

# nvm install node

Step #6: Verification

To ensure the installation was successful, verify the versions of Node.js installed on your system by executing:

# node --version

nvm install node

Install Multiple Versions of Node.js Using NVM

While the previous section covered installing a single Node.js version using NVM, the true power of NVM becomes apparent when you use it to manage multiple versions on a CentOS 7 system. 

This allows for easy switching between different Node.js versions for project-specific requirements.

Note: If you do not have NVM already installed in your system, refer to Method 2 in the previous section to understand how to install NVM on your system.

Install Specific Node.js Versions

To install a specific version of Node.js, simply use the nvm install command followed by the desired version number. 

For instance, to install Node.js version 10.1.0, run this command:

# nvm install 10.1.0

Similarly, you can install the latest Long-Term Support (LTS) version with this command:

# nvm install --lts

You can list the installed Node.js versions by running the following command:

# nvm ls

This command displays all Node.js versions installed, highlighting the default and stable versions in use.

nvm ls

Switch Between Node.js Versions

Once you have multiple versions installed, you can easily switch between them using the nvm use command followed by the version number you want to activate. For this, we recommend the following command:

# nvm use 10.16.0

This will change the active Node.js version to the specified version in your current shell session. After you have changed versions, it is always a good practice to verify the active version by running node -v in your terminal.

Install Node.js Development Tools

Node.js development tools are a suite of utilities that facilitate the compilation and installation of native add-ons from npm. Native add-ons in Node.js are compiled modules written in C or C++. These modules are essential for creating, debugging, and managing Node.js applications efficiently.

You can run the following command to install the Node.js development tools and libraries on your CentOS 7 system:

# sudo yum install gcc g++ make

The command installs these essential packages:

  • gcc: GNU Compiler Collection (C compiler)
  • g++: GNU Compiler Collection (C++ compiler)
  • make: Tool for automating compilation processes

Conclusion

Completing the installation of Node.js and npm on CentOS 7 marks a significant milestone in setting up your development environment. By following the steps outlined in this guide, you are now equipped to leverage the robust features of Node.js and the extensive repository of packages provided by npm

Whether you are developing applications, creating server-side scripts, or managing dependencies, you have the foundational tools necessary to drive your projects forward. Remember, the capabilities of Node.js combined with a CentOS 7 system offer a stable, secure, and efficient platform for any developer looking to enhance their programming endeavors.

FAQs

Q. What is the simplest option for installing Node.js on CentOS 7?

The simplest option for installing Node.js on CentOS 7 is through the NodeSource repository using the command line curl and a single command execution. You can do this with curl -sL https://rpm.nodesource.com/setup_12.x | sudo -E bash – followed by sudo yum install nodejs.

Q. How do I install specific versions of Node.js using NVM?

Use the command nvm install <version> to install a specific version. NVM allows you to effectively manage and switch between installed versions using the nvm use command.

Q. What should I do if I encounter a configuration error or an error timeout during installation?

If you face a configuration error or an error timeout, check your current repositories for updates or conflicts. Ensure your network settings are correct, and if using the EPEL repository, run yum install epel-release to ensure all dependencies are up to date.

Q. How can I ensure compatibility of native add-ons with different Node.js versions?

To ensure compatibility with native add-ons, use NVM to switch between different installed versions of Node.js and use the same version used during the development process. In addition, install development tools using sudo yum install gcc g++ make for building add-ons.

Q. Can I use Node.js to create efficient web applications on CentOS 6.3?

While Node.js can create efficient web applications, CentOS 6.3 might require additional steps or an alternative method to handle more recent software dependencies due to its older package hierarchy. It is recommended to upgrade to a newer version of CentOS or ensure that all current installation script versions are compatible.

Q. How do I resolve issues when the installation doesn’t recognize the current shell session?

If Node.js or NVM does not recognize commands in the current shell session, it might be necessary to restart the terminal or source the profile script again using source ~/.bashrc or ~/.nvm/nvm.sh to refresh the environment settings.

Q. What are some advanced uses of the command line curl for Node.js installation?

Advanced uses of command line curl include downloading binary distributions directly from NodeSource enterprise Linux repositories or configuring the yum install nodejs command with specific options to customize the installation, such as using a custom port or specifying a developer cloud environment.

Q. How do I install Node.js development tools on a system with 64-bit architecture?

To install Node.js development tools on a 64-bit architecture system, use the command sudo yum install gcc g++ make. This prepares your system for developing custom applications by compiling and installing native add-ons.

Q. What are the benefits of using the EPEL repo for Node.js installation?

Using the EPEL repo can provide access to additional packages that are not available in the current CentOS repositories, which can be beneficial for setting up the environment for Node.js development. To add the EPEL repo, run sudo yum install epel-release.

Q. How to Install Node.js and npm on CentOS 7?

To install Node.js and npm on CentOS 7, add the NodeSource repository by executing sudo yum install -y nodesource-release-el7.

Next, install Node.js and npm by executing: sudo yum install nodejs.

Q. What are Node.js and npm?

Node.js is a cross-platform JavaScript runtime environment for server-side programming, while npm is the default package manager for Node.js.

Q. Can I install Node.js and npm on the CentOS 7 server?

Yes, you can install Node.js and NPM on a CentOS 7 server using the NodeSource repository.

Q. What is the recommended way to install Node.js on CentOS 7?

The recommended way to install Node.js on CentOS 7 is using the NodeSource repository.

Q. How do I install npm from the NodeSource repository?

You can install npm from the NodeSource repository by executing the command: sudo yum install npm.

Q. What is NVM, and how can I use it to install Node.js?

NVM stands for Node Version Manager, and you can use it to easily manage and switch between different versions of Node.js on your CentOS system.

Try this guide with our instant dedicated server for as low as 40 Euros