Introduction
Node.js is a popular open-source JavaScript runtime environment that can be used to build server-side applications. It is a powerful platform for developing web applications and is widely used by developers around the world. NPM (Node Package Manager) is a package manager for Node.js that allows developers to easily install, update, and manage packages for their applications. In this tutorial, we will show you how to install Node.js and NPM on CentOS 7. We will also show you how to configure the environment variables and how to verify the installation.
How to Install Node.js and NPM on CentOS 7
1. Update your system:
sudo yum update
2. Install the required packages:
sudo yum install gcc-c++ make
3. Download the Node.js source code:
curl -sL https://rpm.nodesource.com/setup_10.x | sudo -E bash –
4. Install Node.js and npm:
sudo yum install nodejs
5. Verify the installation:
node -v
npm -v
Introduction
Node.js is an open-source cross-platform JavaScript (JS) runtime environment. If you need to build fast and scalable network applications, you may want to think about using Node.js. Apart from offering various JS modules, it is also lightweight, efficient, and supports consistent and integrated development.
Node Package Manager (npm) is Node’s official package manager, used for installing and managing package dependencies.
In this guide, learn how to install Node.js and npm on CentOS 7. We also cover managing multiple Node versions and installing dependencies.
Prerequisites
- An RHEL-based system, such as CentOS 7
- Access to a terminal/command line
- The yum package manager, included by default
- Access to the root user or a user with sudo privileges
Option 1: Install Node.js and npm from NodeSource repository
The simplest way to install Node.js and npm is from the NodeSource repository.
1. First, update the local repository to ensure you install the latest versions of Node.js and npm. Type in the following command:
sudo yum update
2. Next, add the NodeSource repository to the system with:
curl –sL https://rpm.nodesource.com/setup_10.x | sudo bash -
3. The output will prompt you to use the following command if you want to install Node.js and npm:
sudo yum install –y nodejs
4. Finally, verify the installed software with the commands:
node –version
npm –version
As you can see in the images above, the system downloaded the latest stable versions – Node.js 10.16.0, and npm 6.9.0.
Option 2: Install Node.js and npm using NVM
Step 1: Install Node Version Manager (NVM)
Alternatively, you can install Node.js and npm with the Node Version Manager (NVM), a tool practical for managing multiple Node.js versions.
1. To install NVM, download the installation script from GitHub. You will use the command line curl
.
If you do not have this prerequisite tool, install it by running:
sudo apt install curl
Press y
to confirm the installation and hit Enter.
2. After installing curl, download the NVM installation script with the command:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
The command instructs the system to clone the NVM repository and add the NVM path to ZSH or bash. Once this is done, you will receive the following message:
As the output suggests, to enable nvm you need to:
- Close and open the terminal OR
- Run the given command
3. Then, check whether the Node Version Manager installation was successful with the command:
nvm –version
The image below shows you have installed NVM 0.34.0, the latest stable version.
Step 2: Install Node.js and npm
1. Once you have installed NVM, download Node.js and npm with the command:
nvm install node
2. As always, make sure to check the version installed:
node –version
Optional: Install multiple Node.js versions using NVM
NVM is a package manager. Therefore, it can install and manage multiple Node.js versions.
To install a specific version of Node, type in nvm install
and the number of the version.
For example:
nvm install 10.1.0
or to install the latest stable version::
nvm install --lts
To view a list of the versions you have installed on your manager with:
nvm ls
This will list all installed Node.js versions, as well as specify the default and stable versions.
To switch to another version of Node.js you have already installed, use the command:
nvm use 10.16.0
Install Node.js Development Tools
Development tools is an automation tool kit that allows compiling and installing native add-ons from the npm.
To install Node.js development tools and libraries, use the command:
sudo yum install gcc g++ make
Conclusion
After following this guide, you have installed Node.js and npm on your CentOS system using the NodeSource repository or NVM. We also discussed managing multiple Node versions, development tools, and installing dependencies.
How to Install Node.js and NPM on CentOS 7
Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. It is used to develop server-side and networking applications. NPM (Node Package Manager) is a package manager for Node.js packages, or modules if you like. It is used to install, update, and uninstall packages in your development environment.
In this tutorial, we will show you how to install Node.js and NPM on a CentOS 7 system. We will also show you how to create a Node.js application and run it on the system.
Prerequisites
Before you begin, you will need to have the following:
- A CentOS 7 server with a non-root user with sudo privileges.
- Access to the internet.
Step 1 — Installing Node.js
The first step is to install Node.js on your system. We will use the NodeSource repository to install Node.js.
First, add the NodeSource repository to your system:
curl -sL https://rpm.nodesource.com/setup_10.x | sudo bash -
Once the repository is added, you can install Node.js with the following command:
sudo yum install nodejs
Once the installation is complete, you can check the version of Node.js with the following command:
node -v
You should see the following output:
v10.15.3
Step 2 — Installing NPM
NPM is installed with Node.js. You can check the version of NPM with the following command:
npm -v
You should see the following output:
6.4.1
Step 3 — Creating a Node.js Application
Now, we will create a simple Node.js application. First, create a new directory for your application:
mkdir nodejs-app
Next, change the directory to nodejs-app:
cd nodejs-app
Next, create a new file named app.js with the following content:
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 World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Save and close the file. Now, you can start the application with the following command:
node app.js
You should see the following output:
Server running at http://127.0.0.1:3000/
Now, open your web browser and type the URL http://127.0.0.1:3000/. You should see the following page:
Conclusion
In this tutorial, you have learned how to install Node.js and NPM on a CentOS 7 system. You have also learned how to create a Node.js application and run it on the system.