Centos 7 Install Node Js

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.

The importance of Node.js in modern web development cannot be overstated. It has become a fundamental tool for creating scalable and high-performance applications. Its non-blocking architecture allows for handling numerous connections simultaneously, making it an ideal choice for developing applications that require real-time data, such as chat applications, online gaming, and live streaming services.

Preparing CentOS 7 for Node.js Installation

Before installing Node.js on CentOS 7, it’s essential to prepare the system to ensure a smooth installation process. This involves updating the system packages, installing necessary dependencies, and configuring the necessary repositories.

Updating System Packages

First, update the system package index to ensure you have the latest versions of the packages and their dependencies. You can do this by running the following command:

yum update -y

Installing Necessary Dependencies

Next, install the development tools and other necessary dependencies that are required to build Node.js from source:

yum groupinstall 'Development Tools' -y
yum install -y gcc-c++ make

Configuring Node.js Repository

For CentOS 7, Node.js packages are available from the NodeSource repository. You can add the NodeSource repository to your system with the following curl command:

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

Replace “14.x” with the version of Node.js you wish to install. This command will configure the repository and import the NodeSource signing key.

Installing Node.js on CentOS 7

With the repository configured, you can now proceed to install Node.js. The following sections will guide you through the installation process using different methods.

Installing Node.js from NodeSource Repository

To install Node.js from the NodeSource repository, execute the following command:

yum install -y nodejs

This will install Node.js along with npm, which is the Node.js package manager used to install modules and packages.

Installing Node.js Using NVM (Node Version Manager)

NVM is a version manager for Node.js that allows you to install and manage multiple versions of Node.js. To install NVM, use the following curl command:

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

After installing NVM, you can install Node.js by running:

nvm install node

This will install the latest version of Node.js. To install a specific version, you can specify the version number:

nvm install 14.17.0

Verifying Node.js Installation

Once the installation is complete, you can verify that Node.js and npm are installed correctly by checking their versions:

node -v
npm -v

This will output the installed versions of Node.js and npm, respectively.

Configuring npm and Managing Packages

After installing Node.js and npm, it’s important to understand how to configure npm and manage Node.js packages.

Setting Up npm

You can configure npm to set default directories or to manage permissions. For example, to avoid permission issues, it’s recommended to configure npm to use a directory within your home folder for global installations:

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH
source ~/.profile

Installing Node.js Packages

To install a Node.js package globally, you can use the following npm command:

npm install -g package-name

Replace “package-name” with the name of the package you wish to install. For local installations within a project, navigate to the project directory and run:

npm install package-name

Setting Up a Basic Node.js Application

To demonstrate the power of Node.js, let’s set up a basic web server application.

Creating a Project Directory

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

mkdir my-nodejs-app
cd my-nodejs-app

Initializing the Project

Initialize the project with a package.json file by running:

npm init -y

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

Writing the Server Code

Create a file named “server.js” and add the following code to create a simple web server:

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

To start the server, run the following command in your project directory:

node server.js

You should see a message indicating that the server is running. You can now access the server at http://127.0.0.1:3000 in your web browser.

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.

Running Node.js as a Non-Root User

To minimize security risks, it’s best practice to run Node.js applications as a non-root user. You can create a new user and run the application under that user account.

Using Environment Variables for Sensitive Information

Never hardcode sensitive information like database passwords or API keys in your code. Instead, use environment variables to store such information.

Keeping Dependencies Updated

Regularly update your Node.js dependencies to their latest versions to include security patches and bug fixes. You can use npm to check for outdated packages:

npm outdated

Frequently Asked Questions

How do I uninstall Node.js from CentOS 7?

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

yum remove nodejs

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

Yes, you can use NVM (Node Version Manager) to install and manage multiple versions of Node.js on CentOS 7.

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

If you’re using NVM, you can install the latest version of Node.js with:

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

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

yum update nodejs

What should I do if I encounter permission issues with npm?

If you encounter permission issues, you can either fix the permissions or configure npm to use a directory within your home folder for global installations, as shown earlier in this article.

Is Node.js compatible with other software stacks like LAMP or MEAN?

Yes, Node.js can be integrated with other software stacks. It is commonly used as part of the MEAN stack (MongoDB, Express.js, AngularJS, and Node.js) and can also be used alongside LAMP (Linux, Apache, MySQL, PHP/Python/Perl) components.

References

Leave a Comment

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


Comments Rules :

Breaking News