5

I'd like to execute some d3 code from the command line. Initially I just tried something like:

task 'data', 'Build some data with d3', ->
      d3 = require('lib/d3.v2')
      console.log "d3 version = "+ d3.version

But this didn't work. I got errors like this:

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
          ^
ReferenceError: CSSStyleDeclaration is not defined
    at /Users/mydir/Documents/classes/middleclass/app/lib/d3.min.js:1:21272
    at Object.<anonymous> (/Users/mydir/Documents/classes/middleclass/app/lib/d3.min.js:2:25395)
    at Module._compile (module.js:432:26)
    at Object..js (module.js:450:10)
    at Module.load (module.js:351:31)
    at Function._load (module.js:310:12)
    at Module.require (module.js:357:17)
    at require (module.js:368:17)
    at Object.action (/Users/mydir/Documents/classes/middleclass/Cakefile:22:10)
    at /usr/local/lib/node_modules/coffee-script/lib/coffee-script/cake.js:39:26

So...I figured this exception was telling me that I need to execute d3 inside of a browser. I tried this in a couple different ways. Basically though, I thought if I just fired up phantomjs I'd probably be able to do what I wanted to do. Here was my Cakefile:

task 'data2', 'Build some data with d3', ->
  hem = spawn 'hem', ['server']
  phantom = require('phantom')
  phantom.create (ph) ->
    ph.createPage (page) ->
      page.open 'http://localhost:9294/sandbox.html', (status) ->
        page.evaluate (-> window), (window) ->
          require = window.require
          require('lib/d3.v2')
          console.log("d3 version = "+ d3.version)
          ph.exit()
          hem.kill()

When I go this route though, I always end up getting exceptions like this:

TypeError: object is not a function
    at Object.CALL_NON_FUNCTION (native)
    at Object.<anonymous> (/Users/mydir/Documents/classes/middleclass/Cakefile:52:13)
    at Object.<anonymous> (/Users/mydir/Documents/classes/middleclass/node_modules/phantom/node_modules/dnode-protocol/index.js:274:16)
    at apply (/Users/mydir/Documents/classes/middleclass/node_modules/phantom/node_modules/dnode-protocol/index.js:143:17)
    at EventEmitter.handle (/Users/mydir/Documents/classes/middleclass/node_modules/phantom/node_modules/dnode-protocol/index.js:120:13)
    at /Users/mydir/Documents/classes/middleclass/node_modules/phantom/node_modules/dnode-protocol/index.js:81:20
    at EventEmitter.<anonymous> (/Users/mydir/Documents/classes/middleclass/node_modules/phantom/node_modules/dnode/node_modules/lazy/lazy.js:62:13)
    at EventEmitter.<anonymous> (/Users/mydir/Documents/classes/middleclass/node_modules/phantom/node_modules/dnode/node_modules/lazy/lazy.js:46:19)
    at EventEmitter.emit (events.js:67:17)
    at EventEmitter.<anonymous> (/Users/mydir/Documents/classes/middleclass/node_modules/phantom/node_modules/dnode/node_modules/lazy/lazy.js:46:39)

What am I doin wrong??


Thanks to mbostock I got the following working:

My package.json:

{
    "name": "app",
    "version": "0.0.1",
    "dependencies": {
        "d3": "~2.8.0",
        "jsdom": "~0.2.13"
    }
}

My Cakefile:

task 'd3', 'Do something with d3', ->
  jsdom = require('jsdom')
  jsdom.env({
    html: 'public/sandbox.html'
    done: (errors,window) ->
      require('d3/index.js')
      console.log("d3 version = "+ d3.version)
  })
Thorsten S.
  • 4,144
  • 27
  • 41
dsummersl
  • 6,588
  • 50
  • 65
  • It looks like you solved your issue. I personally got the same error, and fixed it with `export NODE_PATH=/usr/local/lib/node_modules` – Thomas Ahle Aug 16 '12 at 19:05

1 Answers1

3

See D3's package.json. More specifically, the file you want to require when running inside Node or similar environments is index.js rather than d3.v2.js; this file contains some special patches that make D3 compatible with the require operator.

To try it out for yourself, cd to the d3 repository, run node to create an interactive shell, and then say

var d3 = require("./");

Or, if you're in your own project folder, if you've installed D3 into node_modules/d3 via npm (npm install d3), you can say:

var d3 = require("d3");
mbostock
  • 51,423
  • 13
  • 175
  • 129
  • Thanks for answering my slightly old question - I'll try it out today. And from the d3 author no less! – dsummersl Mar 06 '12 at 14:05
  • Just thought I'd follow up to ask...since the author is on this thread...I tried 'npm install d3', and whilst var d3 = require('d3'); in node console gives me 'undefined' but I can do d3.version and get a number, putting it in app.js gives me: TypeError: Cannot read property 'BSON' of undefined...am I missing something? – pland Mar 30 '12 at 10:47
  • @pland See http://stackoverflow.com/questions/9948350 `var d3 = require("d3");` prints "undefined" in the console because that expression evaluates to undefined; confusingly, that does not mean that the `d3` variable is undefined! The fact that you can print `d3.version` demonstrates that the package is loaded correctly. – mbostock Mar 30 '12 at 19:31