Thursday, January 09, 2014

How to install Node.js, npm, and use them?

1 Go to and click on Install button, 
2.. Download node and install it
3.. Create an empty folder on your hard disk
     3.1) check environment path of nodejs using command prompt
            a) C:\>set %PATH%
             If it is not showing any path regarding node then set the environment path
            b) C:\>set path=%PATH%;C:\Program Files\nodejs\
4.. Create an package.json file with the following content
      Ex: C:\Program Files\nodejs\package.json
    "name": "App",
    "version": "0.0.1",
    "description": "App",
    "dependencies": {
        "": "latest"
    "author": "developer"
5.. Open windows's command prompt (press Windows key + R and type cmd)
6.. Navigate to your newly created directory with cd command
7.. Type npm install in that directory
      7.1) If you face any problem like file not found for package.json then go to nodejs path
             C:\Program Files\nodejs\ and then type npm install
8.. Wait till everything is downloaded and installed
9.. Create a file app.js with the following content:
var app = require('http').createServer(handler)
  , io = require('').listen(app)
  , fs = require('fs')


function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      return res.end('Error loading index.html');


io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
10.. Create a file index.html with the following content
<script src="/"></script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    socket.emit('my other event', { my: 'data' });
11.. Again, go to the command prompt (console) and type node app.js. This will run nodejs server and you may open localhost:3000