Install Node Js in Centos 7

admin9 April 2024Last Update :

Understanding Node.js and Its Importance

Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to build server-side and networking applications outside of a browser. It’s built on Chrome’s V8 JavaScript engine, and it uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Prerequisites for Installing Node.js on CentOS 7

Before diving into the installation process, it’s essential to ensure that your CentOS 7 system meets the following prerequisites:

  • A CentOS 7 server
  • Access to a user account with sudo privileges
  • Access to the terminal or command line
  • Stable internet connection for downloading packages
  • Basic knowledge of Linux commands

Installing Node.js from the NodeSource Repository

One of the most straightforward methods to install Node.js on CentOS 7 is by using the NodeSource repository. NodeSource provides a repository containing the latest versions of Node.js.

Adding the NodeSource Repository

To add the NodeSource repository to your system, follow these steps:

  1. Open your terminal.
  2. Run the following curl command to add the Node.js 14.x repository:
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -

Replace “14.x” with the version of Node.js you wish to install. For the latest or other versions, visit the NodeSource repository.

Installing Node.js and npm

After adding the repository, you can install Node.js and npm (Node Package Manager) using the following command:

sudo yum install -y nodejs

This command will install both Node.js and npm on your CentOS 7 system.

Verifying the Installation

Once the installation is complete, verify it by checking the installed versions of Node.js and npm:

node -v
npm -v

These commands will display the installed versions of Node.js and npm, respectively.

Installing Node.js Using the EPEL Repository

Another method to install Node.js on CentOS 7 is by enabling the EPEL (Extra Packages for Enterprise Linux) repository.

Enabling the EPEL Repository

To enable the EPEL repository, execute the following command:

sudo yum install epel-release

Installing Node.js from EPEL

With the EPEL repository enabled, you can now install Node.js using the yum package manager:

sudo yum install nodejs

This will install the version of Node.js available in the EPEL repository, which may not be the latest version.

Installing npm Separately

If npm is not included in the Node.js package from EPEL, you can install it separately using the following command:

sudo yum install npm

Compiling Node.js from Source

For those who need a specific version of Node.js or prefer to compile from source, this method provides the most control over the installation process.

Installing Build Tools

Before compiling Node.js, you need to install the necessary build tools:

sudo yum groupinstall 'Development Tools'

Downloading the Node.js Source Code

Next, download the Node.js source code from the official website:

wget https://nodejs.org/dist/v14.16.0/node-v14.16.0.tar.gz
tar -xvzf node-v14.16.0.tar.gz
cd node-v14.16.0

Replace the version number with the one you wish to install.

Compiling and Installing Node.js

Within the source directory, compile and install Node.js using the following commands:

./configure
make
sudo make install

This process may take some time as it compiles the source code on your machine.

Managing Multiple Node.js Versions with NVM

NVM (Node Version Manager) is a tool that allows you to install and manage multiple versions of Node.js. It is especially useful for developers who need to switch between different Node.js versions for various projects.

Installing NVM

To install NVM on your CentOS 7 system, use the following curl command:

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

After installation, load NVM by running:

source ~/.bashrc

Installing Node.js Using NVM

With NVM installed, you can now install Node.js by running:

nvm install 14

Replace “14” with the version of Node.js you want to install.

Switching Between Node.js Versions

To switch between installed Node.js versions, use the following command:

nvm use 12

Replace “12” with the version you want to switch to.

Setting Up a Simple Node.js Application

To test your Node.js installation, let’s set up a simple “Hello World” application.

Creating a New Directory for Your Application

First, create a new directory for your application and navigate into it:

mkdir my-node-app
cd my-node-app

Initializing a New Node.js Project

Initialize a new Node.js project by running:

npm init -y

This will create a package.json file with default values.

Creating the Application File

Create a new file named app.js and add the following code:

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello Worldn');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Running the Application

Run your application using the following command:

node app.js

You should see a message indicating that the server is running. You can now visit http://127.0.0.1:3000 in your web browser to see the “Hello World” message.

Frequently Asked Questions

How do I uninstall Node.js from CentOS 7?

To uninstall Node.js, you can use the following command:

sudo yum remove nodejs

This will remove Node.js from your system. If you installed using NVM, you can uninstall with nvm uninstall [version].

Can I install multiple versions of Node.js on CentOS 7?

Yes, you can install multiple versions of Node.js using NVM, which allows you to switch between versions easily.

How do I update Node.js on CentOS 7?

If you installed Node.js from the NodeSource repository, you can update it using:

sudo yum update nodejs

For NVM installations, use nvm install [new_version] to install a new version and nvm alias default [new_version] to set it as the default.

What is the difference between Node.js and npm?

Node.js is a runtime environment for executing JavaScript code on the server side, while npm is a package manager for installing and managing Node.js packages.

Is Node.js free to use?

Yes, Node.js is open-source and free to use under the MIT license.

References

Leave a Comment

Your email address will not be published. Required fields are marked *


Comments Rules :

Breaking News