Skip to content

ESM loader hooks in Workers no longer working since Node.js 22.2 #53097

@alan-agius4

Description

@alan-agius4

Version

v22.2.0

Platform

Linux 6.6.15-2rodete2-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.6.15-2rodete2 (2024-03-19) x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Since Node.js version 22.2, ESM loader hooks no longer function inside of a worker and the Node.js application becomes unresponsive.

app.js

const { Worker, isMainThread } = require("node:worker_threads");

if (!isMainThread) {
  import("./test.mjs").then(console.log).catch(console.error);
} else {
  new Worker("./app.js", {
    execArgv: [
      '--import=data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("./hooks.mjs", pathToFileURL("./"), { data: {} });',
    ],
  });
}

hooks.mjs

export async function initialize() {
  console.log('init')
}

export async function resolve(specifier, context, nextResolve) {
  console.log('resolve: ' + specifier)
  return nextResolve(specifier, context)
}
$ node app.js

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?

init
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/test-loader-esm/test.mjs' imported from ///test-loader-esm/app.js
    at finalizeResolution (node:internal/modules/esm/resolve:264:11)
    at moduleResolve (node:internal/modules/esm/resolve:924:10)
    at defaultResolve (node:internal/modules/esm/resolve:1148:11)
    at nextResolve (node:internal/modules/esm/hooks:750:28)
    at resolve (/test-loader-esm/hooks.mjs:8:10)
    at nextResolve (node:internal/modules/esm/hooks:750:28)
    at Hooks.resolve (node:internal/modules/esm/hooks:238:30)
    at handleMessage (node:internal/modules/esm/worker:199:24)
    at Immediate.checkForMessages (node:internal/modules/esm/worker:141:28)
    at process.processImmediate (node:internal/timers:478:21) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: '/test-loader-esm/test.mjs'
}
resolve: ./test.mjs

What do you see instead?

Application becomes unresponsive.

Additional information

I suspect that this is caused by #52706

Metadata

Metadata

Assignees

No one assigned

    Labels

    loadersIssues and PRs related to ES module loaders

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions