Node.js on Windows

Thanks to Sharkie’s ongoing organisation efforts, SydJS is a thriving monthly JavaScript meeting here in Sydney. This evening they welcomed me along to talk about Node.js on Windows. Afraid of a mostly non-Microsoft crowd I rocked up with all the anti-Unix jokes I had but they turned out to be all quite friendly and it was a fun little talk.

Here’s what I ran through…

Update 18th July 2011: The latest official builds of node.js now come with a Windows executable. This is thanks to support from Microsoft.

Cygwin

Cygwin gives you a full POSIX environment on Windows. It’s great for running apps designed for Unix, but it’s pretty heavy and not very … Windows-ey. It’d be like creating a “My Documents” folder on Ubuntu.

All that being said, it’s the simplest and most reliable way of getting node running on Windows.

Works for 0.2.6 -> 0.3.1 and 0.4.0+. Anything between 0.3.1 and 0.4.0 won’t compile.

The steps (and common pitfalls) are well documented at https://github.com/joyent/node/wiki/Building-node.js-on-Cygwin-(Windows)

Once you’ve got it running in Cygwin, if you jump out to a standard Windows command prompt and run c:\Cygwin\usr\local\bin\node.exe you’ll get a nice big error. More on this later.

MinGW

The next step up from Cygwin is to compile it under MinGW. MinGW (Minimal GNU for Windows) provides the bare minimum set of libraries required to make it possible to compile Unix-y apps on Windows, avoiding the full POSIX strangehold environment that Cygwin provides.

Works for 0.3.6+.

Again, there are well documented steps for this: https://github.com/joyent/node/wiki/Building-node.js-on-mingw

Once you’ve got it running in MinGW, jump out to a standard Windows command prompt again and run c:\wherever-you-put-your-git-clone\node.exe you’ll get a nice big error.

Standalone

Now that we’ve compiled it with MinGW (you did that in the last step, right?) we’re ready to run it on Windows natively.

From a native Windows command prompt:

  1. Create a new folder (mkdir node-standalone)
  2. Copy in the node.exe you compiled in MinGW (xcopy c:\wherever-you-put-your-git-clone\node.exe node-standalone)
  3. Copy in the MinGW helper libraries (xcopy c:\mingw\bin\lib*.dll node-standalone)
  4. Run node-standalone\node
  5. Voila! It works!

Running as a Service

Next up, I wanted to host node as a service, just like IIS. This way it’d start up with my machine, run in the background, restart automatically if it crashes and so forth.

This is where nssm, the non-sucking service manager, enters the picture. This tool lets you host a normal .exe as a Windows service.

Here are the commands I used to setup an instance of the SydJS website as a service:

nssm.exe install sydjs-node c:\where-i-put-node-standalone\node.exe c:\code\SydJS\server.js
net start sydjs-node

What We Achieved

We now have node.js, running natively on Windows, as a service. Enjoy!

(Just please don’t use this in production – it’s really not ready for that yet.)

28 thoughts on “Node.js on Windows

    1. The guide needs to be updated for 0.4.3+. Uninstall gcc-g++ and install gcc4-g++, make distclean, ./configure, make and you should be back on track.

  1. Tatham,

    As your readers are primarily a .NET crowd, can I suggest that next time you write a non-standard (i.e. non windows) type article, you please provide a bit of background – what is Node.js, why would we want it in the first place, and preferably a link to their site.

    Ash

    1. Good call Ash. In this case I just wanted to throw the resources up quickly while I was still at the group, but I’ll keep that in mind for future posts.

  2. Thanks Tatham. Great info. I am part of Brisbane JS (brisjs.com) and it’s great to hear that you guys are doin cool stuff with Node down in Sydney.

  3. Many Thanks Tatham. I’ve got standalone node.exe running in about half an hour. Quite impressive.
    Is node-waf supposed to work in your setup? It seems I cannot get it working.

  4. I’ve been playing around Node.JS for a while. And it’s really a very good piece of technology. So I tried to build this simple Windows Installer package for NodeJS and some of its most common modules. You can check out my Google project page from here http://j.mp/nP3gDL.

    Currently, the modules included are MySQL and Socket.IO because they’re the most used for my part. However, I’m working on to add many more modules as they’re tested to work perfectly with Windows. I hope someone can help me add more modules, until a package manager that is Windows Compatible arrives, but for the moment, I think this will do.

    Additionally, i have pieced up this simple tutorial on how to add a simple chat system for an existing website (served by an existing web server such as Apache) by simply adding minimal code to the pages it serves and using NodeJS + SocketIO as another server to provide for asynchronous updates (e.g. chat) to the existing website. You can check it out on the wiki page of the project.

  5. Legend, thank you for the NSSM tip. Was pulling my hair out and I wasn’t going to sleep until this was sorted. 😀

  6. Is there any sample for Node with SQL Server 2008. I tried with Node-mssql but not working. Getting some error. Thx!

  7. For compiling on Windows 7. Download latest nodejs (version 6.8 at the time of this post) run the vsbuild.bat file in the root of the extracted folder. Several project files are created. Download C++ 2010 express from microsoft. Open the node.vcxproj and build. It compiled with only minimal warnings from the compiler.
    I hope this helps someone. I looked through the node documentation without success

  8. I want to run a simple webserver on port 3000 that starts when the computer starts.

    When run directly via node it brings up the windows prompt “xyz is requesting network access”.

    However, when installed as a service it fails silently and reports success.

    Any idea how to pre-approve an app for network activity or to have prompt the user on startup?

    1. This sounds like a Windows Firewall prompt. You can add an exception for either the executable, or the port, in Advanced Firewall settings.

  9. When I stop the Windows service, how does the running node process notice this? (I’d like to shut my node child processes gracefully.) Thanks!

Comments are closed.