Centos 7 Install Node Js

admin13 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, which provides exceptional performance for I/O-intensive operations. The event-driven architecture of Node.js makes it particularly well-suited for scalable and real-time situations we often see in cloud services, IoT devices, and complex web applications.

Prerequisites for Installing Node.js on CentOS 7

Before diving into the installation process, ensure that your system meets the following requirements:

  • 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

Installing Node.js Using NodeSource Repository

One of the most popular methods to install Node.js on CentOS 7 is by using the NodeSource repository. NodeSource provides more current versions of Node.js than the default CentOS repositories.

Adding the NodeSource Repository

To add the NodeSource repository, follow these steps:

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

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

Installing Node.js from NodeSource

After adding the repository, you can install Node.js using the yum package manager:

sudo yum clean all
sudo yum install -y nodejs

This command will install both Node.js and npm (Node Package Manager).

Installing Node.js Using NVM (Node Version Manager)

NVM is a script-based tool used to manage multiple active Node.js versions. It is ideal for developers who need to switch between different versions based on project requirements.

Installing NVM on CentOS 7

To install NVM, use the following curl command:

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

Then, source your profile to add nvm to your path:

source ~/.bash_profile

Installing Node.js Using NVM

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

nvm install node # Install the latest version
nvm install 14   # Install a specific version, e.g., 14

You can also list available versions and select one to use:

nvm ls-remote    # List all available versions
nvm use 14        # Switch to use version 14

Verifying the Installation

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

node -v
npm -v

The output should display the installed versions of Node.js and npm.

Setting Up a Simple Node.js Application

To test your Node.js setup, create a simple “Hello World” application:

  1. Create a new directory for your project and navigate into it:
  2. mkdir my-node-app
    cd my-node-app
    
  3. Initialize a new Node.js project and create a package.json file:
  4. npm init -y
    
  5. Create an index.js file with the following content:
  6. 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}/`);
    });
    
  7. Run your application:
  8. node index.js
    

Visit http://127.0.0.1:3000 in your web browser to see the output.

Managing Packages with npm

npm is the default package manager for Node.js and allows you to install libraries and tools for your projects.

  • To install a package globally, use:
  • npm install -g 
    
  • To install a package as a dependency for your project:
  • npm install --save 
    
  • To install a package as a development dependency:
  • npm install --save-dev 
    

Troubleshooting Common Issues

When installing Node.js on CentOS 7, you may encounter some issues. Here are common troubleshooting steps:

  • If you get a permissions error, make sure you have sudo access.
  • In case of installation failures, check if your system is up-to-date:
  • sudo yum update
    
  • If you’re having trouble with NVM, ensure that it’s properly sourced in your profile.
  • For network-related errors, verify your internet connection and firewall settings.

FAQ Section

Q: How do I uninstall Node.js from CentOS 7?
A: You can uninstall Node.js using yum:

sudo yum remove nodejs

Q: Can I install multiple versions of Node.js on the same CentOS 7 system?
A: Yes, you can use NVM to install and manage multiple Node.js versions.

Q: How do I update Node.js to the latest version?
A: If you used NodeSource, simply run:

sudo yum update nodejs

If you’re using NVM, find the latest version with `nvm ls-remote` and then install it with `nvm install `.

Q: What should I do if npm is not working after installing Node.js?
A: Ensure that npm is correctly installed by running `npm -v`. If it’s not recognized, reinstall Node.js or manually install npm.

Conclusion

Installing Node.js on CentOS 7 is straightforward whether you choose to use the NodeSource repository, NVM, or another method. With Node.js installed, you can start building modern, efficient web applications and services. Remember to keep your system updated and regularly check for newer Node.js versions to take advantage of performance improvements and new features.

References:

Leave a Comment

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


Comments Rules :

Breaking News