Skip to content
Commit Detail

Commit ab07d88

Author
James M Snell <jasnell@gmail.com> 2024-08-30 17:26:02 -0700
Parents
67d7678
Tree
8d131bd
Implement node:module createRequire API

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

Files changed