Learn how to install Node.js on CentOS 8 with this comprehensive guide. Step-by-step instructions to set up a Node.js server, ensuring a smooth installation process for your Linux environment. #centlinux #linux #nodejs
Node.js is a powerful and versatile runtime environment that allows developers to execute JavaScript code outside of a web browser. This opens up a wide array of possibilities for server-side development, enabling the creation of dynamic and interactive web applications. Key features of Node.js include:
In summary, Node.js is a robust runtime environment that leverages JavaScript’s capabilities on the server side, offering high performance, scalability, and a vast ecosystem of modules and libraries. It is a popular choice for developers looking to build modern, efficient, and scalable web applications.
Node.js is written in C, C++ and JavaScript. It is distributed under MIT License and can be download from their official website or Node.js Github Repository.
Node.js is used for a wide range of applications due to its efficiency and versatility. Some of the most common use cases include:
In summary, Node.js is a versatile tool used in web development, real-time applications, IoT, microservices, and more. Its event-driven, non-blocking architecture makes it suitable for applications requiring high performance and scalability.
Read Also: How to install Nodejs in Docker
Node.js and Express.js are related but serve different purposes in web development:
In essence, Node.js is the runtime environment that allows JavaScript to run on the server side, while Express.js is a framework that simplifies and streamlines the process of building web applications and APIs using Node.js. Developers often use them together to create powerful and scalable server-side applications with JavaScript.
Read Also: How to install OpenAI on Rocky Linux 9
Recommended Online Training: OpenAI API with Node. JS Bootcamp: ChatGPT API, GPT-4, DALL·E
We have provisioned a minimal CentOS 8 KVM machine with following specifications.
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 operating system by using dnf command.
# 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 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.
# uname -r 4.18.0-193.28.1.el8_2.x86_64 # cat /etc/redhat-release CentOS Linux release 8.2.2004 (Core)
There can be two ways to install Node.js on CentOS 8. According to your requirement, you can follow any one of these methods.
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 already available. These are the most commonly used 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.
# 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 version 12 on our Linux server.
# dnf module install -y nodejs:12... 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.
# node -v v12.18.4 # npm -v 6.14.6
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 version 14 LTS and 15 are already released, but they 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.
# 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 your Linux server.
But first, list down the available versions of Node.js.
# nvm list-remote ... v14.14.0 v14.15.0 (LTS: Fermium) v14.15.1 (Latest LTS: Fermium) v15.0.0 ...
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.
# 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).
# node -v v14.15.1 # npm -v 6.14.8
Similarly, you can switch to another version of Node.js by using the NVM as follows.
# 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.
# node -v v13.6.0 # npm -v 6.13.4
You can test your Node.js server by writing a simple JavaScript.
Create a text file in vim editor.
# 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 Worldn'); }); server.listen(port, () => { console.log(`Server running at http://your-ip-address:${port}/`); });
Temporarily allow the service port 9000/tcp in Linux firewall.
# firewall-cmd --add-port=9000/tcp success
Start a Node.js debugger process by using following command.
# 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.
# curl http://nodejs-01.centlinux.com:9000 Hello World
To start building advanced applications with Node.js, you may need to read Node.js Design Patterns, 3rd Edition (PAID LINK) by Packt Publishing.
Setting up a Node.js server on CentOS 8 can significantly enhance your web development projects by providing a robust and efficient environment. Following the steps outlined in this guide will ensure a smooth installation process.
If you need any further assistance or professional help with your Node.js installation or any other server-related tasks, feel free to check out my Fiverr profile. I offer a range of services to help you with your technical needs, ensuring that your systems run optimally and securely.
Learn how to change Apache document root in Linux by following this step-by-step guide. Adjust…
Discover how to change Apache port in Linux easily. Follow our simple guide to modify…
Learn how to create a virtual host in Apache Server with this comprehensive guide. Set…
Discover 10 practical tasks for the RHCSA exam with step-by-step solutions. Boost your Linux skills…
Discover the ultimate Fail2ban configuration guide. Learn how to set up, customize, and optimize Fail2ban…
Explore the ultimate guide to VPS servers and learn everything about Virtual Private Servers, including…
This website uses cookies.