Skip to content

fix: migrate from deprecated module.register() to module.registerHooks()#585

Closed
cjnoname wants to merge 1 commit into
oxc-project:mainfrom
cjnoname:fix/migrate-to-registerHooks
Closed

fix: migrate from deprecated module.register() to module.registerHooks()#585
cjnoname wants to merge 1 commit into
oxc-project:mainfrom
cjnoname:fix/migrate-to-registerHooks

Conversation

@cjnoname
Copy link
Copy Markdown

@cjnoname cjnoname commented May 9, 2026

Summary

module.register() has been runtime-deprecated as DEP0205 since Node.js v25.9.0 (see nodejs/node#62401), causing the following warning on every invocation:

(node:18283) [DEP0205] DeprecationWarning: `module.register()` is deprecated. Use `module.registerHooks()` instead.

Changes

  • Use module.registerHooks() when available (Node.js >= 24.4), falling back to module.register() for older versions
  • The resolve hook is wrapped to only pass parentURL and conditions to the native createResolve binding, stripping newer context fields (e.g. importAttributes) that the native binding doesn't recognize
  • initTracing() is called directly in the registerHooks branch since esm.mjs is no longer imported in that path

Note for maintainers

This workaround strips unknown fields from the resolve context before passing to the native binding. A more robust long-term fix would be to update the Rust createResolve implementation to either:

  1. Accept and ignore unknown fields in the context object (using #[napi(object)] with #[serde(flatten)] or similar), or
  2. Explicitly support importAttributes (and any future fields Node.js adds to the resolve context)

This would make the native binding forward-compatible with future Node.js versions without needing a JS wrapper.

@cjnoname
Copy link
Copy Markdown
Author

cjnoname commented May 9, 2026

@Boshen This will trigger red warnings in the latest Node.js 25 and 26. If possible, could you please help release a fix as soon as possible? Thanks.

module.register() has been runtime-deprecated as DEP0205 since Node.js v25.9.0.
Use module.registerHooks() when available (Node.js >= 24.4), falling back to
module.register() for older versions.

The resolve hook wrapper strips fields (e.g. importAttributes) not recognized
by the native createResolve binding to maintain compatibility.
@cjnoname cjnoname force-pushed the fix/migrate-to-registerHooks branch from 6bde0c9 to 0ee4b66 Compare May 9, 2026 03:17
@cjnoname
Copy link
Copy Markdown
Author

cjnoname commented May 9, 2026

Update: The registerHooks approach doesn't work as-is because createResolve (native binding) strictly validates the context object and rejects unknown fields like importAttributes that Node.js v26 passes in registerHooks mode.

Even stripping known extra fields in a JS wrapper isn't sufficient — nextResolve callbacks also pass contexts with importAttributes back through the chain.

The root cause is that module.register() runs hooks in a worker thread where the context format differs from registerHooks() which runs in the main thread with the full context object.

To properly fix this, the Rust native createResolve binding needs to be updated to ignore unknown fields in the context object (e.g. using #[serde(deny_unknown_fields)]#[serde(default)] or similar in napi-rs struct definitions).

In the meantime, a workaround is to suppress the deprecation warning by temporarily patching process.emitWarning around the register() call:

const _emitWarning = process.emitWarning;
process.emitWarning = (warning, ...args) => {
  if (typeof warning === "string" && warning.includes("module.register()")) { return; }
  return _emitWarning.call(process, warning, ...args);
};
register("@oxc-node/core/esm", import.meta.url);
process.emitWarning = _emitWarning;

I'll close this PR since it's not the right fix. The proper solution requires changes to the native binding layer.

@cjnoname cjnoname closed this May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant