定制钩子


🌐 Customization Hooks

Node.js 当前支持两种类型的模块自定义钩子:

🌐 Node.js currently supports two types of module customization hooks:

  1. module.registerHooks(options):接受同步钩子函数,这些函数会直接在加载模块的线程上运行。
  2. module.register(specifier[, parentURL][, options]):将指定符指向导出异步钩子函数的模块。这些函数在单独的加载器线程上运行。

异步钩子会因为线程间通信而产生额外开销,尤其是在模块图中自定义 CommonJS 模块时。在大多数情况下,建议通过 module.registerHooks() 使用同步钩子以简化操作。

🌐 The asynchronous hooks incur extra overhead from inter-thread communication, and have several caveats especially when customizing CommonJS modules in the module graph. In most cases, it's recommended to use synchronous hooks via module.registerHooks() for simplicity.