2012-03-01 24 views
5

Tôi muốn thực hiện một số mã d3 từ dòng lệnh. Ban đầu tôi chỉ thử một số nội dung như:Tôi muốn chạy d3 từ một Cakefile

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

Nhưng điều này không hiệu quả. Tôi gặp lỗi như sau:

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 

Vì vậy, tôi đã cho rằng ngoại lệ này cho tôi biết rằng tôi cần thực thi d3 bên trong trình duyệt. Tôi đã thử điều này theo một vài cách khác nhau. Về cơ bản, tôi nghĩ nếu tôi chỉ bắn những bóng ma thì tôi có thể làm những gì tôi muốn làm. Dưới đây là Cakefile tôi:

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() 

Khi tôi đi tuyến đường này, mặc dù tôi luôn luôn kết thúc nhận trường hợp ngoại lệ như thế này:

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) 

Tôi doin gì vậy ??


Nhờ mbostock Tôi đã làm việc như sau:

package.json của tôi:

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

Cakefile của tôi:

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) 
    }) 
+0

Có vẻ như bạn đã giải quyết được vấn đề của mình. Cá nhân tôi cũng gặp lỗi tương tự và sửa nó bằng 'xuất NODE_PATH =/usr/local/lib/node_modules' –

Trả lời

3

See D3 package.json. Cụ thể hơn, tệp bạn muốn yêu cầu khi chạy bên trong Nút hoặc môi trường tương tự là index.js thay vì d3.v2.js; tệp này chứa một số bản vá đặc biệt khiến D3 tương thích với toán tử require.

Để thử nó ra cho chính mình, cd vào kho d3, chạy node để tạo ra một vỏ tương tác, và sau đó nói

var d3 = require("./"); 

Hoặc, nếu bạn đang ở trong thư mục dự án của riêng bạn, nếu bạn' đã cài đặt D3 vào node_modules/d3 qua npm (npm install d3), bạn có thể nói:

var d3 = require("d3"); 
+0

Cảm ơn bạn đã trả lời câu hỏi hơi cũ của tôi - Tôi sẽ dùng thử ngay hôm nay. Và từ tác giả d3 không kém! – dsummersl

+0

Chỉ cần nghĩ rằng tôi sẽ theo dõi để hỏi ... vì tác giả là về chủ đề này ... Tôi đã thử 'npm install d3', và trong khi var d3 = require ('d3'); trong giao diện điều khiển nút mang lại cho tôi 'undefined' nhưng tôi có thể làm d3.version và nhận được một số, đặt nó trong app.js cho tôi: TypeError: Không thể đọc thuộc tính 'BSON' của undefined ... Tôi có thiếu cái gì không? – pland

+0

@pland Xem http://stackoverflow.com/questions/9948350 'var d3 = require (" d3 ");' in "không xác định" trong bảng điều khiển vì biểu thức đó đánh giá là không xác định; gây nhầm lẫn, điều đó không có nghĩa là biến 'd3' không xác định! Thực tế là bạn có thể in 'd3.version' chứng minh rằng gói được tải chính xác. – mbostock

Các vấn đề liên quan