front-end-monorepo: Pure ES Modules break the Mocha tests
Updated 2024 - List of third-party packages this applies to:
- mime https://github.com/zooniverse/front-end-monorepo/pull/6265
- react-player https://github.com/zooniverse/front-end-monorepo/pull/6436
- d3 https://github.com/zooniverse/front-end-monorepo/pull/3781
- chai
- graphql-request https://github.com/zooniverse/front-end-monorepo/pull/6427
- sinon-chai https://github.com/zooniverse/front-end-monorepo/pull/6543
(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:
- The awkward valley to ESM: Node.js, Victory, and D3 - 15 Jun 2022, formidable.com - TL;DR: as more libraries (e.g. d3.js) drop CommonJS support and go 100% ESM (ECMAScript Modules), a lot of upstream/downstream dependencies that use CommonJS get borked. (ESM setups can read ESM + CommonJS modules, but CommonJS setups can’t read ESModules.) Awkward transpiling workarounds are required.
- Importing Issues - Error [ERR_REQUIRE_ESM]: Must use import to load ES Module - 29 Jun 2021, D3 repo - TL;DR: lol good luck
- import conflict with 3.0.0 update - 25 Apr 2021, D3 repo - TL;DR: lol not a d3 problem, fix your NextJS
- Yeah, but lib-classifier has the same error, and it’s not running Next.JS. Also, I think our tests aren’t running Next.JS?
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)
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.