Can no longer use require("d3-array") in Node.js
jljorgenson18 opened this issue · 2 comments
jljorgenson18 commented
Steps to reproduce
- Create a project with d3-array as a dependency (version 3.0.0 and up)
- Create a node script with contents
require("d3-array");
- Execute script with Node.js (
node myScript.js
);
What you get is this
require() of ES modules is not supported.
require() of /Users/jessejorgenson/projects/uno/node_modules/d3-array/src/index.js from /Users/jessejorgenson/projects/uno/scripts/d3ArrayCheck.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/jessejorgenson/projects/uno/node_modules/d3-array/package.json.
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1154:13)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/Users/jessejorgenson/projects/uno/scripts/d3ArrayCheck.js:1:1)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14) {
code: 'ERR_REQUIRE_ESM'
}
This seems to be due to adding "type": "module" to the d3-array package.json and not including a CommonJS fallback. I don't know exactly how it should be configured given the confusion with bundlers, but I believe just removing "type": "module" would make everything work again and then bundlers could use the "module" entry point.
jljorgenson18 commented
@mbostock Any specific reason you closed this?
Fil commented
Probably because everyone asks the same question. We tried to document it in the release notes:
This package now requires Node.js 12 or higher. For more, please read Sindre Sorhus’s FAQ.
and at the top of https://github.com/d3/d3-array