Developing Node.js applications on Windows

Share on Tumblr

Node.js is becoming a really popular development platform. It is originally designed to work on *nix environments and most of the modules are now working properly on Windows thanks Microsoft support, but most of the modules does not necessarily means that the modules you require will actually work; one of them: Mongoose, a driver to work with MongoDBUpdate: I just found that Mongoose is already working on Windows, but this guide is still valid if you need to work with a Linux server. To solve this situation I had struggled through different configurations and tests and so far the nicest one for me is the one I’ll describe below.

Considerations

  • It is not required that you know how to use a Linux distribution, but it is highly desirable since you’ll be doing several things in there. If you have questions about Ubuntu, I might be able to help you a bit but I am not a Linux/Ubuntu expert; I highly recommend to use Ubuntu’s forums and of course, Google.
  • As this guide is focused on developers that want/need to work with Node.js I will assume you already know what Node (and development) is.
  • You do not need to be a Virtual Machine expert, but again, some previous knowledge is recommended. Again, I might be able to help you a bit as well as Google.

Let’s rock.

Get VirtualBox

We’ll be doing our development on VirtualBox, so let’s grab the installer and install it on our machine. It is a really straight forward setup: Next, Next, Next, … Finish!

VirtualBox will add a new virtual network device to your machine, this requires administrator privileges and also will make your PC to lost connection for a couple of minutes. If you are downloading something else you may want to finish it before.

On my laptop this virtual device makes my WiFi (using Odyssey Access Client) to think I am always connected, you may want to disable it (as I did) after the installation is done.

 

Get Ubuntu ISO, preferably Server version

Download an ISO image of the easiest Linux distribution out there: Ubuntu. Remember, we are setting a development environment to program some awesome node.js code and do not want to waste several hours/days setting up a custom kernel and all that. I recommend you to get the Ubuntu 11.10 Server ISO because:

  1. It is the one used on this guide
  2. We do not need the complete UI, software, etc. that will make our virtual machine heavier to start and run, we just need a basic server to install node and some extra packages to do our job.

Create a Virtual Machine

Create a VM is really easy. We just need to set some specific values and most of them are the default ones provided by the program itself.

 

Install Ubuntu

We are on a VM on a text installer, so there is no mouse support; don’t worry just click inside the VM window and move with the direction keys and Tab key on your keyboard when required.

  • Start your VM.
  • Select your language and start install process.
  • Choose your system language and keyboard layout, configure network and hostname (I like to keep defaults :)
  • Select Yes if prompted to write changes to disk (there is no risk, we are using a virtual machine and the disk is actually just a file).
  • An important point of the installer. If you are always behind a proxy, you MUST set this when prompted to do so. If you are not sure, you can leave this empty, I have more details regarding proxy settings later.
  • I dont like automatic updates on my VM, so I wont select them. If you prefer, you can go for it.
  • I will also select OpenSSH server because we will use it later; if you forgot it or did not select it, you can install once linux is ready by doing: sudo apt-get install openssh-server
  • If at the end of the installation the installer prompts to remove the install disk, just select ok.


Get Putty and forget about yout VM Window

Using the VM window is not always the best option, specially if you need to do setups, copy and paste lines, etc. We are going to let the VM to receive connections using some ports. We’ll setup ports 22 to accept connections to our ssh server and make our life easier also we’ll add port 3000 that is defaults express port.

  • On your VM window select Devices menu and then, Network Adapters
  • Click on Port forwarding
  • On the opened window add (by clicking on the green “+” button) 2 rules, one for 3000 and one for 22 (you can add more ports now or later).
  • Click on OK buttons until all dialogs are closed
  • Download PuTTY (if you dont already have it)
  • Execute it and use localhost as your Host Name and Port 22
  • After click on connect it will bring a one time message, click on Yes
  • Login as your ubuntu user

Congratulations!, you are now connected to your Linux VM from your Windows desktop.

 

Add proxy settings if required

If you require to use proxy and you didn’t set it up during installation you’ll need to export some environment variables. This can be done automatically on your .bashrc file but since I constantly change between Proxy and Non-Proxy settings I prefer to have a simple script to setup my proxy settings when I need them. To do so, follow the next commands:

  • Move to your home folder by just doing:
cd
vi proxy
  • Hit “i” to enter –INSERT– mode
  • Copy next text and paste it on yout putty window by right clicking it
#!/bin/bash
# Note: Replace proxy:port with your actual settings

http_proxy=http://proxy:port
https_proxy=http://proxy:port
ftp_proxy=http://proxy:port

export http_proxy
export https_proxy
export ftp_proxy
  • After pasting it, hit ESC on your keyboard and then type:
:wq
  • With the above instructions you now have a text file named proxy. We’ll now set execution permissions:
chmod +x proxy

 

The file proxy is now our custom command to set our proxy settings whenever we need them instead of permanent settings. To enable proxy settings on the current session we only need to do:

. ./proxy

Note: All dots are required, that is ‘dot’ ‘space’ ‘dot’ ‘slash’ and the word ‘proxy’

 

Update and install required packages

On your putty window, enter the following commands:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential dkms linux-headers-`uname -r` libssl-dev

Note: The `uname -r` part is required, what it does is to dynamically get the kernel release of our current installation and append it to the ‘linux-headers-” text

 

Install guest additions

  • On the VM window, select Devices menu, then Install Guest Additions.
  • Now, back to putty, execute:
sudo mount -t iso9660 /dev/cdrom1 /media/cdrom
cd /media/cdrom/
sudo ./VBoxLinuxAdditions.run

Note 1: The device might be different based on your VM setup. It might also be: /dev/cdrom, /dev/cdrw, /dev/dvd.
Note 2: The command might end on “Installing the Window System drivers …fail!” this is because we are on a server installation, hence there is no graphical UI. Do not worry, we now have what we need.

cd
sudo umount /media/cdrom/

 

Setup shared folders (and share something)

One of my favorite options on VirtualBox besides the port forwarding is the option to share folders. This allows me to read and write content from my guest.

  • On the VM window, select Devices menu, then Shared folders.
  • Click on the small folder button with the green “+”.
  • On Folder path enter or search for your desired folder.
  • Select OK on all dialogs after you are done.

Now, before we can actually use the shared folder(s) we need to add our user to a special group. Back to putty!

sudo usermod -a -G vboxsf `whoami`

Note: since I do not know your user name, I am using again a command to automagically put your user name instead of mine :)

After above step is done, you’ll need to close your current session and start a new one. Just type ‘exit’ or ‘logout’ and putty should shut down. Open it again, login using same data as above and you will be now able to read and write data to your shared folder(s) from linux to windows.

Your shared folders will be under /media and the names you selected will autoprepend “sf_” to the names, so in my example my folder “Erick” inside linux is /media/sf_Erick/

A last optional (but recommended) step is to create a shortcut to your virtual machine in your desktop so you can start it without actually opnening VirtualBox window first. This is easily done by opening the VirtualBox window, right-clicking your desired VM and selecting the option “Create a shortcut on the desktop”.

 

With the above steps we are done with our Linux installation and preparation. From now on everytime we want to use our linux for node development we just need to start our VM and connect to it using putty (or just use the VM window, but I do not like it)

 

Install node

On our putty shell, execute:

wget http://nodejs.org/dist/v0.6.15/node-v0.6.15.tar.gz
tar -zxvf node-v0.6.15.tar.gz
cd node-v0.6.15/
./configure
make
sudo make install

 

Install express as a global command

On our putty shell, execute:

sudo npm install express -g

 

Create a project

Now here comes another tricky part… by some unknown reason to me, npm fails to install modules inside any place of my shared folder, but I have an easy way to workaroud this to continue working on windows and just executing my code on Linux :)

First, I need to create an express project on my windows machine, so I execute express and provide it a path to my windows desired folder:

express /media/sf_Erick/Desktop/helloworld

Since sf_Erick points to my user folder, now I have a folder on my desktop called helloworld. That will be my main development folder where I’ll create all kind of awesome code. You can think “ok, we just need to copy each file from windows to linux when we update it” but there is actually an easier way to do it :)

We’ll replicate our folder using a linux command called rsync and save it as a “custom” command so we do not keep typing all everytime. I’ll use absolute paths so you will need to update those to reflect your current settings:

cd
vi run
  • Enter edit mode typing “i”
  • Copy and paste the following text and update the initial path to where you created your node projects. In my case I like to keep some of those on my desktop.
#!/bin/bash
rsync -ruv /media/sf_501677641/Desktop/$1/ /home/`whoami`/$1/
node /home/`whoami`/$1/app.js
  • Hit ESC and then
:wq
  • Now, set execution mode to run by doing
chmod +x run

What we are doing with those commands is sync the express app provided as a command parameter and all its content from /media/sf_YOUR_FOLDER/YOUR_PATH/ to your home folder, and once it is done, execute it.

To execute my run command for “helloworld” I just need to do:

./run helloworld

The very first time you execute it it will end with an error; this is because we do not have the node modules on the folder. to fix that cd to your newly created folder and do:

npm install

Now, go back to home by doing cd and run it again

./run helloworld

And you should get a wonderful “Express server listening on port 3000 in development mode”

To test it, open your browser and navigate to http://localhost:3000

To edit your files, simply open on windows your project and update it. I also like to recommend to download (and buy) Sublime Text (2) that is an AWESOME code editor. I usually create a project inside my windows folder so I can double click it and start working on it. After any edit I want to test:

  • I save my files
  • Switch to putty
  • Ctrl + C (to stop current express server)
  • ./run MY_PROJECT
  • Back to browser and refresh

13 thoughts on “Developing Node.js applications on Windows

  1. Pingback: DRAFT: Developing Node applications on Windows | Erick Ruiz de Chavez

  2. Joan Piedra

    Erick,

    The port forwarding trick was pure genius, I wish I had known about it a few weeks ago. This definitely improves my workflow whenever I need to test something on a Linux VM.

    Thanks.

    Reply
  3. Rick

    Let me get this straight: the first step to developing node.js on Windows is to install Linux? I’m sorry, but this is a poor choice.

    A better choice would be to install node.js for Windows and deal with modules that don’t work on Windows. If it’s a question of IDEs, may I suggest WebStorm? Great node.js support right out of the box.

    Reply
    1. Erick Post author

      @Rick

      You are getting it wrong. The purpose of this guide, as stated at the beginning of the guide, is to get it working when some of your required modules do not work on windows. You can make it work several ways, like with Cygwin, but I prefer to have a real Linux on my computer. It is not a question of IDEs either, I work with Sublime Text which is a cross platform tool. This guide is intended for those who need to work with Linux tools on a Windows corporate environment (many other developers out there).

      Reply
  4. tablet pcs

    Smart, but interesting, as are many of your posts.
    I read through the past entries over the past week or two, and I must
    say I think I’m found a new favorite.

    Reply
    1. Erick Post author

      Thanks for your comment and for taking the time to read the blog :) I’ll try to keep the post flow as I’ve done over the past weeks. Feel free to drop me an email if you have questions ;) .

      Reply
  5. best web design firms

    Hmm it looks like your blog ate my first comment (it was
    super long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog. I as well am an aspiring blog writer but I’m still new to everything. Do you have any tips and hints for rookie blog writers? I’d genuinely appreciate it.

    Reply
  6. Ganon

    Thanks for your tutorial! I’ll try it this week-end but just wanted to know why did you not take the x64 version? Maybe because of compatibility of packages or not at all?

    Reply
    1. Erick Post author

      Hey, sorry for the delay on my response but I was on vacations.

      As you said, I choose the 32 bit platform mainly because of compatibility of packages, but I haven’t seen any problems on x64 (on my job we use x64 on all servers without problems).

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="">