How to Install Node.js and npm on CentOS 8

Published on

4 min read

Install Node.js and npm on CentOS 8 Linux

Node.js is a cross-platform JavaScript runtime environment built on Chrome’s JavaScript designed to execute JavaScript code on the server-side. With Node.js, you can build scalable network applications.

npm, short for Node Package Manager is the default package manager for Node.js that helps developers to share and reuse their code. It also refers to the world’s largest software repository for the publishing of open-source Node.js packages

In this article, we’re going to walk you through two different ways to install Node.js and npm on CentOS 8. Choose the installation option that might be most appropriate for your environment.

Installing Node.js and npm from the CentOS repositories

Node.js and npm can be installed from the standard CentOS repositories. At the time of writing, the Node.js version in the repositories is v10.x.

List the modules that provide the nodejs package by running the following command:

yum module list nodejs

The output shows that the nodejs module is available with only one stream. Stream 10 represents the Node.js version.

CentOS-8 - AppStream
Name      Stream    Profiles                                Summary             
nodejs    10 [d]    common [d], development, minimal, s2i   Javascript runtime 

The nodejs package provides four different profiles. The default profile, the one marked with [d] installs a common set of runtime packages.

To install the default Node.js package on your CentOS system, type:

sudo yum module install nodejs

The command above also installs NPM.

If you are a developer, install the development profile, which also installs additional libraries necessary to build dynamically loadable modules.

sudo yum module install nodejs/development

One the installation is complete, verify it by typing:

node --version

The command displays the Node.js version:

v10.16.3

This is the easiest way to install Node.js and npm on CentOS 8 and should be sufficient for most use cases.

Installing Development Tools

The development tools are necessary for compiling and installing native add-ons from the npm registry. Install the package by running:

sudo dnf groupinstall 'Development Tools'

Uninstalling Node.js

If for some reasons you want to uninstall Node.js and npm packages, use the following command:

sudo yum module install nodejs

Installing Node.js and npm using NVM

NVM (Node Version Manager) is a bash script that allows you to manage multiple Node.js versions on a per-user basis. With NVM you can install and uninstall any Node.js version that you want to use or test.

To install NVM on your CentOS system, run the command below. Do not use sudo as it will enable the script for the root user.

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

The installation script clones the NVM repository from Github to the ~/.nvm directory and adds the nvm path to your Bash or ZSH profile.

...
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

To start using the nvm script, either open a new shell session or run the commands printed on your screen. Do whatever is easier for you.

Now that the nvm script is enabled on your CentOS, you can install the latest stable version of Node.js with:

nvm install node
...
Computing checksum with sha256sum
Checksums matched!
Now using node v13.0.1 (npm v6.12.0)
Creating default alias: default -> node (-> v13.0.1)

Let’s install two more versions, the latest LTS version and version 10.16.0:

nvm install --ltsnvm install 10.16.0

Once the installation is complete, you can list all installed Node.js versions by typing:

nvm ls
->     v10.16.0
       v12.13.0
        v13.0.1
default -> node (-> v13.0.1)
node -> stable (-> v13.0.1) (default)
stable -> 13.0 (-> v13.0.1) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/erbium (-> v12.13.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.2 (-> N/A)
lts/dubnium -> v10.17.0 (-> N/A)
lts/erbium -> v12.13.0

The entry with an arrow on the right (-> v10.16.0), is the version used in the current shell session. The default version which is used when you open new shell sessions is set to v13.0.1.

If you want to change the currently active version, let’s say to v12.13.0 you would run:

nvm use v12.13.0

To change the default Node.js, to v12.13.0 use:

nvm alias default v12.13.0

Conclusion

We have shown you two different ways to install Node.js and npm on CentOS 8. The method you choose depends on your requirements and preferences.

Now that you’ve installed Node.js on your CentOS 8 system, it’s time to deploy your application.

If you want to use Yarn to manage your application dependencies, check our tutorial on how to install and use yarn on CentOS 8 .

If you have any questions or feedback, feel free to comment below.