Skip to content

Registering customization hooks via --require results in double registration #52377

Open
@Flarna

Description

@Flarna

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    loadersIssues and PRs related to ES module loadersmoduleIssues and PRs related to the module subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions