Install Node.js on CentOS / Red Hat

Node.js is not available in default CentOS / Red Hat package repository but is available in EPEL (Extra Packages for Enterprise Linux) repository. It however hosts slightly outdated version thus require installation from NodeSource repository if you require latest versions of Node.js.

Install official version of Node.js for Centos / Red Hat

  1. Enable epel-release repository by installing the epel-release package.
    $ sudo yum install -y epel-release
  2. Install nodejs package. This will also install npm as dependency.
    $ sudo yum -y install nodejs
    ##########################
    
    ==============================================================================================
     Package              Arch            Version                             Repository     Size
    ==============================================================================================
    Installing:
     nodejs               x86_64          1:6.14.3-1.el7                      epel          4.7 M
    Installing for dependencies:
     http-parser          x86_64          2.7.1-5.el7_4                       base           28 k
     libuv                x86_64          1:1.22.0-1.el7                      epel          127 k
     npm                  x86_64          1:3.10.10-1.6.14.3.1.el7            epel          2.5 M
    
    Transaction Summary
    ==============================================================================================
    Install  1 Package (+3 Dependent packages)
    ##########################
  3. Check installed node and npm version.
    $ node -v
    v6.14.3
    $ npm -v
    3.10.10

Install latest version of Node.js for CentOS / Red Hat from NodeSource repository

  1. Install curl if it's not already available in your system.
    $ sudo yum install -y curl
  2. Add and enable NodeSource's Node.js repository.
    $ curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -

    Replace 10.x with 8.x or 9.x depending on your actual required version

  3. Install nodejs.
    $ sudo yum install -y nodejs
    ##########################
    ==============================================================================================
     Package          Arch             Version                         Repository            Size
    ==============================================================================================
    Installing:
     nodejs           x86_64           2:10.9.0-1nodesource            nodesource            18 M
    
    Transaction Summary
    ==============================================================================================
    Install  1 Package
    ##########################

    npm is automatically installed in nodejs package and does not require separate installation.

  4. Check version of installed node and npm.
    $ node -v
    v10.9.0
    $ npm -v
    6.2.0