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

Open
jasnell opened this issue Aug 26, 2024 · 0 comments
Open

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

jasnell opened this issue Aug 26, 2024 · 0 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.

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

1 participant