私はCakeFileからD3を実行したいです
-
15-11-2019 - |
質問
コマンドラインからいくつかのD3コードを実行したいと思います。最初は私はちょうどのように試してみました:
task 'data', 'Build some data with d3', ->
d3 = require('lib/d3.v2')
console.log "d3 version = "+ d3.version
.
しかしこれはうまくいきませんでした。私はこのようなエラーを得ました:
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
.
だから...私はこの例外が私がブラウザの内側にD3を実行する必要があることを私に言っていました。私はこれをいくつかの異なる方法で試しました。基本的に、私がPhantomjsを発射したばかりだと思いました。これが私の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()
.
このルートを行ったとき、私は常にこのような例外を取得します。
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)
.
私は何を間違っていますか?
MboStockのおかげで、次の作業が得られました:
私のpackage.json:
{
"name": "app",
"version": "0.0.1",
"dependencies": {
"d3": "~2.8.0",
"jsdom": "~0.2.13"
}
}
.
私の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)
})
. 解決
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");