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

Ensure CJS/MJS require interop in nodejs-compat #2606

Closed
jasnell opened this issue Aug 26, 2024 · 2 comments
Closed

Ensure CJS/MJS require interop in nodejs-compat #2606

jasnell opened this issue Aug 26, 2024 · 2 comments

Comments

@jasnell
Copy link
Member

jasnell commented Aug 26, 2024

In Node.js, there is now experimental support for require('...') an ESM.

In Node.js, by default, requiring an ESM, the require returns the module namespace. There is a new PR, however, that will allow the ESM to indicate that the require should return the default export instead: nodejs/node#54563

Example:

// a.mjs
export default class Foo {};
console.log(require('./a.mjs'));
// [Module: null prototype] { __esModule: true, default: [class Foo] }

With the PR

// a.mjs
export default class Foo {};
export const __cjsUnwrapDefault = true;
console.log(require('./a.mjs'));
// [class Foo]

When nodejs_compat_v2 mode is on, and we are calling require('...') from a CommonJS module, the new export should be honored.

@anonrig
Copy link
Member

anonrig commented Oct 7, 2024

It seems like this is already implemented @jasnell. Can we close this?

@jasnell
Copy link
Member Author

jasnell commented Oct 7, 2024

Yes

@anonrig anonrig closed this as completed Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants