Open
Description
Version
v23.9.0
Platform
Darwin MacBook-Pro 24.3.0 Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:23 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T8122 arm64
Subsystem
No response
What steps will reproduce the bug?
Create the following files:
instrument.js
import * as mod from "module";
mod.registerHooks({
load(url, context, nextLoad) {
return nextLoad(url, context);
},
});
mod.register(new URL("hooks.js", import.meta.url).toString());
hooks.js
export async function load(url, context, nextLoad) {
return nextLoad(url, context);
}
app.js
console.log("Hello from app.js");
Run node --import ./instrument.js ./app.js
How often does it reproduce? Is there a required condition?
Always, if the project includes a CJS file.
What is the expected behavior? Why is that the expected behavior?
No exception, logs Hello from app.js
What do you see instead?
node --import ./instrument.js ./app.js
node:internal/modules/customization_hooks:276
throw new ERR_INVALID_RETURN_PROPERTY_VALUE(
^
TypeError [ERR_INVALID_RETURN_PROPERTY_VALUE]: Expected a string, an ArrayBuffer, or a TypedArray to be returned for the "source" from the "load" hook but got null.
at validateLoad (node:internal/modules/customization_hooks:276:11)
at nextStep (node:internal/modules/customization_hooks:190:14)
at load (file:///Users/timokoessler/Git/nodejs-module-hooks-bug/instrument.js:5:12)
at nextStep (node:internal/modules/customization_hooks:185:26)
at loadWithHooks (node:internal/modules/customization_hooks:348:18)
at #loadSync (node:internal/modules/esm/loader:790:14)
at ModuleLoader.load (node:internal/modules/esm/loader:749:28)
at ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:536:43)
at #createModuleJob (node:internal/modules/esm/loader:560:36)
at #getJobFromResolveResult (node:internal/modules/esm/loader:312:34) {
code: 'ERR_INVALID_RETURN_PROPERTY_VALUE'
}
Additional information
Real world cases where this bug occurs
- Using a test runner that uses
module.register
(e.g. tapjs) with an app that usesmodule.registerHooks
- Using two instrumentation libraries, one using
module.register
(e.g. Sentry) and the other usingmodule.registerHooks
More information
- The error does not occur when only ESM is used in the project
- Simply create a
package.json
with"type:" "module"
and re-run the command above.
- Simply create a
- The error does not occur when only one of the register methods is used
Git Repo: timokoessler/nodejs-module-hooks-bug