我正在尝试导入 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.
我尝试按照 How to use D3 in Node.js properly? 中描述的导入 d3 的方法进行操作:
yarn add d3
(将 7.0.0 添加到我的 package.json 中)- 将
var d3 = require("d3");
放在我的 main.js 中 - 运行
node src/main.js
这似乎正是 https://stackoverflow.com/a/9949301/2848676 等答案中指定的内容.
我该如何解决这个错误?
最佳答案
我发现我可以通过降级我使用的 d3 版本来解决这个问题:
yarn remove d3
yarn add d3@4.12.2
node src/main.js
现在它不再给我那个错误了。
我不太了解 Node 模块约定的细微差别,无法对其进行解释,但我确实看到了 d3 7.0.0 中与模块有关的变化,所以这可能与根本原因有关:https://github.com/d3/d3/issues/3501 .
关于node.js - D3 7.0.0 语法错误 : Unexpected token 'export' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68461788/