Description
The current recommendation to install customization hooks is to use --import
.
But --import
is still marked as experimental therefore not the preferred thing to use in production.
If I use the stable alternative --register
the loader hooks are installed twice.
Easy to reproduce:
// loader.mjs
export async function resolve (specifier, context, parentResolve) {
console.log('execArgv', process.execArgv);
return await parentResolve(specifier, context);
}
// register.js
const { register } = require('node:module');
const { pathToFileURL } = require('node:url');
const url = pathToFileURL('./loader.mjs');
console.log('registering', url.href);
register(url);
// main.mjs
console.log("hello");
run with --require
results in
node --require ./register.js main.mjs
registering file:///C:/work/node-double-loader/loader.mjs
registering file:///C:/work/node-double-loader/loader.mjs
execArgv [ '--require', './register.js' ]
execArgv [ '--require', './register.js' ]
hello
run with --import
:
registering file:///C:/work/node-double-loader/loader.mjs
execArgv [ '--import', './register.js' ]
hello
I guess the --require
argument gets forwarded to the internal Worker
(along with env and other command line args) and in contrast to --import
this seems to have an effect.
Not sure if the problem is the different behavior of --require
vs --import
in Worker.
But in general I think we should not blindly forward and command line args to the internal hooks worker.
Edit: Thinking once more I think we should forward env/--require/... configs to the internal worker. People migth want to get e.g. Otel into this thread to capture traces.