d3/d3-time-format

Cannot require() starting with version 4

mdmower opened this issue · 3 comments

Starting with version 4.0.0, it is not possible to import d3-time-format in a script using require() syntax, for example:

const { timeParse, timeFormat } = require('d3-time-format');

The error is:

internal/modules/cjs/loader.js:1102
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /path/to/myprogram/node_modules/d3-time-format/src/index.js
require() of ES modules is not supported.
require() of /path/to/myprogram/node_modules/d3-time-format/src/index.js from /path/to/myprogram/myprogram.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 /path/to/myprogram/node_modules/d3-time-format/package.json.

Could you restore support for using require(...) syntax instead of requiring import ... syntax? Note that developers can still opt to use import ... syntax if they like, this would just restore support for require(...) which is quite convenient when quickly writing utility scripts.

I'm sorry that you don't see this as an issue and have simply closed it. I'll just provide one article worth reading, paying special attention to section How to Create a Good “Dual Package” Containing Both CJS and ESM: https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1

I deliberately dropped support for CJS. I have no interest in maintaining multiple module formats and encourage everyone to adopt ES modules. If you disagree, you are free to fork this module, to use an earlier version, or simply to not use it. Thank you for your feedback.

I understand. I appreciate the response.