Can I use CoffeeScript instead of JS for node.js?
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?
Yes, CoffeeScript simply compiles into pure JS, making it completely compatible with node.js.
To run CoffeeScripts on node, you can either:
coffee -c example.coffeeto compile, followed by
node example.jsto run the compiled JS.
- Simply type
Not only can you run CoffeeScript files directly in Node with
lib.coffee in a directory, you can write
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.
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.
require('coffee-script')as the first line that would get executed in
app.js. (UPDATE: since coffee script 1.7, you will have to do
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 !).
npm install would install all packages necessary. And
npm start would have you up and running
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.
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.
Coffeescript + ExpressJS + Couchdb + Redis + Auth: