small medium large xlarge

Back to: All Forums  CoffeeScript
07 May 2011, 11:32
Jim Weirich (2 posts)

Hi, I’m really enjoying the book, but am having problems running the example code. In the Classes directory of the zipped source code provided, I get this:

$ ls		OWL2.txt		OWL2.js
$ node -v
$ coffee -v
CoffeeScript version 1.1.0
$ coffee 
ReferenceError: Grid is not defined
    at /Users/jim/pgm/coffee/5x5-solution-src/Classes/5x5/
    at Object.<anonymous> (/Users/jim/pgm/coffee/5x5-solution-src/Classes/5x5/
    at Object.<anonymous> (/Users/jim/pgm/coffee/5x5-solution-src/Classes/5x5/
    at Module._compile (module.js:404:26)
    at (/usr/local/lib/node_modules/coffee-script/lib/coffee-script.js:62:19)
    at /usr/local/lib/node_modules/coffee-script/lib/command.js:120:29
    at /usr/local/lib/node_modules/coffee-script/lib/command.js:90:26
    at [object Object].<anonymous> (fs.js:107:5)
    at [object Object].emit (events.js:61:17)
    at afterRead (fs.js:864:12)

(running on a MacBook Pro)

Any ideas?

07 May 2011, 18:51
Trevor Burnham (53 posts)

Hi Jim,

Thanks for reporting this. There are actually two things going on here. The first is my bad: In, the section

require './Dictionary'
require './Grid'
require './Player'
require './OWL2'

needs to be changed to

{Dictionary} = require './Dictionary'
{Grid} = require './Grid'
{Player} = require './Player'
{OWL2} = require './OWL2'

I made the mistake of changing the root from globals to exports (as is generally proper Node style) without changing the way things are required. Evidently I tested the code in the jQuery and Node chapters after making that change, but not the Classes chapter. Sorry about that.

The second problem is more, er, problematic. CoffeeScript 1.1.0 was just released, and while it fixed many bugs, it also introduced a few of its own. One of them is that the context (this) is lost in some list comprehensions, which means that the Grid constructor

class Grid
  constructor: ->
    @size = 5
    @tiles = for x in [0...@size]
      for y in [0...@size]

doesn’t work (@size is undefined). This affects the example code for the Classes, jQuery, and Node chapters. The good news is that this bug has already been fixed on CoffeeScript’s master branch. You could either install that, or revert to 1.0.1 until 1.1.1 comes out (which should be very soon). To install 1.0.1 with npm:

npm install -g coffee-script@1.0.1
07 May 2011, 20:00
Jim Weirich (2 posts)

Thanks, that fixed it.

I wasn’t aware of the distinction between globals and exports, and I see it is covered in chapter 6 (which I hadn’t got to yet). You might need to move that topic a bit earlier in the book if it is used in these examples.

Awesome job on the book, BTW. I’m really enjoying working through it.

10 May 2011, 14:30
Trevor Burnham (53 posts)

You’re right, I do need to give a brief explanation of @exports@/@require@ earlier.

An update of the book’s code for 1.1.0 is pending… in the meantime, CoffeeScript 1.1.1 has been released! I highly recommend updating immediately, as it fixes the bug mentioned above where

foo = (x for x in [0...@bar])

fails because the context is lost in the list comprehension.

12 May 2011, 14:11
Bharat Ruparel (148 posts)

Trevor, Can you provide instructions on how to “upgrade” to the latest version of Coffeescript and node js/npm? Thanks. Bharat

12 May 2011, 15:27
Trevor Burnham (53 posts)

Assuming the old installation was done using the instructions in the book, just re-running the same commands will replace the old versions with the new ones. So for instance, on a Mac with Homebrew, I’d update with

brew update
brew install node
curl | sh
npm install -g coffee-script

(The @brew update@ command tells Homebrew to update the local catalog of version numbers. npm, by contrast, always checks the remote database before installing, unless you specify a version number.)

You must be logged in to comment