
Node.js is an open-source JavaScript runtime built on Chrome's V8 JavaScript engine primarily suitable for developing scalable network applications. NPM (Node Package Manager) is a standard package manager that allows you to build Node.js projects with all the necessary dependency packages.
This article explains how to install Node.js and NPM on Rocky Linux 9. In addition, you will install and manage multiple Node.js versions using the Node Version Manager (NVM).
Before you begin:
Node.js has three primary types of releases you should consider before choosing a specific version for your project. These include:
Use the LTS Node.js versions for production environments to develop applications with the best balance between stability and updated features.
Node.js is available in the default package repositories on Rocky Linux 9, but the included versions may be outdated. Follow the steps below to install the NodeSource repository which contains the latest Node.js version information using the DNF package manager.
Download the latest NodeSource repository script.
Run the NodeSource repository installation script.
Install Node.js and NPM on your server.
View the Node.js version.
Output:
View the NPM version.
Output:
Follow the steps below to create and run a sample web application to test Node.js on your server.
Create a new my-node-app directory for your test project.
Navigate to the my-node-app directory.
Initialize a new Node.js project with the default environment settings.
Install the Express framework using NPM.
Create a new index.js sample application file.
Add the following contents to the index.js file.
Save and close the file.
The above application code creates an Express server that runs on the host port 3000 and returns a Greetings from Vultr message when you access it on a web browser.
Start the Node.js application as a background process.
Use the Curl utility to test access to the application on the host port 3000.
Output:
Stop the Node.js background application process using its job ID. For example, 1.
NVM (Node Version Manager) allows you to install, use, and switch between different Node.js versions on a server. Follow the steps below to install NVM and manage multiple Node.js versions depending on your project needs.
Visit the NVM releases page and download the latest NVM installation script.
Run the script to install NVM.
Reload the shell environment to apply the NVM configurations in your session.
View the NVM version on your server.
Output:
Check which Node.js versions are available.
Output:
Install a specific Node.js version. For example, Node.js version 19.
List the running Node.js versions and verify the default version.
Output:
Switch to a specific Node.js version. For example, Node.js version 19.
Output:
Set the default Node.js version. For example, Node.js version 20.
You have installed Node.js on Rocky Linux 9 and managed multiple versions using NVM. You can use multiple Node.js versions to develop different applications depending on the application's dependencies. For more information, visit the Node.js documentation.
0 Comments
Be the first to comment and share your perspective with the community.