Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support require(esm) in --module nodenext #60761

Merged
merged 11 commits into from
Jan 23, 2025

Conversation

andrewbranch
Copy link
Member

@andrewbranch andrewbranch commented Dec 13, 2024

Fixes #60534

require(esm) is currently unflagged in Node.js v22, but may be in v20 by the time this PR lands for 5.8. We’ll have a discussion about the flag name if it doesn’t happen by 5.8-beta. EDIT: the backport will not come in time for 5.8; we are only changing --module nodenext here, and will aim to make a --module node20 in 5.9.

This PR does not currently attempt to error on require calls of async modules (those with top-level await). We do not emit any special marker in declaration files indicating the presence of TLA, which is a prerequisite. My suspicion is that the sentiment will be that a static check for async modules in the require graph is not worth the pain of making a backward-incompatible declaration file change.

The other change in this PR is that import assertions are now prohibited in --module nodenext, since Node.js v22+ does not parse them. Since there have been a lot of module target changes in fast succession, here’s the summary:

5.6

target moduleResolution import assertions import attributes JSON imports require(esm)
node16 es2022 node16 no restrictions
nodenext esnext nodenext no restrictions

5.7.3

target moduleResolution import assertions import attributes JSON imports require(esm)
node16 es2022 node16 no restrictions
nodenext esnext nodenext needs type "json"

5.8

target moduleResolution import assertions import attributes JSON imports require(esm)
node16 es2022 node16 no restrictions
node18 es2022 node16 needs type "json"
nodenext esnext nodenext needs type "json"

5.9 (planned)

target moduleResolution import assertions import attributes JSON imports require(esm)
node16 es2022 node16 no restrictions
node18 es2022 node16 needs type "json"
node20 es2023 node16 needs type "json"
nodenext esnext nodenext needs type "json"

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Dec 13, 2024
@typescript-bot
Copy link
Collaborator

Thanks for the PR! It looks like you've changed the TSServer protocol in some way. Please ensure that any changes here don't break consumers of the current TSServer API. For some extra review, we'll ping @sheetalkamat, @mjbvz, @zkat, and @joj for you. Feel free to loop in other consumers/maintainers if necessary.

@typescript-bot
Copy link
Collaborator

Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page.

Also, please make sure @DanielRosenwasser and @RyanCavanaugh are aware of the changes, just as a heads up.

@marco-ippolito
Copy link

marco-ippolito commented Dec 13, 2024

I will probably release Node v20 (maybe the backport for require(esm)) around half of January 🙂

@andrewbranch andrewbranch reopened this Dec 19, 2024
@andrewbranch
Copy link
Member Author

@marco-ippolito things still look good for backporting?

@marco-ippolito
Copy link

marco-ippolito commented Jan 21, 2025

@marco-ippolito things still look good for backporting?

It might take a bit longer than expected due to the complexity of the backport, @joyeecheung is actively working on it. Ill keep here updated.

@marco-ippolito
Copy link

We won't actually be able to fully backport require(esm) in the next v20 release happening next week, it will go to next one probably (March I think)

@andrewbranch
Copy link
Member Author

Since the backport won’t happen until after 5.8 stable is out, I think the best thing to do might be to put this functionality only in --module nodenext for this release, and hopefully for 5.9 we can snap a --module node20

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM but I know the naming very much depends on Node's scheduling and our own scheduling.

@andrewbranch andrewbranch changed the title Add --module node20 with require(esm) support Support require(esm) in --module nodenext Jan 23, 2025
Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving a second time, but I do enjoy how "make require(ESM) work" is just a one line change 😄

@andrewbranch andrewbranch merged commit 5e52b28 into microsoft:main Jan 23, 2025
32 checks passed
@andrewbranch andrewbranch deleted the module-node20 branch January 23, 2025 00:56
@joyeecheung
Copy link

Hi, as commented by Marco before the backport was more challenging than we thought, though there's still hope to get it out in the March release. See nodejs/node#52697 (comment) and nodejs/node#56730 for more information about the progress.

@andrewbranch
Copy link
Member Author

If the March release contains the backport, that will be on track for TypeScript 5.9. 5.8 stable is scheduled to come out in February.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Add --module node22 (or --module node20) with support for require(esm)
5 participants