Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to run d3 from a Cakefile

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)
  })
like image 473
dsummersl Avatar asked Oct 24 '22 07:10

dsummersl


1 Answers

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");
like image 136
mbostock Avatar answered Oct 26 '22 20:10

mbostock