Centos 6/RHEL install Node.js and Npm

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Installing Node.js is straightforward now as the source is available from Github and it is in the Epel repo for Centos 6. 
Both methods are shown here, however the recommended installation method is via the Epel repo.

Install from Github

Install dependencies and git (Ensure to set up kernel files)

$ sudo yum -y install binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-devel dkms git

$ git clone git://github.com/ry/node.git

When all is downloaded move in to the node directory

$ cd node

$./configure

$ make

$ sudo make install

Now you can run node to check the version

$ node -v                       node version 
v0.8.21

$ npm -v         node package manager version      
1.2.11

Install npm from Epel repo 

The recommended way to install Node.js is via the Epel repo 
as it comes ready with many essential packages.

Once you have done that you can just enable and install

$ sudo yum -y --enablerepo=epel install npm

Let's test it out by running a small script in node.

Open up a text editor and copy the following into it before saving the file as 'example.js' It builds a http server.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello this is a test file for the Node generated server on port 1337\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

That done, now open a terminal and issue the following command

$ node example.js

This runs the test script, you should get the output below


Indicating that the server is running at http://127.0.0.1:1337

Now in your browser go to that address, not forgetting the port number (1337)



I trimmed the jpeg to remove unwanted whitespace, but the image above is something like what you should be seeing, depending on  which browser you are using.

Try another test, the ubiquitous hello.js, open up your editor and type into it or copy & paste the lines below.

setTimeout(function(){
  console.log("excessively drawn out");
}, 1000) 
 setTimeout(function(){
  console.log("dramatically elongated");
}, 2000) 
 setTimeout(function(){
  console.log("ridiculously stretched");
}, 3000)   
  setTimeout(function(){
  console.log("exuberantly prolix");
}, 4000) 
 setTimeout(function(){
  console.log("profoundly protractile");
}, 5000)    
setTimeout(function(){
  console.log("abundantly irrelevant");
}, 6000)
setTimeout(function(){
  console.log("resolutely persistent");
}, 7000) 
 setTimeout(function(){
  console.log("ambivalently & ambiguously esoteric");
}, 8000)
 setTimeout(function(){
  console.log("but disparately unique");
}, 9000)                     
setTimeout(function(){
  console.log("World !");
}, 10000)
  console.log("Hello, annoyingly long");

Save it as hello.js and run it with

$ node hello.js               (wait around 10 seconds)


So now you can build things using Node.js on Centos 6 or RHEL.

More repos.


Labels: , , ,