Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 475
donald Avatar asked Jan 13 '11 12:01

donald


People also ask

Is CoffeeScript same as JavaScript?

One crucial difference between the two languages is that TypeScript is the superset of JavaScript while CoffeeScript is a language which is an enhanced version of JavaScript. Not just these two languages but there are other languages such as Dart, Kotlin, etc. which can be compiled into JavaScript.

Why should you use CoffeeScript instead of JavaScript?

CoffeeScript is something that makes even good JavaScript code better. CoffeeScript compiled code can do everything that natively written JavaScript code can, only the code produced by using CoffeeScript is way shorter, and much easier to read.

Is js necessary for Node JS?

Node. JS is a toolkit built around Javascript, so yes, to get good at it would require Javascript knowledge.


7 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 by node example.js to run the compiled JS.
  • Simply type coffee example.coffee
like image 73
David Tang Avatar answered Oct 05 '22 22:10

David Tang


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.

like image 42
Trevor Burnham Avatar answered Oct 05 '22 22:10

Trevor Burnham


Yes, here's a different & simpler answer. You need to do 2 steps.

  1. npm install coffee-script --save # I assume you would have done this already.

  2. Have require('coffee-script') as the first line that would get executed in server.js of app.js. (UPDATE: since coffee script 1.7, you will have to do require('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

like image 37
gprasant Avatar answered Oct 05 '22 20:10

gprasant


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.

  1. Coffeescript and Node.js
  2. Nodemon
like image 22
Mark Essel Avatar answered Oct 05 '22 20:10

Mark Essel


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.

like image 43
Roger Garzon Nieto Avatar answered Oct 05 '22 21:10

Roger Garzon Nieto


Coffeescript + ExpressJS + Couchdb + Redis + Auth:

https://gist.github.com/652819

like image 20
Shripad Krishna Avatar answered Oct 05 '22 22:10

Shripad Krishna


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

like image 40
Dave Dopson Avatar answered Oct 05 '22 21:10

Dave Dopson