Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoffeeScript: coffee -w name-of-file.coffee complains: “window is not defined”

Tags:

coffeescript

In CofeeScript I am creating a global object by doing this:

window.App = 
  init : ->
    ...

Running coffee -w app.coffee complains window is not defined and doesn't rewrite the app.js file.

However, running coffee -c app.coffee compiles without a problem. How can I get coffee -w to accept global window?

CoffeeScript version is 1.1.1 (from coffee -v)

Thanks!

like image 233
Misha Reyzlin Avatar asked Jun 14 '11 17:06

Misha Reyzlin


1 Answers

If you want to watch a file and have it compiled you need to do:

coffee -wc file.coffee

Using only the -w flag causes coffee to just run the script when it changes, as if you had run:

coffee file.coffee

In regards to the window is not defined error, if you want to make your script runnable both in a browser and in node.js, then you can do this:

root = exports ? this

class Thing
  constructor: (@name) ->
  whoAreYou: ->
    alert @name

root.Thing = Thing

Another useful flag combination is -wp which just pipes the compile javascript to standard out each time you make a change to the file.

like image 145
Acorn Avatar answered Nov 01 '22 19:11

Acorn