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

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

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
[email protected] 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!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Author: Rishabh

Rishabh is a full stack web and mobile developer from India. Follow me on Twitter.

2 thoughts on “Using CoffeeScript over JavaScript in your Node.js Application or Module”

  1. there is a project on github amqp-coffee, I want to use that project in my nodejs project, I know I can compile the project from coffee into nodejs file, but there is too much coffee files, I think compile one by one isnt the right way, but how can i use it anyway?

Leave a Reply

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