Getting Started with Node.js on Windows

UPDATE: New packages and further items

What a difference a year makes.  Last year at the first conference solely dedicated to JavaScript, JSConf 2009, there was a big focus on the uses of JavaScript on the client side.  This year, there was a marked change with server-side JavaScript taking up about half the sessions at JSConf 2010 this past April.  The rise of server-side JavaScript has been in large part to two platforms, Narwahl and node.js.  Node in particular has garnered a bit of attention lately, especially with the node knockout which happened very recently.  The winners in the node knockout competition alone should give you some clue as to its potential.

Why Node?  Simply put, the purpose of node.js is “To provide a purely evented, non-blocking infrastructure to script highly concurrent programs.”  This stands in contrast to many other concurrency models where threads and synchronization blocks are used, which can cause any number of issues.  Because of its lock free nature, and a bigger freedom from deadlocks, it allows for a more approachable way to concurrency.  The node.js site itself has a great amount of resources here to get started.

In this series, I’m going to focus a little bit on getting started on node.js and how you can mix the Reactive Extensions for JavaScript into your node solutions.

Getting Started

One downside has been in the past for me, being a developer who uses Windows frequently (given that I’m employed by Microsoft), has been the lack of native support for Windows.  Fortunately, with the release of node.js version 0.2, there is now support for Windows, albeit through Cygwin.  Following the instructions here, I was able to get it to install on any number of machines, from XP, to Windows 7 to Windows Server 2008 R2.   

When installing Cygwin, I chose the following packages:

  • Dev (install all works just fine for all of them if space is not an issue, and I’d recommend that)
    • gcc g++ C++ compiler
    • gcc mingw-g++
    • gcc4-4++ G++ subpackage
    • git
    • make
    • openssl
    • pkg-config
    • zlib-devel
  • Python – install
  • Web
    • wget

Once that installs, before you’ll be able to successfully build anything, you should do a rebaseall by doing the following steps:

  1. Close any instances of Cygwin
  2. Launch a command prompt
  3. Execute ash.exe (commonly found in C:\Cygwin\bin)
  4. Execute ./rebaseall –v

You should see results similar to the following:

/usr/lib/abc.dll new base = 1234, new size = 1234

Now it’s time for us to pull down a fresh copy of node.js.  You can either do it from wget and unpack it as the following:

$ wget http://nodejs.org/dist/node-v0.2.0.tar.gz
$ tar xvf node-v0.2.0.tar.gz

Or using Git, we can pull that down by the following command, if you installed Git of course like I told you to:

$ git clone http://github.com/ry/node.git
$ cd node
$ git fetch --all
$ git tag
$ git checkout v0.2.0

If you’re using a proxy server, like I was in several installs, you can get around that by setting the http_proxy environment variable:

$ export http_proxy=http://someproxy:1234/

Then you can start to configure and build such as the following:

$ cd node
$ ./configure
$ make
$ make install

Let this chug for a little bit and it should build just fine.  You can check for success by displaying the version of node such as:

$ node --version
v0.2.0

Another thing to notice is that we need to configure the DNS.  Although Cygwin itself relies upon the Windows DNS for its queries, node.js uses the c-ares library that relies on /etc/resolv.conf file.  Edit this file (if found, else create it) and add the entries to the Google public DNS entries:

nameserver 8.8.8.8
nameserver 8.8.4.4

Now, it’s time to set up our first file, called example.js and put it in the root of your node install, just for simplicity sake at this point.  We’ll create the typical web server example where we write our header and a body containing Hello World from Cygwin.

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/html'});
  response.end('<b>Hello World from Cygwin</b>');
}).listen(8124);

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

And then we can start the server by the following:

$ node example.js
Server running at http://127.0.0.1:8124/

And now we can see the fruits of our labor by going to the address in our browser:

image

And there you have it, ready to roll with node.js on Windows (via Cygwin).

Conclusion

Here we’ve just scratched the surface of what we can do with node, now that it can be accessible to the many Windows developers out there.  Node, with its engaging community, is giving developers options for creating interesting scalable applications using JavaScript (yes, JavaScript).  In this series, I hope to dive a little bit into it as well as how the Reactive Extensions for JavaScript can play a part.

This entry was posted in JavaScript, node, Reactive Framework, ssjs. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Juan Lanus

    Recently, like in Feb 2013, I installed a portable node.js + MongoDB environment that might be interesting.
    I used only portable versions (except for Mongo) of the software components involved: node, git, ruby, python …
    It’s documented in detail here: http://bit.ly/YbGYVg (in my DropBox) as I was starting with all that stuff.
    You end up with a shortcut in your desktop that opens a commandline that behaves a bit like *NIX, to commands lije node, npm, mongo, git, bash (it comes with git), all hanging from a single 200MB directory that you can put into a pendrive.
    For issues or corrections pls contact me. I’m juan dot lanus at gmail .

  • Sudhir Pingale

    Thanks for the node js  installation guide i have successfully installed it on my windows.
    But can you please tell me how to install express js now.
    Thanks.

  • Devansdev

    This is a great article

  • Ajay Kumar Vegesna

    Ca we connect to databases like MySql by using node.js??

  • bseddon

    Did you ever do anything with node and RxJS?  It seems like event composition across callback from http, db and other modules could be really useful – if they can be made to work.

  • http://twitter.com/akmjenkins Adam Jenkins

    I’ve been trying to install nodejs for the past several months on my win7 machine and, with the help of google, some common sense at parts, and by and large, this tutorial, I’ve finally got it, and I just wanted to let you know that I am appreciative! Thank you very much.

  • David J Wallace

    Thank you muchly. I’ve followed a series of false leads so far, from failing to compile through MinGW, to installing node compiled with cygwin as a service… your example worked… rather easily :P

    @Adam – I read http://www.nodebeginner.org/#head7, and he explained that the double-request is favicon request related.

  • http://twitter.com/kasmura Kasper Rasmussen

    Thank you, so very much!

  • http://www.facebook.com/apostolovd Daniel Apostolov

    thank you!

  • Cam

    thanks for the tut

  • Phil

    Even if /etc/resolv.conf is set to Google DNS, ECONNREFUSED will happen on .listen() if Windows is not connected to the internet and you attempt to listen at localhost or the machine name (ie. joeslaptop etc.) Only workaround for offline Windows (such as a development VM) to listen() is to use 127.0.0.1 explicitly, neither localhost nor the machine name will resolve when offline :(

  • Adam

    Hi, I’ve run through this example on Windows using binaries from here: http://www.rafaljonca.org/d/nodejs-windows and got it working :)
    However I have a small question. If I add
    console.log(‘Served request’);
    just after the response.end line I get the console.log message twice when I hit the URL. I dont understand why this would happen?
    Thanks.

  • Andy

    NOTE, you ALSO need this cygwin package:

    openssl-devel

    Not just openssl! Noe.js will not compile with crypto without it. Can you please update your original blog post? I only found this out by finding a similar error a Ubuntu user was having

  • Karl

    v0.2.3 failed with the above error out the door, but after compiling v0.2.0 I was able to checkout the latest code and compile v0.2.3 successfully

  • dtanders

    The 0.2.3 tarball that’s up on nodejs.org just compiled for me. No telling what I’ve done to this thing over the last few years that might have made the difference, but you guys could try updating cygwin.

    Why the heck isn’t rebase part of the cygwin install process if it’s needed to load some DLLs properly?

  • Rinie

    Andy, up untill v0.2.0 cygwin builds just work fine.
    The {task: cc eio.c -> eio_1.o} occurs with v0.2.3…

  • Andy

    When I run ./configure I get
    ” error: Could not autodetect OpenSSL support. Make sure OpenSSL development packages are installed. Use configure –without-ssl to disable this message.”

    Any idea how to fix that?

    Then I run ./configure –without-ssl and it gives me its happy warning, then when I run make it craps:

    Build failed: -> task failed (err #1):
    {task: cc eio.c -> eio_1.o}

    I have all of the dependencies you list. Node.js does not seem cygwin friendly at all!

  • http://implementationdetails.com Peter Tate

    If you go get the pre-compiled binaries that Andy points to (thanks Andy) be sure to use the latest 7-zip 9.16 beta to unpack them. The current 4.65 won’t work.

  • http://node-js.prcn.co.cc Andy

    Cygwin support (unofficial?) was there for longer than the post suggests. http://node-js.prcn.co.cc has precompiled binaries for 0.1.102 (and for 0.2.0 of course)

    No fresh binaries (0.2.2?) though

  • http://www.twitter.com/mwjacks0n Matt

    No problem Paul… I was banging my head against a wall about that for a good hour, so glad to help

  • Paul

    Awesome explanation, thanks Matt!

  • http://codebetter.com/members/Matthew.Podwysocki/default.aspx Matthew.Podwysocki

    @Matt

    Good point, and I’ll update the post to reflect that.

    Matt

  • http://codebetter.com/members/Matthew.Podwysocki/default.aspx Matthew.Podwysocki

    @urza

    If you wish, you can use VS2010 to manage your JavaScript files in some sort of project and use the intellisense features. In most of my dealings, I’ve been using Notepad++, gvim and the likes.

    Matt

  • urza

    Can I use Visual Studio 2010 to ease the development with javascript/nodejs?

  • Matt

    One gotcha I struggled with… If you’re getting ECONNREFUSED errors, make sure you add your DNS server to /etc/resolv.conf as node.js doesn’t automatically use cygwins networking information for resolving external hotnames.