Skip to content

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

@everett1992

Description

@everett1992

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions