Node.js is a cross-platform JavaScript runtime environment, which is built on Chrome JavaScript designed to run JavaScript code on the server side. Using Node.js, you can build extended web applications.
npm, short for Node Package Manager, is the default package manager developed by Node.js for the convenience of developers to use and reuse code. It is also the world's largest software repository for publishing open source Node.js packages.
In this article, we will take you through two different ways to install Node.js and npm on CentOS 8. According to your environment, choose the method that suits you best.
Node.js and npm can be installed through standard CentOS source repositories. At the time of writing this article, the Node.js version in the repository is v10.x. List the nodejs
package by running the following command:
yum module list nodejs
The output shows that there is only one version of the nodejs module:
CentOS-8- AppStream
Name Stream Profiles Summary
nodejs 10[d] common [d], development, minimal, s2i Javascript runtime
The nodejs package provides different profiles. The default profile, marked with [d]
, will install a common runtime package.
To install the default Node.js package on your CentOS system, enter:
sudo yum module install nodejs
The above command will also install NPM.
If you are a developer, install the development profile, it will also install some additional libraries in order to build dynamically loaded modules.
sudo yum module install nodejs/development
Once the installation is complete, enter the following command to verify the installation:
node --version
This command displays the version of Node.js:
v10.16.3
This is the most convenient way to install Node.js and npm on CentOS 8, and it should satisfy most user scenarios.
For compiling and installing local plugins from npm repository, development tools must be installed. Install by running the following command:
sudo dnf groupinstall 'Development Tools'
If for some reason, you need to uninstall Node.js and npm packages, use the following command:
sudo yum module remove nodejs
NVM (Node Version Manager) is a bash script that allows you to manage multiple Node.js versions. With NVM, you can install or uninstall any Node.js version you want to use or test.
To install NVM on your CentOS system, run the following command. Don't use sudo
, because this will enable the root user to enable this script.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh | bash
This installation script will clone NVM repository on Github to the ~/.nvm
directory and add the nvm path to the Bash or ZSH profile file.
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
Start using the nvm script and open a new shell session.
Now that the nvm
script is enabled on your CentOS system, you can run the following command to install the latest stable Node.js version:
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 or more versions, the latest LTS version and version 10.16.0:
nvm install --lts
nvm install 10.16.0
Once the installation is complete, you can list all installed Node.js versions:
nvm ls
- > v10.16.0
v12.13.0
v13.0.1default->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 line with the arrow, on the right ((-> v10.16.0)
), is the Node.js version used in the current shell session. When you open a new shell session, the default Node.js version is v13.0.1
.
If you want to change the currently available version number, for example: v12.13.0
, you can run:
nvm use v12.13.0
To change the default version of Node.js, for example to v12.13.0
, use:
nvm alias default v12.13.0
We have demonstrated two ways to install Node.js and npm on CentOS 8. According to your requirements and preferences, choose your way.
Now that you have installed Node.js on CentOS 8, you can deploy your application.
Recommended Posts