-
Notifications
You must be signed in to change notification settings - Fork 310
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
Implement node:module createRequire API #2636
Implement node:module createRequire API #2636
Conversation
If we have Otherwise we might end up with some iconsistencies (i.e. |
Re unenv: I think unenv should still return all known built-ins for |
Good point. |
Not quite sure I'm following. Are you saying the runtime should not expose |
I think Is that correct @pi0 ? |
@vicb ... I'm just a bit confused because |
Now I'm confused too 🤔 |
import module from "node:module";
for (const m of modules()) {
if (!module.isBuiltin(m)) {
console.error(m);
}
}
function modules() {
// return the output of module.builtInModules as of Node.js v22.6.0 minus "internal/..." packages
}
Noe that |
Would this comment
be easy to add to the PR? |
It's not clear what you're saying in this comment.
It would be fairly trivial yes. |
The docs says "Note: the list doesn't contain prefix-only modules like node:test." I interpreted that as |
6118b4a
to
89292e5
Compare
|
Marking this ready for review |
A few comments: Node 22.6 had 2 extras builtin modules: I got my previous cross runtime wrong as the reference was
Which means that
I think adding |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a few minor comments
89292e5
to
1904a89
Compare
Thanks for the updates James |
The `createRequire()` function is used in node.js to create a `require()` function that can be used from inside an ESM module. This is useful largely for porting existing CommonJS code to ESM as it allows the ESM module to keep using `require()` to load other modules. This is being implemented in the runtime rather than as a polyfill because it needs to integrate tightly with the module registry implementation. The implementation is as close to Node.js' as we can currently make it. Note that the return value of `createRequire()` is a function. In Node.js, this function has additional properties like `require.resolve(...)`, `require.main`, `require.extensions`, and `require.cache`. We are not currently implementing these additional properties but we could in the future if there is a need for them. (`require.cache` is unlikely to ever be implemented since workers does not have a module cache the way Node.js does). This was implemented because the frameworks team asked for it. Refs: https://nodejs.org/docs/latest/api/module.html#modulecreaterequirefilename
1904a89
to
ab07d88
Compare
Running an internal CI run before landing. |
The
createRequire()
function is used in node.js to create arequire()
function that can be used from inside an ESM module. This is useful largely for porting existing CommonJS code to ESM as it allows the ESM module to keep usingrequire()
to load other modules.This is being implemented in the runtime rather than as a polyfill because it needs to integrate tightly with the module registry implementation.
The implementation is as close to Node.js' as we can currently make it.
Note that the return value of
createRequire()
is a function. In Node.js, this function has additional properties likerequire.resolve(...)
,require.main
,require.extensions
, andrequire.cache
. We are not currently implementing these additional properties but we could in the future if there is a need for them. (require.cache
is unlikely to ever be implemented since workers does not have a module cache the way Node.js does).Because it was extremely trivial to do so, is closely related, and we already have to maintain a list of known node builtins,
this also implements the
require('module').isBuiltin(...)
API.This was implemented because the frameworks team asked for it.
Refs: https://nodejs.org/docs/latest/api/module.html#modulecreaterequirefilename