Skip to content

Install Node.js and npm

View as Markdown

A common follow-up to launching a CentOS or RHEL node is installing Node.js to run an application. Use the package source that matches your OS version and what your application needs. Skip the old Node.js 6.x setup scripts; they are obsolete.

Log into the node as root first. See Connect to a Linux node.

dnf module list nodejs
dnf install -y nodejs npm

On an older CentOS image that still uses yum, install with:

yum install -y nodejs npm
node -v
npm -v

Confirm the install with a small test server

Section titled “Confirm the install with a small test server”

Create a file:

vim test_server.js

Add this content:

const http = require("http");
http
.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Welcome");
})
.listen(3001, "127.0.0.1");
console.log("Server running at http://127.0.0.1:3001/");

Start it:

node test_server.js

You see Server running at http://127.0.0.1:3001/, and visiting that address inside the node returns Welcome. Node.js and npm are ready to use.

If a change here leaves the node unreachable, see Troubleshooting node networking and connectivity.