Running and Compiling CoffeeScript

Like most scripting languages, CoffeeScript has a REPL (“read-eval-print loop,” a term that originated with Lisp) where you can run commands interactively. To enter the REPL, just run coffee:

 $ ​​coffee
 coffee>​​ ​​audience​​ ​​=​​ ​​'world'
 'world'
 coffee>​​ ​​"Hello, #{audience}!"
 'Hello, world!'

To exit the REPL, press Ctrl-d.

The REPL handily shows you the output of each expression you enter. However, editing nontrivial amounts of code on the REPL is a pain. So how do we make coffee evaluate code written in our favorite text editor?

Create a file named hello.coffee with this code:

 rl = require(​'readline'​).createInterface
  input: process.stdin
  output: process.stdout ...

Get CoffeeScript, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.