Running Node.js on Windows

2010-04-15

Let me first start by making clear that to run Node.js on Windows, you need some kind of virtualization. At the time of writing it's impossible to get Node.js to run under MinGW or related apps (MinGW is a Linux/Unix emulator for Windows. Well... sort of).

This guide tries to help you to set up a Node.js server under Windows, with a virtualized Ubuntu instance running. The process is free (and legal), so no cost is involved here. We use VirtualBox to start a virtual instance of Ubuntu and we'll use a ready Ubuntu image from virtualboxes.org.

Start by downloading VirtualBox. Install the application as usual, nothing fancy here.

Next, download a(n?) Ubuntu Image from virtualboxes. This is a hard drive image that contains a clean installed version of Ubuntu, ready to go. At the time of writing, the latest image is Ubuntu Linux 9.10, which even contains the Guest Additions which makes using VirtualBox quite a bit easier. The username for that image is "ubuntu" and the password "reverse". This is the same password you need for "sudo". I don't know what the root password is for that image (the site says it's usually "toor", but that doesn't seem to work) but if you need this guide, you probably don't need root anyways ;) If you download a different image, make sure you write down the username/password supplied by the image or you'll have a hard time logging in :)

Right, so after you've downloaded an image open VirtualBox.
Click on "New".
Click "Next".
Enter a name ("ubuntu" would fit), select "Linux" for Operating System and "Ubuntu" for version. Click "Next".
Select the amount of memory you want to give to the virtual instance. This will be it's max RAM. While the image is active, this much RAM is used from your real RAM. On my 4Gig laptop I've assigned 1Gig to Ubuntu. Different setups require different shares...
Select "Use existing hard disk" and select the image you've downloaded. Click "Next".
Click "Finish".

Now a new item will appear in the list of images by the name you gave it. Double click it to boot it. A new window will pop up, this is the virtual instance of Ubuntu. VirtualBox will show you quite a few popup hints for various reasons. Read them (or ignore them) and click them away, requesting not to be shown again when you're comfortable with their content. In the example image, the host additions are installed. This makes it better to work with a mouse within virtualizations. Otherwise you need to press ctrl to get your mouse out :p (really annoying)

Anyways, once booted (and clicking through popups), login as the login supplied with your image. For the example above that would be "ubuntu" with password "reverse". You'll see a clean Ubuntu desktop. Hurray!

Now go to the menu at the top. On your left there's an item "Applications". Go to "Applications", "Accessories", "Terminal". You should see a black window with a "prompt", which looks a lot like a DOS window. In fact, it's basically the same, except for a different operating system. Under current Windows you can get it by going to Run, typing "cmd" and executing it. It will not listen to the same commands though.

So now we're in the terminal! First we set up g++, because Node needs a compiler and at least my example image did not have one Node can use. We execute

Code: (terminal)
sudo apt-get install g++

It will ask for a sudo password, for the images this will probably be the same as the one you logged in with (for the example, it's "reverse"). This installs a c++ compiler which Node will use. This can take a few minutes (but probably no more than that).

Once we have a compiler we need to get the sources. We'll get the latest sources from Github, but before we can, we need to install Git on our Ubuntu. So we install Git:

Code: (terminal)
sudo apt-get install git-core

Another few minutes pass and Git is installed.

To download the sources we do:

Code: (terminal)
git clone git://github.com/ry/node.git

More minutes pass :)

Next the last step of the installation process, building the Node.js application. Follow these steps:

Code: (..)
cd node

./configure

make

sudo make install

Of course you wait untill each command finishes before entering the next. This will take the longest, especially the make took a few minutes on my system.

After this you are set. Node is installed!

To test you can try the following...

Code: (terminal)
nano helloworld.js

This starts a very simple editor. Now copy the next snippet below and "paste" it in the editor. In the terminal, you paste by shift+insert.

Code: (Node.js)
var sys = require('sys'),
http = require('http');
http.createServer(function (req, res) {
res.sendHeader(200, {'Content-Type': 'text/html'});
res.write('Hello World!');
res.close();
}).listen(8000);
sys.puts('Server running at http://127.0.0.1:8000/');

After you've pasted this, press ctrl+o followed by enter. This saves the file. Now press ctrl+x to exit the editor.

Run the application by calling:

Code: (terminal)
node helloworld.js

You should see node telling you that it has started to listen on localhost. Now go and click on the Firefox icon near the top-center of the screen. Go to http://127.0.0.1:8000/ and watch the magic. You've just ran your first hello world on Node.js!

You can stop the node instance running by pressing ctrl+c in the terminal window where you started it. Edit the source and repeat.

Some of the steps I've taken from this guide I've found. The other findings were my own (or that of the Ubuntu hint system ;)). Now go read the documentation and get writing! :D

Hope it helped you