front-end-monorepo: Pure ES Modules break the Mocha tests

Updated 2024 - List of third-party packages this applies to:

(Dependabot has been told to ignore the major version upgrades)


_Originally posted by @shaunanoordin in https://github.com/zooniverse/front-end-monorepo/issues/3361#issuecomment-1171274486_ The tests for app-project and lib-classifier are failing on:

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/shaun/projects/front-end-monorepo/node_modules/d3/src/index.js from /Users/shaun/projects/front-end-monorepo/packages/app-project/src/shared/components/ProjectStatistics/components/CompletionBar/CompletionBar.js not supported.
Instead change the require of index.js in /Users/shaun/projects/front-end-monorepo/packages/app-project/src/shared/components/ProjectStatistics/components/CompletionBar/CompletionBar.js to a dynamic import() which is available in all CommonJS modules.

and

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/shaun/projects/front-end-monorepo/node_modules/d3/src/index.js from /Users/shaun/projects/front-end-monorepo/packages/lib-classifier/src/components/Classifier/components/Feedback/components/Graph2dRangeFeedback.js not supported.
Instead change the require of index.js in /Users/shaun/projects/front-end-monorepo/packages/lib-classifier/src/components/Classifier/components/Feedback/components/Graph2dRangeFeedback.js to a dynamic import() which is available in all CommonJS modules.

and etc

OK, so upon review:

  • D3 now ships as pure ES modules and requires Node.js 12 or higher. - this IS a problem.

Additional reading:

Status

🤷 throw_hands_up_emoji

Good gravy, I need to check with the other devs to see if anybody else has encountered the ESM import issues. I might have missed something someone on the team is already aware of, since this doesn’t seem to be a niche problem.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 35 (35 by maintainers)

Most upvoted comments

I have to confess that currently I use Storybook with Playwright to run tests for JSX components in a pure ESM project, and it just works out-of-the-box.

https://github.com/nismod/irv-jamaica/blob/83a31ea2e4a20b4a7ab42555b262b4af8894dfcc/.github/workflows/storybook-tests.yml#L19-L29

require(esm) is now default behaviour in Node 23. 🎉

https://nodejs.org/en/blog/release/v23.0.0

I think you can enable it with a flag in Node 22, which will enter LTS next week.

Top level await is still restricted to modules.

With this feature enabled, Node.js will no longer throw ERR_REQUIRE_ESM if require() is used to load a ES module. It can, however, throw ERR_REQUIRE_ASYNC_MODULE if the ES module being loaded or its dependencies contain top-level await. When the ES module is loaded successfully by require(), the returned object will be a ES module namespace object similar to what’s returned by import(), and can be checked using util.isModuleNamespaceObject().