What are my restrictions if I want to code node.js and use CoffeeScript? Can I do anything I'd be able to do in JS?
8 Answers
Yes, CoffeeScript simply compiles into pure JS, making it completely compatible with node.js.
To run CoffeeScripts on node, you can either:
- Type
coffee -c example.coffee
to compile, followed bynode example.js
to run the compiled JS. - Simply type
coffee example.coffee

- 92,262
- 30
- 167
- 149
-
@donald, I'm not aware of any combined CoffeeScript + Node tutorials. Learning them separately is ideal. The link I gave above for Node is a perfect introduction, and for CoffeeScript, this is all you need: http://jashkenas.github.com/coffee-script/ – David Tang Jan 13 '11 at 23:23
-
@donald here, try this one: http://nodetuts.com/tutorials/16-introduction-to-coffeescript-and-nodejs.html#video – jcollum Mar 15 '12 at 20:34
-
2But what about when using other CoffeeScript files? I don't want to put everything in 1 CoffeeScript – Jiew Meng Jun 10 '12 at 12:32
-
3@Jiew Meng you can compile a whole directory in the same way (replace example.coffee with the directory path), or you can `require` uncompiled coffeescript files as long as you `require('coffee-script')` beforehand. – David Tang Jun 12 '12 at 03:47
-
1Is running `coffee example.coffee` primarily for development? Or is it stable enough to do in production? – mrmagooey Jun 12 '13 at 11:00
-
5Yeah, what's the difference between the two choices? – Steve Bennett Oct 02 '13 at 02:50
-
4To install coffee `sudo npm install -g coffee-script` – Dmitry Polushkin Nov 06 '13 at 12:44
-
worth pointing out - I've found in some cases that only the version coffee example.coffee will work - worth giving that a shot before you try to debug javascript exceptions – Jordan Mar 09 '15 at 17:10
Not only can you run CoffeeScript files directly in Node with
coffee source.coffee
you can also require them as if they were JavaScript files. For instance, if you have lib.coffee
in a directory, you can write
require './lib'
from another CoffeeScript file in the same directory. (In order to do this from a JavaScript file, you'll have to add require 'coffee-script'
at the top.) So, you never have to do compilation explicitly under Node, unless you're packaging your project for deployment with a tool like npm.
One caveat: In stack traces, the line numbers you'll see refer to the compiled JavaScript, even when you're running CoffeeScript directly (so you don't have access to the JavaScript). A lot of folks are trying to fix this, but it's a big challenge.

- 76,828
- 33
- 160
- 196
-
1
-
client-side needs ad interpreter you have to include in HTML page. – Daniele Vrut Oct 06 '13 at 07:19
-
1@fancy, coffescript > 1.6.1 [supports source maps](http://coffeescript.org/#source-maps) – MegaTux Jun 30 '14 at 13:41
-
1As far as I can tell this is not able to be debugged without correlating JS to coffee in your head. – Eric Rini Jul 26 '14 at 00:29
-
1Coffeescript to JS conversion rules are pretty simple, they are all well documented :) – Kedare Oct 18 '14 at 07:40
-
Since CoffeeScript 1.7.0 you have to load it as follows: `require('coffee-script/register');` ([Source](https://github.com/maxtaco/coffee-script/issues/138)) – bootsmaat Mar 17 '15 at 12:04
Yes, here's a different & simpler answer. You need to do 2 steps.
npm install coffee-script --save # I assume you would have done this already
.Have
require('coffee-script')
as the first line that would get executed inserver.js
ofapp.js
. (UPDATE: since coffee script 1.7, you will have to dorequire('coffee-script/register'))
This registers coffeescript compiler to your app and you can start treating coffee files and js files equally now (meaning that you can require coffee files too !).
This method will require you to write just the one file (app.js) in vanilla javascript. But the advantage is that your deploy environment need not have coffeescript as an initial globally installed dependency to run your app. In this case, you would just have to copy over your code, and npm install
would install all packages necessary. And npm start
would have you up and running

- 11,733
- 9
- 70
- 131

- 15,589
- 9
- 43
- 57
-
2Thanks for the note about the 1.7 update @gprasant, I was pulling my hair out wondering why this stopped working. – pix0r Apr 29 '14 at 07:48
Video Tutorials
I've seen a great tutorial series by Pedro Teixeira. He's been building an entire series on node tutorials. He includes reference to nodemon for auto detection and compilation and reloading of edited .coffee files.

- 4,606
- 2
- 29
- 47
-
The http://nodetuts.com/ links above are broken. The tutorials on that site look great, though. – awhie29urh2 Dec 06 '12 at 22:34
-
thanks, the original link destinations vanished/moved. Set them to what google search revealed, the Vimeo videos @countfloortiles – Mark Essel Dec 10 '12 at 11:02
-
3See http://brunch.io for a great NodeJS daemon / asset pipeline solution. Covers Coffee, CSS supersets and more. – Lincoln B Dec 19 '12 at 22:49
-
spotted this a while back, but haven't visited since. thanks for bringing it up – Mark Essel Dec 20 '12 at 14:34
You can use Jitter, a Simple continuous compilation for CoffeeScript.
npm install -g jitter
Let's say you have a bunch of *.coffee files in the coffee directory, and want to compile them to the js directory. Then run:
jitter coffee js
Jitter runs in the background until you terminate it (Ctrl+C), watching for new changes.

- 6,554
- 2
- 28
- 24
Try this
#!/usr/bin/env coffee
v = 78
console.log "The value of v is '#{v}'"
Then do:
chmod +x demo.coffee
./demo.coffee
CoffeeScript has pretty solid integration with node. Once the 'coffee-script' module is loaded, either by require('coffee-script')
, by the she-bang I demo'd above, or by running coffee demo.coffee
... once loaded, you can used require('./foo')
to bring in foo.coffee

- 41,600
- 19
- 95
- 85
If you want to automatically compile all your coffeescript files (in one directory including subdir) every time they change into javascript, just use this command:
find . -name '*.coffee' -type f -print0 | xargs -0 coffee -wc

- 10,687
- 4
- 58
- 84

- 921
- 8
- 14
-
7What's wrong with the built-in coffeescript functionality? `coffee -o lib/ -cw src/` – aaaidan Aug 03 '12 at 05:30