load(url, context, nextLoad)


  • url <string> resolve 链返回的 URL
  • context <Object>
    • conditions <string[]> 导出相关 package.json 的条件
    • format <string> | <null> | <undefined>resolve 钩子链可选提供的格式。输入可以是任何字符串值;输入值不需要符合下面描述的可接受返回值列表。
    • importAttributes <Object>
  • nextLoad <Function> 链中后续的 load 钩子,或在最后一个用户提供的 load 钩子之后的 Node.js 默认 load 钩子
    • url <string>

    • context <Object> | <undefined> 如果省略,将提供默认值。提供时,默认值会与提供的属性合并,并以提供的属性为优先。在默认的 nextLoad 中,如果 url 指向的模块没有明确的模块类型信息,则 context.format 是必需的。

  • 返回值: <Object> | <Promise> 异步版本接收一个包含以下属性的对象,或者一个将解析为该对象的 Promise。 同步版本仅接受同步返回的对象。

load 钩子提供了一种定义自定义方法的方法,用于确定 URL 应该如何被解释、获取和解析。它还负责验证导入属性。

【The load hook provides a way to define a custom method of determining how a URL should be interpreted, retrieved, and parsed. It is also in charge of validating the import attributes.】

format 的最终值必须是以下之一:

【The final value of format must be one of the following:】

formatDescriptionAcceptable types for source returned by load
'addon'Load a Node.js addon<null>
'builtin'Load a Node.js builtin module<null>
'commonjs-typescript'Load a Node.js CommonJS module with TypeScript syntax<string> | <ArrayBuffer> | <TypedArray> | <null> | <undefined>
'commonjs'Load a Node.js CommonJS module<string> | <ArrayBuffer> | <TypedArray> | <null> | <undefined>
'json'Load a JSON file<string> | <ArrayBuffer> | <TypedArray>
'module-typescript'Load an ES module with TypeScript syntax<string> | <ArrayBuffer> | <TypedArray>
'module'Load an ES module<string> | <ArrayBuffer> | <TypedArray>
'wasm'Load a WebAssembly module<ArrayBuffer> | <TypedArray>

source 的值会被忽略,因为对于 'builtin' 类型,目前无法替换 Node.js 内置(核心)模块的值。

【The value of source is ignored for type 'builtin' because currently it is not possible to replace the value of a Node.js builtin (core) module.】