D3 7.0.0语法错误:意外的令牌'export'。

3
我正在尝试将 d3 导入到我的 Node 应用程序中,但是我遇到了以下错误:
$ 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. 在我的main.js中放置var d3 = require("d3");
  3. 运行node src/main.js

这似乎正是诸如https://dev59.com/jGkw5IYBdhLWcg3wXZX8#9949301的答案中所指定的内容。

我该如何解决此错误?


1
当我升级到d3 7.0.0时,遇到了相同的问题。最后一个可用版本是6.7.0。 - Thijs Limmen
2个回答

3

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

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

现在它不再给我报错了。

我不太熟悉Node模块约定的细节,但我确实看到d3 7.0.0中涉及模块的更改,因此可能与根本原因有关:https://github.com/d3/d3/issues/3501


1
谢谢你的回答。我之前使用的是7.0.1版本,降级到6.7.0版本解决了问题。 - Awad Maharoof

0
npm i d3@6.*

这对我也适用


这并没有回答问题。一旦您拥有足够的声望,您将能够评论任何帖子;相反,提供不需要询问者澄清的答案。- 来自审核 - Geshode

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接