
Node.js is a cross-platform JavaScript run-time environment built on Chrome’s JavaScript that allows server-side execution of JavaScript code. Mostly, Node.js is used on the back-end, but it is also popular as a full-stack and front-end solution. With Node.js, you can build scalable network applications. npm is the very popular and default package manager for Node.js with large number of open-source Node.js packages. This guide explains how to install Node.js and npm on CentOS 8 system.
In this article given two different ways to install Node.js and npm on CentOS 8 system. To install from NVM is the best option if you are using Node.js for the development purposes.
Installing Node.js and npm from the CentOS repositories
You can install Node.js and npm from the standard CentOS repositories. Currenly, the Node.js version in the repositories is v10.x.
Run the below command to list the modules that provides nodejs
packages:
yum module list nodejs
It should show output as below:
CentOS-8 - AppStream Name Stream Profiles Summary nodejs 10 [d] common [d], development, minimal, s2i Javascript runtime
Here, Stream 10 represents the version of Node.js and [d]
mark represents to installs a common set of runtime packages.
By typing below command you can install default Node.js package on your CentOS system:
sudo yum module install nodejs
To build dynamically loadable modules it’s required additional libraries, run the below command to install additional libraries.
sudo yum module install nodejs/development
You can verify the installation by typing:
node --version
It will show Node.js version:
v10.16.3
Install Node.js and npm using nvm
NVM (Node Version Manager) script is used to manage multiple Node.js versions. You can install or uninstall specific Node.js versions using NVM.
At first, we need to download nvm
install script on your system using below command:
Do not use sudo as it will enable the script for the root user.
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.0/install.sh | bash
It will copy the nvm
repository from Github to the ~/.nvm directory and add the nvm path to your Bash profile.
=> 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
As you can see in output, it is suggesting to either open new shell session or run the commands to add the path to the nvm script to your current session.
You can verify the nvm installation by typing:
nvm --version
0.35.0
Now you have nvm installed on your CentOS 8 system. You can install the latest available stable version of Node.js using below command:
nvm install node
It will show output as below:
... Computing checksum with sha256sum Checksums matched! Now using node v13.3.0 (npm v6.13.1) Creating default alias: default -> node (-> v13.3.0)
Once the installation is finishe you can check the version of Node.js by typing :
node --version
v13.3.0
Next, We will install two more versions of Node.js. To do so type following:
nvm install --lts
nvm install 10.17.0
Once the both of above versions are installed we can view list using below command:
nvm ls
-> v10.17.0 v12.13.1 v13.3.0 default -> node (-> v13.3.0) node -> stable (-> v13.3.0) (default) stable -> 13.0 (-> v13.3.0) (default) iojs -> N/A (default) lts/* -> lts/dubnium (-> v12.13.1) 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 lts/dubnium -> v12.13.1
In output version with arrow is represent current shell session. Default version is set to v10.17.0
and it will be used when you open new shell sessions.
You can change the current default version of Node.js by using the following command:
nvm use 12.13.1
Now verify it by typing:
nvm current
v12.13.1
You also can set set version 12.13.1 as the default Node.js version using below command:
nvm alias default 12.13.1
You can uninstall a Node.js version type following command:
nvm uninstall 10.17.1
Install development tools
To be able to build native modules from npm we will need to install the development tools and libraries:
sudo dnf groupinstall 'Development Tools'
Uninstalling Node.js
If you want to uninstall Node.js and npm packages, use the following command:
sudo yum module install nodejs
Conclusion
Here, explained how to install Node.js and npm using different methods on your CentOS 8 system. You can choose best method as per your requirement.
If you have any questions or suggestion, please leave comment below.
Leave a Reply