Install Node.js on CentOS 8 Server - CentLinux

Latest

Thursday, 19 November 2020

Install Node.js on CentOS 8 Server

Install Node.js on CentOS 8 Server

Node.js is an open source JavaScript Runtime Environment. Here, you will learn how to install Node.js on CentOS 8 server.

 

Table of Contents:

     

    What is Node.js? :

    Node.js is an open-source, cross-platform, back-end, JavaScript runtime environment that executes JavaScript code outside a web browser. Node.js lets developers use JavaScript to write command line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser. Consequently, Node.js represents a "JavaScript everywhere" paradigm, unifying web-application development around a single programming language, rather than different languages for server- and client-side scripts. (Source: Wikipedia)

    Node.js is written in C, C++ and JavaScript. Node.js is distributed under MIT License and can be download from Node.js official website or Node.js Github Repository.

     

    Environment Specification:

    We have provisioned a minimal CentOS 8 KVM machine with following specifications.

    • CPU - 3.4 Ghz (2 cores)
    • Memory - 2 GB
    • Storage - 20 GB
    • Operating System - CentOS 8.2
    • Hostname – nodejs-01.centlinux.com
    • IP Address - 192.168.116.230 /24

     

    Updating Linux Software Packages:

    Connect with nodejs-01.centlinux.com as root user by using a ssh client.

    As per the best practice, update your installed software packages on Linux server by using dnf command.

    [root@nodejs-01 ~]# dnf update -y Last metadata expiration check: 0:00:20 ago on Wed 18 Nov 2020 09:43:56 PM PKT. Dependencies resolved. Nothing to do. Complete!

    Our Linux operating system is already up-to-date. Therefore, no package was updated. The output may vary on your Linux server.

    Verify the Linux operating system and Kernel version that are being used in this installation guide.

    [root@nodejs-01 ~]# uname -r 4.18.0-193.28.1.el8_2.x86_64 [root@nodejs-01 ~]# cat /etc/redhat-release CentOS Linux release 8.2.2004 (Core)

     

    Installing Node.js from Linux Yum Repository:

    The easiest method to install Node.js on CentOS 8 is by using the Linux yum repository.

    In CentOS 8 yum repositories, the two stable versions of the Node.js are available. These are the most commonly used Node.js versions and may work in most situations.

    To get a list of available versions of Node.js in Linux yum repository, you can use following dnf command.

    [root@nodejs-01 ~]# dnf module list nodejs Last metadata expiration check: 0:07:05 ago on Wed 18 Nov 2020 09:43:56 PM PKT. CentOS-8 - AppStream Name Stream Profiles Summary nodejs 10 [d] common [d], development, minimal, s2i Javascript runtime nodejs 12 common [d], development, minimal, s2i Javascript runtime Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

    Install your required version of Node.js using dnf command, we are installing the Node.js v12 on our Linux server.

    [root@nodejs-01 ~]# dnf module install -y nodejs:12 Last metadata expiration check: 0:11:10 ago on Wed 18 Nov 2020 09:43:56 PM PKT. Dependencies resolved. ================================================================================ Package Arch Version Repo Size ================================================================================ Installing group/module packages: nodejs x86_64 1:12.18.4-2.module_el8.2.0+530+cb1b9c8b AppStream 10 M npm x86_64 1:6.14.6-1.12.18.4.2.module_el8.2.0+530+cb1b9c8b AppStream 3.8 M Installing weak dependencies: nodejs-docs noarch 1:12.18.4-2.module_el8.2.0+530+cb1b9c8b AppStream 4.0 M nodejs-full-i18n x86_64 1:12.18.4-2.module_el8.2.0+530+cb1b9c8b AppStream 7.5 M Installing module profiles: nodejs/common Enabling module streams: nodejs 12 Transaction Summary ================================================================================ Install 4 Packages Total download size: 26 M Installed size: 109 M Downloading Packages: (1/4): nodejs-docs-12.18.4-2.module_el8.2.0+530 149 kB/s | 4.0 MB 00:27 (2/4): npm-6.14.6-1.12.18.4.2.module_el8.2.0+53 125 kB/s | 3.8 MB 00:31 (3/4): nodejs-full-i18n-12.18.4-2.module_el8.2. 129 kB/s | 7.5 MB 00:59 (4/4): nodejs-12.18.4-2.module_el8.2.0+530+cb1b 154 kB/s | 10 MB 01:08 -------------------------------------------------------------------------------- Total 376 kB/s | 26 MB 01:09 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Running scriptlet: npm-1:6.14.6-1.12.18.4.2.module_el8.2.0+530+cb1b9c8b 1/1 Preparing : 1/1 Installing : nodejs-docs-1:12.18.4-2.module_el8.2.0+530+cb1b9c8b. 1/4 Installing : nodejs-full-i18n-1:12.18.4-2.module_el8.2.0+530+cb1b 2/4 Installing : npm-1:6.14.6-1.12.18.4.2.module_el8.2.0+530+cb1b9c8b 3/4 Installing : nodejs-1:12.18.4-2.module_el8.2.0+530+cb1b9c8b.x86_6 4/4 Running scriptlet: nodejs-1:12.18.4-2.module_el8.2.0+530+cb1b9c8b.x86_6 4/4 Verifying : nodejs-1:12.18.4-2.module_el8.2.0+530+cb1b9c8b.x86_6 1/4 Verifying : nodejs-docs-1:12.18.4-2.module_el8.2.0+530+cb1b9c8b. 2/4 Verifying : nodejs-full-i18n-1:12.18.4-2.module_el8.2.0+530+cb1b 3/4 Verifying : npm-1:6.14.6-1.12.18.4.2.module_el8.2.0+530+cb1b9c8b 4/4 Installed: nodejs-1:12.18.4-2.module_el8.2.0+530+cb1b9c8b.x86_64 nodejs-docs-1:12.18.4-2.module_el8.2.0+530+cb1b9c8b.noarch nodejs-full-i18n-1:12.18.4-2.module_el8.2.0+530+cb1b9c8b.x86_64 npm-1:6.14.6-1.12.18.4.2.module_el8.2.0+530+cb1b9c8b.x86_64 Complete!

    After successful installation, check the version of Node.js and Node Package Manager (NPM) versions.

    [root@nodejs-01 ~]# node -v v12.18.4 [root@nodejs-01 ~]# npm -v 6.14.6

     

    Installing Node.js using NVM:

    Although it is quiet convenient to install Node.js from Linux yum repository. But you may not found the latest versions of Node.js therein.

    For Example, Node.js v14 LTS and v15 are already released, but are not available in Linux yum repository.

    Therefore, if you wish to install latest version of Node.js then you have to use the Node Version Manager (NVM).

    By using NVM you can easily install/uninstall different versions of Node.js on/from your Linux server.

    NVM is available at GitHub. You can see complete project details at NVM Github repository.

    You can install NVM by using the script provided in the NVM documentation.

    [root@nodejs-01 ~]# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.0/install.sh | bash % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 13527 100 13527 0 0 7056 0 0:00:01 0:00:01 --:--:-- 7052 => Downloading nvm as script to '/root/.nvm' => Appending nvm source string to /root/.bashrc => Appending bash_completion source string to /root/.bashrc => 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

    NVM has been installed, you can now use nvm command it to install Node.js on Linux server.

    But first, list down the available versions of Node.js.

    [root@nodejs-01 ~]# nvm list-remote ... v14.10.0 v14.10.1 v14.11.0 v14.12.0 v14.13.0 v14.13.1 v14.14.0 v14.15.0 (LTS: Fermium) v14.15.1 (Latest LTS: Fermium) v15.0.0 v15.0.1 v15.1.0 v15.2.0 v15.2.1

    Although, Node.js v15 is available, but it is a better approach to install the LTS (Long Term Support) version especially if your are installing Node.js on a production machine.

    You can install the latest LTS version of Node.js by using nvm command.

    [root@nodejs-01 ~]# nvm install --lts Installing latest LTS version. Downloading and installing node v14.15.1... Downloading https://nodejs.org/dist/v14.15.1/node-v14.15.1-linux-x64.tar.xz... ######################################################################### 100.0% Computing checksum with sha256sum Checksums matched! Now using node v14.15.1 (npm v6.14.8) Creating default alias: default -> lts/* (-> v14.15.1)

    Verify the installed versions of Node.js and Node Package Manager (NPM).

    [root@nodejs-01 ~]# node -v v14.15.1 [root@nodejs-01 ~]# npm -v 6.14.8

    Similarly, you can switch to another version of Node.js by using the NVM as follows.

    [root@nodejs-01 ~]# nvm install v13.6.0 Downloading and installing node v13.6.0... Downloading https://nodejs.org/dist/v13.6.0/node-v13.6.0-linux-x64.tar.xz... ######################################################################### 100.0% Computing checksum with sha256sum Checksums matched! Now using node v13.6.0 (npm v6.13.4)

    Again check the installed versions of Node.js and NPM.

    [root@nodejs-01 ~]# node -v v13.6.0 [root@nodejs-01 ~]# npm -v 6.13.4

     

    Create & Deploy a Simple Node.js Application:

    You can test your Node.js server by writing a simple JavaScript.

    Create a text file in vim editor.

    [root@nodejs-01 ~]# vi Node.js_test.js

    Add following lines of code in this file.

    const http = require('http'); const port = 9000; const server = http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(port, () => { console.log(`Server running at http://your-ip-address:${port}/`); });

    Temporarily allow the service port 9000/tcp in Linux firewall.

    [root@nodejs-01 ~]# firewall-cmd --add-port=9000/tcp success

    Start a Node.js debugger process by using following command.

    [root@nodejs-01 ~]# node --inspect Node.js_test.js Debugger listening on ws://127.0.0.1:9229/d25c2fe9-b50e-4bfb-aa45-1d6988d5d470 For help, see: https://nodejs.org/en/docs/inspector Server running at http://your-ip-address:9000/

    Use a web browser or following Linux command to test your Node.js application.

    [root@nodejs-01 ~]# curl http://nodejs-01.centlinux.com:9000 Hello World

     

    Conclusion:

    In this article, you have installed Node.js on CentOS 8 server and deploy a test JavaScript on it. To start building advanced applications with Node.js, you may need to read Node.js Design Patterns, 3rd Edition by Packt Publishing.

    No comments:

    Post a comment