首页 > 解决方案 > D3 7.0.0 SyntaxError:意外的令牌“导出”

问题描述

我正在尝试将d3导入我的节点应用程序,但出现以下错误:

$ node src/main.js
this is logged from main.js 2021-07-20 2:33 PM
/Users/michaelosofsky/Developer/shh/node_modules/d3/src/index.js:1
export * from "d3-array";
^^^^^^

SyntaxError: Unexpected token 'export'
    at Module._compile (internal/modules/cjs/loader.js:892:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Module.require (internal/modules/cjs/loader.js:849:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/Users/michaelosofsky/Developer/shh/src/main.js:3:10)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

我尝试按照如何在 Node.js 中正确使用 D3 中描述的导入 d3 的方法?

  1. yarn add d3(将 7.0.0 添加到我的 package.json 中)
  2. 放入var d3 = require("d3");我的 main.js
  3. node src/main.js

这似乎正是https://stackoverflow.com/a/9949301/2848676等答案中指定的内容。

如何解决此错误?

标签: node.jsd3.js

解决方案


我发现我可以通过降级我正在使用的 d3 版本来解决这个问题:

  1. yarn remove d3
  2. yarn add d3@4.12.2
  3. node src/main.js

现在它不再给我那个错误了。

我不太了解节点模块约定的细微差别来解释它,但我确实看到了与模块有关的 d3 7.0.0 的变化,所以这可能与根本原因有关:https://github。 com/d3/d3/issues/3501


推荐阅读