Choosing a marker to customize default exports returned by require(esm)
#1622
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When a user have an existing CommonJS package like this:
Now that
require(esm)
is possible and they can ship ESM to their CommonJS users without breaking them, they would try something like this:As you can see, for CommonJS consumers this will not be exactly equivalent. Since in ESM the default exports is a property of the name space, they need to access
.default
from the result returned byrequire(esm)
. So the idea is that Node.js can take a marker from package authors to directly customize whatrequire(esm)
returns in this case.To keep
const foo = require('foo')
working, the two proposed markers are:or
Further to this vote, there is a follow-on proposal to reuse this marker on namespaces representing CJS modules imported into ESM, where attaching this marker would effectively define a scheme for converting CJS into ESM that would retain full transitive interop semantics allowing arbitrary CJS to be converted into ESM as a non-breaking change.
Under this follow-on marking, when importing some CJS module
bar
into ESM:Users importing with
console.log(await import('bar'))
would obtain a namespace object either of the formModuleNamespace { __cjsUnwrapDefault: true, default: 'bar' }
orModuleNamespace { default: 'bar', 'module.exports': 'bar' }
depending on which option is chosen for this first PR.The vote should stay open for 7 days, or until all TSC voting members have cast a ballot.
Vote instructions:
To close the vote, at least 3 secret holder(s)1 must run the following command:
git node vote https://github.com/nodejs/TSC/pull/1622 --decrypt-key-part --post-comment
/cc @nodejs/tsc
Current estimated participation: 15.79%
Footnotes
secret holders are folks who have access to the private key associated with a public key on hkps://keys.openpgp.org that references an email address listed on the TSC voting member list at the time of the opening of the vote. ↩