Introduction
Node.js is a Javascript platform created for the purpose of server-side programming. The platform allows users to create applications that require backend functionality. This program is further used to offer full-stack services and for front-end purposes.
Node.js has a default package manager called the Node Package Manager (npm) that helps in the publication of open-source packages for Node.js.
This guide will take you through different approaches when installing Node.js and npm on a CentOS 7 server. First, we will look at how to use the yum packages from NodeSource in the installation of Node.js and then show you how to use nvm script when installing Node.js and npm.
Let’s get started.
Step 1 – Installing Node.js and Node.js Package Manager from The NodeSource
NodeSource company provides efficient Node services and ensures there is an up-to-date Node.js repository for Linux.
The following steps will help you with the installation of Node.js and npm using the NodeSource repository on CentOS server.
1. Adding NodeSource yum Repository
The first step is to include the NodeSource repository to your server. If the current Node.js LTS version is 8.x, and you require version 10, simply change the setup_8.x with setup_10.x. Then use the command below to add this yum repository:
$ curl -sL https://rpm.nodesource.com/setup_8.x | sudo bash -
2. Installing Node.js And Node.js Package Manager With yum
After enabling the repository, now start installing Node.js and npm using the command below:
$ sudo yum install nodejs
3. Verifying The installation Of Node.js and npm
To confirm that Node.js installation went through, run the commands below to print the current versions of Node.js and npm.
To confirm the actual version of Node.js run the command below:
$ node --version
The output should look like this:
V8.11.1
To check the Node.js Package Manager version:
$ npm --version
The output should look like this:
5.6.0
Step 2 – Installing Node.js using NVM
Node Version Manager (NVM) is a described as a script that helps a user to manage different versions of Node.js. You can use it in the process of installing and uninstalling a certain version of Node.js which allows the user to have several versions of Node.js you need to test or use.
Follow these steps to start installing Node.js and npm with NVM on CentOS server.
1. Installing Node Version Manager (NVM)
Start by downloading the nvm script using the command below:
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
This script will act as a replica to the nvm repository derived from Github to ~/.nvm. Add this script to Bash or the ZSH profile.
The output should look like this:
=> Close and reopen your terminal to start using nvm or run the following to use it now: export NVM_DIR="$HOME/.nvm" [ -s"$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm [ -s"$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion
From the results above, it is clear that you need to either close your terminal and reopen it or just run the following commands to include the pathway to the nvm script as well as your recent session.
To confirm that nvm is installed, run the command below:
$ nvm --version
The output should be like this:
0.33.11
2. Installing Node.js With NVM
After installing nvm on your system, now install the current Node.js version using the command below:
$ nvm install node
The output should look like this:
Downloading and installing node v10.1.0... Downloading https://nodejs.org/dist/v10.1.0/node-v10.1.0-linux-x64.tar.xz... ########################################################################100.0% Computing checksum with sha256sum Checksums matched! Now using node v10.1.0 (npm v5.6.0) Creating default alias: default -> node (-> v10.1.0)
Now, confirm the actual version of Node.js you’re using, run the following command:
$ node --version
The output should be:
V10.1.0
3. Installing different versions of Node.js with NVM
Next, install two additional versions of Node.js i.e the latest version of LTS and version 6.14.2
$ nvm install --lts $ nvm install 6.14.2
Once they are installed, type:
$ nvm ls
The output should look like this:
-> v6.14.2 # ACTIVE VERSION v8.11.1 v10.1.0 system default -> node (-> v10.1.0) # DEFAULT VERSION node -> stable (-> v10.1.0) (default) stable -> 10.1 (-> v10.1.0) (default) iojs -> N/A (default) lts/* -> lts/carbon (-> v8.11.1) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.14.2 lts/carbon -> v8.11.1
From the output, the entry (-> v6.14.2) indicates the actual version in this shell session while the entry v10.1.0 indicates the default version. When the new shells open, the default version will become active.
4. Changing The Node.js version using NVM
To modify the currently active version of Node.js, run the command below:
$ nvm use 8.11.1
Verify the version with the following:
$ nvm current
The output:
V8.11.1
5. Changing the default version of Node.js using NVM
To change the default Node.js version to 8.11.1 run the command below:
$ nvm alias default 8.11.1
Step 3: Installing The Development Tools
To can create modules with nvm, you will be required to start by installing the development tools:
$ sudo yum install gcc-c++ make
Step 4: Installing The Yarn Package Manager
Yarn is a popular package manager by JavaScript that works fine with npm. The program helps to speed up the building process of packages and reduce errors associated with network connectivity.
To get the most updated version, you have to check it out on the Yarn’s official repository and it’s the most recommended method to install Yarn version that works for the CentOS system.
Follow these steps to begin Yarn installation using yum on the CentOS server:
1. Adding Yarn yum repository
Begin by adding the GPK key to your machine together with the repository using these commands:
$ curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo $ sudo rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg
2. Installing Yarn on CentOS 7
After adding the repository, now start installing the Yarn package with the command below:
$ sudo yum install yarn
3. Verifying the Installation
To make sure the installation is successful, print the version number using the command below:
$ yarn --version
The output should look like this:
1.7.0
That’s it.
Conclusion
That’s it! You have installed Node.js and npm on your CentOS system using two different methods. The choice of your method will depend on your preferences. While the NodeSource repository helps to install the packaged version of Node.js, nvm method can be used to either add or remove multiple Node.js versions when needed.
Check out these top 3 Node.js hosting services:
- Your query to the best web hosting can end by clicking on this link.