Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language many are already familiar with from browser-based web development.
Using the NodeJS framework, you can build many useful applications. When you install NodeJS on your CentOS or any other Linux distribution, the supportive npm packages also automatically install on your system that allows developers to share and reuse the code.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform several NodeJS queries.
In this context, we shall look into different ways for the installation of NodeJS on CentOS 8 distribution through the command line.
The NodeJS along with npm can install on your CentOS 8 system by using the following methods:
i. Install NodeJS using the CentOS 8 repository
ii. Install NodeJS using the NVM
The NodeJS modules can be installed directly from the official CentOS 8 repository.
While we are writing this guide, the latest available version of NodeJS is v10.x.
i. Use the following command to list the modules that contain the NodeJS packages:
$ sudo yum module list nodejs
The output that will display on the terminal as per the command below signifies that the NodeJS package is available with multiple streams.
There are four different profiles of the NodeJS package.
The one marked with [d] is a default profile, it installs a set of common runtime packages.
ii. However, use the following command to install the Nodejs package on CentOS 8 system:
$ sudo yum module install nodejs
This command also installs the required NPM modules after, pressing the 'y' and then 'Enter' key.
The other Nodejs profile is the development profile, which installs some additional packages that are necessary to build the loadable modules dynamically.
iii. Use the following command to install a development profile of NodeJS:
$ sudo yum module install nodejs/development
iv. Once the NodeJS installation is completed, type the following command to check the installed version:
$ node --version
Using the NVM (Node Version Manager) bash script, you can install and manage the NodeJS version on need.
The NVM allows you to install or remove any NodeJS version from your CentOS 8 system that you want to test or use.
i. So, use the following script to install the NVM on your CentOS 8 system:
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
The above installation script clones the NVM repository from Github to the ~/.nvm directory and exports the nvm path to your Bash_profile.
To install the NodeJS using the nvm script, either, execute the commands that are printed on your terminal's window or open a new shell session.
ii. After that, use the following command to install the nodejs stable version:
$ nvm install node
iii. Here, install the latest version of NodeJS using the NVM by running the below-mentioned command:
$ nvm install –lts
iv. Once the NodeJS is installed on your CentOS 8 system, list the all installed versions of NodeJS by executing the following command:
$ nvm ls
v. The default version v15.12.0 is currently using on this system, but, you can also change this version and use another NodeJS version by using the following command:
$ nvm use v14.16.0
vi. To change the default version to another, use the following command:
$ nvm alias default v12.13.0
The development tools are required for compiling and installing the add-ons from the npm registry.
So, install the all necessary development tools by running the following command:
$ sudo dnf groupinstall 'Development Tools'
Press 'y' and then 'Enter' to install all necessary development packages.
Use the following command, to remove or uninstall the NodeJS and npm modules from your CentOS 8 system:
$ sudo yum module remove nodejs
This article covers the different methods to perform the installation of NodeJS on the CentOS 8 system.
Now that NodeJS is installed on your CentOS 8 system, you can use it to deploy a NodeJS application on the system.
Node.js is a cross-platform, runtime environment that was built on Chrome's JavaScript. This environment is designed to execute JavaScript code on the server-side.
To check Node version number, run the command:
# node --version