The random utterances of David Arno

Beginners guide to using CoffeeScript on Windows with node.js

coffeescript logoWith Adobe’s relegation of Flash to the web sidelines last November, I finally decided that maybe I should take a serious look at JavaScript. One of the first things I discovered was a beautifully simple, elegant and lightweight “syntactic sugar” language built on JavaScript called CoffeeScript. So I figured I’d install the compiler and get experimenting…

Sometimes I figure things wrong and this was one such occasion. The CoffeeScript compiler is written in JavaScript, so to run it as a command line tool, one needs a command line JavaScript environment, which can be easier said than done. The obvious choice is node.js, but until recently, node.js didn’t work too well on Windows. So if you wanted to run the CoffeeScript compiler, doing so via node.js sat somewhere between impossible and very difficult. There are other options involving ruby and the like, but they are all complex to set up. I wanted a simple way of compiling CoffeeScript to JavaScript. That simple way came with recent releases of node.js, which finally offer good Windows support.

First of all, visit the node.js download page and download and install the Windows installer. This will install both node.js and the npm package manager as well as updating your Path variable with both the node.js folder and the npm package manager’s module install folder. Don’t forget to log out and in again for changes to %Path% to take effect!

To test the installation, open a cmd window and type node -v, which should print something like v0.6.9. Note that the exact number may vary as node.js is an active project with frequent releases. If all is OK, the next step is to install CoffeeScript. To do so, type npm install -g coffee-script. This should result in something like the following being output:

npm http GET https://registry.npmjs.org/coffee-script
npm http 200 https://registry.npmjs.org/coffee-script
npm http GET https://registry.npmjs.org/coffee-script/-/coffee-script-
1.2.0.tgz
npm http 200 https://registry.npmjs.org/coffee-script/-/coffee-script-
1.2.0.tgz
C:\Users\david\AppData\Roaming\npm\cake -> C:\Users\david\AppData\
Roaming\npm\node_modules\coffee-script\bin\cake
C:\Users\david\AppData\Roaming\npm\coffee -> C:\Users\david\AppData\
Roaming\npm\node_modules\coffee-script\bin\coffee
coffee-script@1.2.0 C:\Users\david\AppData\Roaming\npm\node_modules\
coffee-script

To test all is OK, run coffee -bpe "alert i for i in [0..10]" and it should output:

var i;

for (i = 0; i <= 10; i++) {
  alert(i);
}

If so, you now have a functional CoffeeScript compiler running on your machine and can start experimenting with this exciting new language.


Share This Post...
8 comments so far, click here to read them or add another

8 Comments so far

  1. Nik February 29th, 2012 10:40

    Thanks!! That was really helpful. The g flag is for global right?

  2. Gorm March 8th, 2012 20:27

    THANK YOU!!

    I was about to mock about with a windows/ubuntu dual-boot solution, but this was so much easier.

    Now to make some coffee(-script) ;)

  3. reds April 2nd, 2012 18:04

    Thanks for writing. works great.

  4. Samer Hijazi May 15th, 2012 11:07

    nice, and easy

  5. Christopher Elwell May 28th, 2012 19:46

    actual output for
    C:\>coffee -bpe “alert i for i in [0..10]”
    is:

    var i, _i;

    for (i = _i = 0; _i <= 10; i = ++_i) {
    alert(i);
    }

  6. ian June 29th, 2012 19:36

    I get the same output but I don’t think this is right because it makes no sense if you read the for loop. It’s saying i = _i which is = to 0 then if _i is less than or equal to 10 (what about i?) then that crazy looking increment.

    It works but it’s ridiculously redundant with the extra variable.

    I’m getting this output but also have other issues like compile error for ‘->’ so I think it’s buggy in windows

  7. kris October 9th, 2012 20:36

    How I can use it in phpstorm or Sublime text. Not sure but I thing it’s support cmd kind of functionality itself.

  8. Nic November 6th, 2012 08:40

    thanks David.. just starting with coffeescript, from flash.. heading towards angularjs, ext js and the like.. your post has saved me a lot of time. all the best with it !