small medium large xlarge

Back to: All Forums  CoffeeScript
Jim_jdd_portrait-750x1000_pragsmall
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
Dictionary.coffee	OWL2.coffee		OWL2.txt		console.coffee
Grid.coffee		OWL2.js			Player.coffee		convert.coffee
$ node -v
v0.4.4
$ coffee -v
CoffeeScript version 1.1.0
$ coffee console.coffee 
ReferenceError: Grid is not defined
    at /Users/jim/pgm/coffee/5x5-solution-src/Classes/5x5/console.coffee:15:5
    at Object.<anonymous> (/Users/jim/pgm/coffee/5x5-solution-src/Classes/5x5/console.coffee:109:3)
    at Object.<anonymous> (/Users/jim/pgm/coffee/5x5-solution-src/Classes/5x5/console.coffee:110:4)
    at Module._compile (module.js:404:26)
    at Object.run (/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?

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

Hi Jim,

Thanks for reporting this. There are actually two things going on here. The first is my bad: In console.coffee, 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]
        randomLetter()

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
Jim_jdd_portrait-750x1000_pragsmall
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.

Trevor_burnham__goofy_headshot__pragsmall
10 May 2011, 14:30
Trevor Burnham (54 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.

Generic-user-small
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

Trevor_burnham__goofy_headshot__pragsmall
12 May 2011, 15:27
Trevor Burnham (54 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 http://npmjs.org/install.sh | 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