Node.js is an open-source, cross-platform runtime environment for executing JavaScript code on the server-side. Built on Chrome’s V8 JavaScript engine, Node.js allows developers to build scalable network applications with ease. This article will guide you through the process of installing Node.js on CentOS Stream 9 and RHEL 9 (Red Hat Enterprise Linux).
Prerequisites
- A CentOS Stream 9 or RHEL 9 system with root or sudo user access.
- Access to a terminal or command-line interface.
Step 1: Update Your System
Before installing Node.js, it’s essential to ensure your system is up to date. Open your terminal and execute the following command:
sudo dnf update -y
Step 2: Install Node.js from the AppStream Repository
CentOS Stream 9 and RHEL 9 offer Node.js through the AppStream repository. You can install Node.js by running the following command:
sudo dnf module install nodejs -y
After the installation is complete, check the installed version of Node.js by executing the following command:
node --version
Step 3: Alternative Installation Method using NodeSource Repository (Optional)
If you prefer to install a specific version of Node.js or need a more up-to-date version than the one provided by the AppStream repository, you can use the NodeSource repository.
First, you will need to add the NodeSource repository to your system by executing the following command. Replace ’18.x’ with the desired version number (e.g., ’16.x’).
curl -sL https://rpm.nodesource.com/setup_18.x | sudo bash -
Now, install Node.js by running the following command:
sudo dnf install nodejs -y
To verify the installed version of Node.js, execute:
node --version
Step 4: Install NPM (Node Package Manager)
Node.js comes with NPM (Node Package Manager) by default. However, if you need to install it separately, run the following command:
sudo dnf install npm -y
Check the installed version of NPM by executing:
npm --version
Step 5: Test Your Node.js Installation
To verify your Node.js installation is working correctly, create a simple JavaScript file named “server.js” with the following content:
1 2 3 4 5 6 7 8 9 10 11 | const http = require('http'); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, Node.js on CentOS Stream 9 & RHEL 9!\n'); }); server.listen(3000, () => { console.log('Server running on http://localhost:3000/'); }); |
Save the file and run it using the following command:
node server.js
Open your web browser and visit http://localhost:3000/. You should see the message “Hello, Node.js on CentOS Stream 9 & RHEL 9!”.
Conclusion
You have successfully installed Node.js on your CentOS Stream 9 or RHEL 9 system. Now you can start developing server-side JavaScript applications or use various tools and frameworks built on Node.js. Remember to keep your Node.js installation up-to-date to take advantage of new features, performance improvements, and security fixes.