Install Node Js on 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. Its non-blocking, event-driven architecture enables efficient processing and scalability, making it an ideal choice for building modern web applications. Node.js uses the V8 JavaScript engine, the same engine that powers Google Chrome, which provides exceptional performance for server-side applications.

Prerequisites for Installing Node.js on CentOS 7

Before proceeding with the installation of Node.js on CentOS 7, it’s essential to ensure that your system meets the following prerequisites:

  • A CentOS 7 server or virtual machine
  • Access to a user account with sudo privileges
  • An internet connection to download necessary packages
  • Basic knowledge of Linux commands and the terminal

Choosing the Node.js Version

Node.js releases come in two types: Long Term Support (LTS) versions and Current versions. LTS versions are recommended for most users as they are more stable and receive security updates for a longer period. Current versions have the latest features but might not be as stable. You can choose which version to install based on your project’s requirements.

Installing Node.js on CentOS 7

Option 1: Using NodeSource Repository

NodeSource provides a repository containing the latest versions of Node.js. To install Node.js from the NodeSource repository, follow these steps:

  1. First, you need to add the NodeSource repository to your system. To do this, run the following command, replacing X.XX with the version number you wish to install:
curl -sL https://rpm.nodesource.com/setup_X.XX | sudo bash -
  1. Once the repository is added, you can install Node.js using the following command:
sudo yum install -y nodejs
  1. After the installation is complete, verify the installation by checking the version of Node.js:
node -v

Option 2: Using EPEL Repository

The Extra Packages for Enterprise Linux (EPEL) repository contains additional packages for CentOS, including an older version of Node.js. To install Node.js from EPEL, follow these steps:

  1. First, install the EPEL repository:
sudo yum install epel-release
  1. Next, install Node.js:
sudo yum install nodejs
  1. Optionally, you can install the Node.js package manager, npm:
sudo yum install npm
  1. Verify the installation of Node.js and npm:
node -v
npm -v

Option 3: Compiling from Source

For those who need a specific version of Node.js or prefer to compile from source, follow these steps:

  1. First, install the development tools required to build Node.js:
sudo yum groupinstall 'Development Tools'
  1. Download the Node.js source code from the official website. Replace X.XX.X with the version number:
curl -O https://nodejs.org/dist/vX.XX.X/node-vX.XX.X.tar.gz
  1. Extract the tarball:
tar -xvf node-vX.XX.X.tar.gz
  1. Navigate to the extracted directory:
cd node-vX.XX.X
  1. Compile and install Node.js:
./configure
make
sudo make install
  1. Verify the installation:
node -v

Configuring npm and Managing Node.js Packages

npm is the default package manager for Node.js and is used to install and manage Node.js packages. After installing Node.js, you can configure npm and start installing packages for your project.

  • Configure npm to use a global directory for global packages:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH
source ~/.profile
  • Install packages globally without using sudo:
npm install -g package-name
  • Install packages locally within your project directory:
cd /path/to/project
npm install package-name

Setting Up a Simple Node.js Application

To test your Node.js installation, you can set up a simple web server. Create a 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}/`);
});

Run the application using the command:

node app.js

You should see the message “Server running at http://127.0.0.1:3000/” in your terminal. You can now open a web browser and navigate to that address to see your Node.js server in action.

Securing Node.js Applications on CentOS 7

Security is a critical aspect of web application development. Here are some tips to secure your Node.js applications on CentOS 7:

  • Keep your Node.js version up-to-date to receive the latest security patches.
  • Use environment variables to store sensitive information instead of hardcoding them into your application.
  • Implement HTTPS using SSL/TLS certificates to encrypt data in transit.
  • Use security-related npm modules like helmet to set HTTP headers for security.
  • Regularly audit your Node.js packages for vulnerabilities using tools like npm audit or Snyk.

Troubleshooting Common Node.js Installation Issues

During the installation of Node.js on CentOS 7, you may encounter some common issues. Here are a few troubleshooting tips:

  • If you encounter a permissions error, ensure you are using sudo or have the necessary privileges.
  • If you cannot find the Node.js package in the EPEL repository, make sure the repository is enabled and up-to-date.
  • If compilation from source fails, check that all development tools and dependencies are installed.
  • If npm commands are not working, ensure that the npm directory is included in your PATH environment variable.

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

If you installed Node.js from source, you would need to manually remove the binaries and configuration files.

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

Yes, you can use version managers like nvm to install and manage multiple versions of Node.js.

How do I update Node.js to the latest version?

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

sudo yum update nodejs

For nvm-managed installations, use:

nvm install node --reinstall-packages-from=node

What should I do if npm is not installed with Node.js?

If npm is not installed, you can install it separately using:

sudo yum install npm

Is it necessary to compile Node.js from source?

Compiling from source is not necessary for most users. Pre-built binaries from the NodeSource or EPEL repositories are sufficient and easier to install.

References

For further reading and resources, you can refer to the following:

Leave a Comment

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


Comments Rules :

Breaking News