jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: require of ESM not supported while process.feature.require_module:true

Open everett1992 opened this issue 2 months ago • 2 comments

Version

30.2.0

Steps to reproduce

Jest cannot require esm modules, but it does not override node's process.features.reqiure_module, so code like this will throw an error despite attempts to be defensive.

if (process.features.require_module) {
  require("./some-esm.js")
} else {
  // some fallback
}

Expected behavior

I expect either require esm to work, or the fallback code path to be executed

Actual behavior

Instead it throws

Must use import to load ES Module

Additional context

As a workaround I can mock the value myself.

  beforeEach(() => {
    if ("require_module" in process.features) {
      jest.spyOn(process.features, "require_module", "get").mockReturnValue(false);
    }
  })

Environment

System:
    OS: Linux 6.14 Ubuntu 24.04.3 LTS 24.04.3 LTS (Noble Numbat)
    CPU: (14) x64 Intel(R) Core(TM) Ultra 7 165U
    Memory: 21.96 GB / 30.79 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 20.19.5 - node
    npm: 8.19.4 - npm

everett1992 avatar Oct 13 '25 09:10 everett1992

hi @everett1992, can you provide a repo for reproduction? There are some missing pieces like Babel config, the project type being CommonJS or ECMAScript, et cetera.

hainenber avatar Oct 18 '25 09:10 hainenber

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Nov 17 '25 10:11 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar Dec 17 '25 11:12 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar Dec 17 '25 11:12 github-actions[bot]