How to use D3 in Node.js properly?

前端 未结 3 1110
别跟我提以往
别跟我提以往 2020-12-02 10:06

I\'ve been trying to invoke D3 within Node.js. I tried firstly to import d3.v2.js from D3\'s website with the script tag, but then read this thread:

I want to run d3

3条回答
  •  醉酒成梦
    2020-12-02 10:42

    The correct way to use D3 within Node is to use NPM to install d3 and then to require it. You can either npm install d3 or use a package.json file, followed by npm install:

    {
      "name": "my-awesome-package",
      "version": "0.0.1",
      "dependencies": {
        "d3": "3"
      }
    }
    

    Once you have d3 in your node_modules directory, load it via require:

    var d3 = require("d3");
    

    And that's it.

    Regarding your other issues: Canvas is not required to use D3. The node-canvas example you linked requires canvas because it renders to a canvas. The TypeError (Cannot read property 'BSON' of undefined) appears to be related to your use of mongoose / monogdb, not D3.

提交回复
热议问题