Installing Node.js On Windows

Node.js is an evented system, that allows JavaScript developers to write fast, non-blocking, applications that run independent of the client and have access to aspects of the underlying operating system such as the file system.

Installing Node.js on systems such as Linux or Mac OSX is straight forward and simple but, installing it under Windows is a little more tricky, that is until the port of Node.js to Windows is complete, allowing Node.js to run natively on Windows.

Your first step is to download and install Cygwin. For Nodejs you can grab the zip archive (remember, even numbers = stable releases, uneven numbers != stable) or you can clone the repository directly from Github. I am using the clone route for this article.

Getting Cygwin Ready

Whether you are going to work from a clone using Git or not, we need to step back for a little bit and add a couple of additions to Cygwin before we can continue. To install extra features run the setup.exe file you downloaded when initially installing Cygwin. Once you get to the select packages window, type git into the search input.

From the result, open up the develop tree and select git and git-completion(optional):

Adding Git support

Next type in python, open up the interpreters tree and select the Python interpreter:

Adding Python Support

Next we need to add a C++ compiler, type g++ into the search input, open up the develop tree and select gcc-g++

Adding the C++ Compiler

We also need to add the openssl-develop package. Type in openssl, expand the Develop tree and select openssl-develop

Adding OpenSSL Support

Lastly, we need the all important make utility. Type make into search, expand the Develop tree and select make from the list:

Adding the make utility

Accept the required packages and click next again. Once the installation is complete, we can continue.

Getting The Nodejs Source

Open up Cygwin again and navigate to the directory you want to clone Nodejs to:

cd /cygdrive/c/github/

Now run the following command to clone the repo:

git clone https://github.com/joyent/node.git nodejs

After this is complete, move into the nodejs directory. We now need to checkout the version we want to build so, instead of just a plain checkout, we will specify the version we want:

git checkout v0.4.12

NOTE: If you have AVG AntiVirus installed it is going to mark the above process as a possible virus and prevent the checkout from happening successfully. Head into the AVG preferences and under ‘Resident Shield’ add an exception that points to the path where you Nodejs source will be located.

Building Node

Once the checkout is complete, run:

./configure

NOTE: If when running the above command you get an error stating “unable to remap…….”, you will need to rebase. To do this, close your current Cygwin window and open the Windows command prompt.

From here navigate to the bin directory inside Cygwin, for example

c:/cygwin/bin

From here run ash.exe. At the resulting bash, type /bin/rebaseall

Now, open up your Cygwin shell again and rerun configure. Once completed, run make and lastly run make install. Once this is DONE, you have a Nodejs environment on Windows.

Testing Our Installation

To test that our environment does indeed work, let’s use the staple Hello World example from the Nodejs site. In a folder anywhere on your computer, create a new file and call it hello.js, inside this, add the following:

var http = require('http');
  http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
  }).listen(1337, "127.0.0.1");

console.log('Server running at http://127.0.0.1:1337/');

Now, navigate to this file location using Cygwin and run the following command:

node hello.js

If you get a response such as “Server running at http://127.0.0.1:1337/”, Nodejs is working. For more proof, open up that URL and stare in amazement. And that is it, a working Nodejs installation on your Windows machine, enjoy!

I will be following this article up with a short one on setting up NPM, the Nodejs package manager, on Windows. On a side note, there is currently a native node.exe installer for Windows but, this is in the unstable 0.5.8 branch so, although I have been told it should be fine for testing and local development, if you wanna live on the edge, give it a go.

7 responses

  1. jmdesp wrote on :

    IMO requiring cygwin is no-go. A MinGW version would be acceptable.

    Some people seem to say ( http://blog.tatham.oddie.com.au/2011/03/16/node-js-on-windows/ )the Windows executable of the unstable version (v0.5.9 – http://blog.nodejs.org/2011/10/10/node-v0-5-9/ ) is already actually quite usable.

    And of course, I would love to get to see a windows version of SpiderNode …

  2. Schalk Neethling wrote on :

    Honestly, I have never used MinGW and have always stuck with the old reliable Cygwin 😉 I am definitely going to look into MinGW though, thanks for the note. Regarding the Windows executable, I have also heard that for local developmment, it should be stable enough but, definitely not for production.

    Thanks for the comment.

  3. Oskar Eriksson wrote on :

    Thank a bunch for this!

    I’m getting an error while trying to run the ./configure, though.
    These are the error messages:

    ./configure: line 2: $’\r’: command not found
    ./configure: line 22: syntax error: unexpected end of file

    The first one is easy enough, it’s just to remove the empty line, but the end of file error has me confused. Any ideas how to fix this would be much appreciated!

    Best,
    Oskar

  4. Schalk Neethling wrote on :

    Hey Oskar, which version of Node are you building?

  5. Oskar wrote on :

    v0.4.12, if I’m not mistaken 🙂 I do get “HEAD is now at 771ba34… Bump version to v0.4.12” after the checkout.

  6. Schalk Neethling wrote on :

    Hey Oskar, have a read here, http://cs.nyu.edu/~yap/prog/cygwin/FAQs.html#dos2unix , it seems it is a Window to Unix file format issue.

  7. Oskar wrote on :

    Yep, that did it! Had to configure some permissions in the new file though (just compare the permissions in the old and the new file and make sure they’re the same). Thanks a lot for the help!