Skip to content

Tracking issue: require(esm) #52697

Open
Open
@joyeecheung

Description

@joyeecheung
Member

Before it's unflagged

  • Figure out default export interop with transpilers, either adding __esModule to required ESM on our end (module: add __esModule to require()'d ESM #52166), or transpilers update themselves to check the result returned by require():
    conditional exports for module, regardless of whether it's loaded by require or import. Something like module which is recognized by Webpack and Rollup would be good (maybe this doesn't need to block unflagging, but should be done before stablization) module: implement the "module-sync" exports condition #54648
    Move experimental warning to where require() is actually handling a ESM

Before it is promoted to be stable:

Nice-to-haves:

Bug fixes & changes:

Related features that interoperate with require(esm) and need to be considered when being backported together:

v22.x backport (see a summary of regression analysis in #55217 (comment))

v20.x backport: #56927

Activity

added
moduleIssues and PRs related to the module subsystem.
esmIssues and PRs related to the ECMAScript Modules implementation.
on Apr 25, 2024
GeoffreyBooth

GeoffreyBooth commented on Apr 30, 2024

@GeoffreyBooth
Member

@nodejs/loaders

jakebailey

jakebailey commented on May 7, 2024

@jakebailey

I just wanted to note it here, but it would be super super awesome if (once stable) this were backported to Node 20/22 or even Node 18 if still in support. I'd love to be able to propose a change to switch TypeScript to ESM (given I have it working without breaking CJS consumers), but the time horizon of Node 22 being the oldest supported version is pretty daunting.

It also seems like there is a hacky way using multiple entrypoints that could allow for TS to grab Node's builtins conditionally without #52599/#52762, though none of that is possible without require(ESM), of course.

Even without TypeScript's use case, I think the feature itself is a really important one for the ecosystem. Backporting would really make ESM changeovers a lot less painful.

Andarist

Andarist commented on May 7, 2024

@Andarist
Contributor

IIRC from some Twitter threads - there is a plan to backport this once the feature stabilizes.

joyeecheung

joyeecheung commented on Jul 11, 2024

@joyeecheung
MemberAuthor

Regarding the conditional exports, @guybedford suggested to implement just the module condition that Rollup and Webpack already recognize. I have a WIP but need to do some testing which involves many combinations of test cases 😵‍💫 but will also do some npm crawling to see if it can be picked up/is in conflict with any existing popular packages.

GeoffreyBooth

GeoffreyBooth commented on Jul 11, 2024

@GeoffreyBooth
Member

@guybedford suggested to implement just the module condition that Rollup and Webpack already recognize.

The module top level field, or within exports?

Personally I think require-module makes more sense, as it would complement the existing require and import keys that Node supports.

joyeecheung

joyeecheung commented on Aug 29, 2024

@joyeecheung
MemberAuthor

Opened PR for "module" in #54648

Personally I think require-module makes more sense, as it would complement the existing require and import keys that Node supports.

If we are starting from scratch, yes, but then the "module" condition has already been adopted by bundlers that support require(esm) in the wild, so it seems better to go along with the existing convention. See https://gist.github.com/sokra/e032a0f17c1721c71cfced6f14516c62

151 remaining items

styfle

styfle commented on May 18, 2025

@styfle
Member

For v20.x backport: TBD

Looks like this was backported to 20.x according to the docs which say "Added in: v20.19.0"

https://nodejs.org/docs/latest-v20.x/api/process.html#processfeaturesrequire_module

kjots

kjots commented on Jul 1, 2025

@kjots

@joyeecheung Looks like #58515 is now occurring in the latest Node LTS release:

// cyclic-cjs-via-esm-issue.ts
import { S3Client } from '@aws-sdk/client-s3';

console.log(S3Client);
$ ~/.local/share/mise/installs/node/22.17.0/bin/node --import tsx cyclic-cjs-via-esm-issue.ts
node:internal/assert:17
  throw new ERR_INTERNAL_ASSERTION(message);
        ^

Error [ERR_INTERNAL_ASSERTION]: Unexpected module status 3.
This is caused by either a bug in Node.js or incorrect usage of Node.js internals.
Please open an issue with this stack trace at https://github.com/nodejs/node/issues

    at Function.fail (node:internal/assert:17:9)
    at ModuleJob.runSync (node:internal/modules/esm/module_job:317:12)
    at require (node:internal/modules/esm/translators:149:9)
    at Object.<anonymous> (/Users/kots/Development/Git/github.com/SEEK-Jobs/melways/tmp/scripts/node_modules/.pnpm/@aws-crypto+crc32c@5.2.0/node_modules/@aws-crypto/crc32c/src/aws_crc32c.ts:6:1)
    at loadCJSModule (node:internal/modules/esm/translators:164:3)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:200:7)
    at ModuleJob.runSync (node:internal/modules/esm/module_job:309:39)
    at require (node:internal/modules/esm/translators:149:9)
    at Object.<anonymous> (/Users/kots/Development/Git/github.com/SEEK-Jobs/melways/tmp/scripts/node_modules/.pnpm/@aws-crypto+crc32c@5.2.0/node_modules/@aws-crypto/crc32c/src/index.ts:64:1)
    at loadCJSModule (node:internal/modules/esm/translators:164:3) {
  code: 'ERR_INTERNAL_ASSERTION'
}

Node.js v22.17.0
$ ~/.local/share/mise/installs/node/22.16.0/bin/node --import tsx cyclic-cjs-via-esm-issue.ts 
[class S3Client extends Client]
joyeecheung

joyeecheung commented on Jul 1, 2025

@joyeecheung
MemberAuthor

The fix just got released in 24 last week. Per the TLS policy it will probably need to wait for the next 22 release (scheduled 2025-07-21) to go out in 22.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.moduleIssues and PRs related to the module subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @voxpelli@styfle@GeoffreyBooth@jonkoops@kjots

        Issue actions

          Tracking issue: require(esm) · Issue #52697 · nodejs/node