Install Last Node.js on RHEL/CentOS 7 and Avoid Permissions Errors

Featured image

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. As an asynchronous event driven JavaScript runtime, Node is designed to build scalable network applications.

Install Node.js and npm

References:

Update the system and install necessary packages

sudo yum install curl

We will install Node.js v6 LTS and npm from the NodeSource repository which depend on the EPEL repository being available.

To enable the EPEL repository on your CentOS 7 VPS, issue the following command:

sudo yum install epel-release

Once the EPEL repository is enabled run the following command to add the Node.js v12 repository:

curl --silent --location https://rpm.nodesource.com/setup_12.x | sudo bash -

Install

Once the NodeSource repository is enabled we can proceed with the Node.js v12 and npm installation:

sudo yum install -y nodejs

To verify if the Node.js installation was successful, issue the following command:

node -v

The output should be like the following:

v12.4.0

To verify if the npm installation was successful, issue the following command:

npm -v

The output should be like the following:

6.4.1

If you want to test the installation, create a test file:

vi hello_world.js

and then add the following content:

const http = require('http');
const port = 3000;
const ip = '0.0.0.0';

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World');
}).listen(port, ip);

console.log(`server is running on ${ip}:${port}`);

Start the node web server by issuing the following command:

node hello_world.js

the output should be like the following:

server is running on 0.0.0.0:3000

If you now visit http://your_server_IP:3000 from your browser, you will see ‘Hello World’.

Prevent Permissions Errors: Change npm’s Default Directory

Source: How to Prevent Permissions Errors

To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, it will be a hidden directory on your home folder.

Make a directory for global installations:

mkdir ~/.npm-global

Configure npm to use the new directory path:

npm config set prefix '~/.npm-global'

Open or create a ~/.bashrc file and add this line:

export PATH=$PATH:~/.npm-global/bin

Back on the command line, update your system variables:

source ~/.bashrc

Test: Download a package globally without using sudo.

npm install -g express

See also

Monitor SSIS job and package executions

date_range 02/09/2020

Featured image

How to monitor SSIS job and package executions.

Enable network connectivity between Docker containers on CentOS 8

date_range 15/08/2020

Featured image

Enable a network connectivity between Docker containers on CentOS 8.

Setup a GitHub repository to serve your Sphinx documentation

date_range 07/04/2020

Featured image

Sphinx and GitHub provide an efficient and free way to publish your documentation online. Here we describe how to do so.