Using CoffeeScript over JavaScript in your Node.js Application or Module

If you’ve wanted to code your next Node.js app in CoffeeScript then it ain’t that hard. Since coffeescript is just a little language that compiles down to javascript it is pretty much compatible with node.js. Let’s take a look at some of the ways in which you can execute your node.js script (written in coffeescript).

We’ll reuse the simple web server code found on

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '');

console.log('Server running at');

Converting the code to coffeescript and putting it in a file called

http = require 'http'

http.createServer (req, res) ->
  res.writeHead 200, {'Content-Type': 'text/plain'}
  res.end 'Hello World\n'
.listen 1337, ''

console.log 'Server running at'

Simple Usage

Now you can definitely compile the to javascript and run it using the node command like this -

$ coffee -c
$ node server.js
Server running at

Even better, you can simple execute the script by executing coffee without any options -

$ coffee 
Server running at

Executable File

You may add the following shebang to your script (and make it an executable file) to use coffee as the interpreter -

#!/usr/bin/env coffee

# coffee code follows ...

Then give your script executable permission and execute it.

$ chmod +x
$ ./ 
Server running at

This approach of making your script an executable file is not something I usually pick.

Using nodemon or forever

If you’re using nodemon to watch files for changes and restart node server in your development then it works pretty well with coffee implicitly -

$ nodemon
14 Mar 01:24:32 - [nodemon] v0.7.2
14 Mar 01:24:32 - [nodemon] watching: /var/www/dev/node
14 Mar 01:24:32 - [nodemon] starting `coffee`
Server running at

Are you using forever to run your servers forever? Just use the -c option to pass a command to execute.

$ forever start -c coffee 
info:    Forever processing file:
$ forever list
info:    Forever processes running
data:        uid  command script        forever pid   logfile                          uptime      
data:    [0] iv2r coffee 16710   16712 /home/codetheory/.forever/iv2r.log 0:0:0:2.508 
$ cat ~/.forever/iv2r.log 
Server running at

via JavaScript Code

There’s one more way, you can have a index.js file that loads your (that in turn loads your entire web application).


Obviously for this you’ll need to have coffeescript installed.

$ npm install coffee-script
npm http GET
npm http 304
coffee-script@1.6.1 node_modules/coffee-script

This method is helpful when your’re writing a node module in coffeescript that other people can install via npm and re-use no matter what they use for their own codebase (javascript or coffeescript).

Hope these tips help!


One thought on “Using CoffeeScript over JavaScript in your Node.js Application or Module

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>