我尝试将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.
我尝试按照How to use D3 in Node.js properly?中描述的方法导入d3:
yarn add d3
(将7.0.0添加到我的package.json)
1.把var d3 = require("d3");
放到我的main.js中
1.运行node src/main.js
这似乎正是https://stackoverflow.com/a/9949301/2848676等答案中指定的内容。
如何解决此错误?
2条答案
按热度按时间gywdnpxw1#
我发现我可以通过降级我正在使用的d3版本来解决这个问题:
yarn remove d3
yarn add d3@4.12.2
个node src/main.js
现在它不再给予我那个错误了。
我不太了解节点模块约定的细微差别,无法解释它,但我确实看到了d37.0.0中与模块相关的变化,所以这可能与根本原因有关:https://github.com/d3/d3/issues/3501。
uajslkp62#
对我来说也一样