# Install Node.js 16 on CentOS 8 | CentOS 7

Node.js packages are provided through the NodeSource Node.js Binary Distributions via .rpm. Add the repository to the system using the commands below:

curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo bash -

Once the repository has been configured on your CentOS server you can proceed to install Node.js 16 on CentOS 8 | CentOS 7:

sudo yum install -y nodejs

Confirm that you can start node shell:

node

Welcome to Node.js v16.4.1. Type ".help" for more information.

.exit If you need development tools to build native addons:

sudo yum install gcc-c++ make

To install the Yarn package manager run the following commands:

curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
sudo yum install yarn

Testing Node.js 16 installation on CentOS 8 | CentOS 7

Once we have installed Node.js, let’s build our first web server. Create a file named app.js containing the following contents:

tee app.js<<EOL
const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
EOL

Then run your web server using the following command

node app.js

This runs the service on port 3000

sudo ss -tuenlp | grep 3000

tcp     LISTEN   0        128            127.0.0.1:3000          0.0.0.0:*       users:(("node",pid=13377,fd=18)) ino:50295 sk:7 <->

Visit http://localhost:3000 and you will see a message saying “Hello World“

# Upgrade Node.js

sudo yum remove -y nodejs npm
Removed:
  nodejs.x86_64 1:6.17.1-1.el7                                   npm.x86_64 1:3.10.10-1.6.17.1.1.el7

Complete!

Now let’s install the new version of Node.js:

sudo yum list available nodejs

Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile

# Available Packages

nodejs.x86_64 2:12.14.1-1nodesource nodesource

sudo yum install nodejs