node icon indicating copy to clipboard operation
node copied to clipboard

Tracking issue: require(esm)

Open joyeecheung opened this issue 10 months ago • 11 comments

Before it's unflagged

  • [x] Figure out default export interop with transpilers, either adding __esModule to required ESM on our end (https://github.com/nodejs/node/pull/52166), or transpilers update themselves to check the result returned by require():
  • [x] 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) https://github.com/nodejs/node/pull/54648
  • [x] Move experimental warning to where require() is actually handling a ESM

Before it is promoted to be stable:

  • [x] Implement detection https://github.com/nodejs/node/pull/52047
  • [x] Some marker to customize the default exports returned by require(esm) https://github.com/nodejs/node/pull/54563
  • [ ] Support module customization hooks https://github.com/nodejs/loaders/pull/198

Nice-to-haves:

  • [ ] https://github.com/nodejs/node/issues/52180
  • [x] https://github.com/nodejs/node/issues/52599 (fixed in https://github.com/nodejs/node/pull/52762)

Bug fixes & changes:

  • https://github.com/nodejs/node/pull/52264
  • https://github.com/nodejs/node/pull/52487
  • https://github.com/nodejs/node/pull/52047
  • https://github.com/nodejs/node/pull/52868
  • https://github.com/nodejs/node/pull/52166
  • https://github.com/nodejs/node/pull/54592
  • https://github.com/nodejs/node/pull/54648
  • https://github.com/nodejs/node/pull/55250

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

  • https://github.com/nodejs/node/pull/53619
  • https://github.com/nodejs/node/pull/53725

For v22.x backport (see a summary of regression analysis in https://github.com/nodejs/node/pull/55217#issuecomment-2388704035)

  • https://github.com/nodejs/node/pull/55241
  • https://github.com/nodejs/node/pull/55243
  • https://github.com/nodejs/node/pull/55286
  • https://github.com/nodejs/node/pull/55217

joyeecheung avatar Apr 25 '24 21:04 joyeecheung

(Slightly unrelated) Maybe we should add a new tracker label to the repo?

redyetidev avatar Apr 25 '24 21:04 redyetidev

@nodejs/loaders

GeoffreyBooth avatar Apr 30 '24 19:04 GeoffreyBooth

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.

jakebailey avatar May 07 '24 22:05 jakebailey

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

Andarist avatar May 07 '24 22:05 Andarist

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.

joyeecheung avatar Jul 11 '24 23:07 joyeecheung

@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.

GeoffreyBooth avatar Jul 11 '24 23:07 GeoffreyBooth

Opened PR for "module" in https://github.com/nodejs/node/pull/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

joyeecheung avatar Aug 29 '24 22:08 joyeecheung