Wednesday, 26 June 2013

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://

When all is downloaded move in to the node directory

$ cd node


$ make

$ sudo make install

Now you can run node to check the version

$ node -v                       node version 

$ npm -v         node package manager version      

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, '');
console.log('Server running at');

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

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.

  console.log("excessively drawn out");
}, 1000) 
  console.log("dramatically elongated");
}, 2000) 
  console.log("ridiculously stretched");
}, 3000)   
  console.log("exuberantly prolix");
}, 4000) 
  console.log("profoundly protractile");
}, 5000)    
  console.log("abundantly irrelevant");
}, 6000)
  console.log("resolutely persistent");
}, 7000) 
  console.log("ambivalently & ambiguously esoteric");
}, 8000)
  console.log("but disparately unique");
}, 9000)                     
  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.

No comments:

Post a Comment