Back to Tsx

Module customization hooks

notes/node/module-hooks.md

4.23.62.1 KB
Original Source

Module customization hooks

Node's async and sync module customization-hook APIs.

module.register()

module.register() installs asynchronous ESM resolve and load hooks on a dedicated loader worker thread. Synchronous callers communicate through a MessagePort, block in makeSyncRequest, and wait with AtomicsWait.

module.registerHooks()

module.registerHooks() installs synchronous resolve and load hooks in the current thread. The public entry point is registerHooks; Node routes work through loadWithHooks and resolveWithHooks.

CJS reload support

Sync hooks later gained a load-to-translate handshake that can re-enter the CJS loader. Before the boundary, sync load results lack shouldBeReloadedByCJSLoader (v24.11.0 load.js#L133-L158). At v24.11.1 the load result gains that field (load.js#L144-L171), and the translator can route the module through Module._load (translators.js#L334-L363). The same shape is backported to v22.22.3 (load.js#L148-L175).