@@ -268,8 +268,8 @@ It's possible to call `register` more than once:
268268// entrypoint.mjs
269269import { register } from ' node:module' ;
270270
271- register (' ./first .mjs' , import .meta.url);
272- register (' ./second .mjs' , import .meta.url);
271+ register (' ./foo .mjs' , import .meta.url);
272+ register (' ./bar .mjs' , import .meta.url);
273273await import (' ./my-app.mjs' );
274274` ` `
275275
@@ -279,20 +279,23 @@ const { register } = require('node:module');
279279const { pathToFileURL } = require (' node:url' );
280280
281281const parentURL = pathToFileURL (__filename );
282- register (' ./first .mjs' , parentURL);
283- register (' ./second .mjs' , parentURL);
282+ register (' ./foo .mjs' , parentURL);
283+ register (' ./bar .mjs' , parentURL);
284284import (' ./my-app.mjs' );
285285` ` `
286286
287- In this example, the registered hooks will form chains. If both ` first .mjs ` and
288- ` second .mjs ` define a ` resolve` hook, both will be called, in the order they
289- were registered. The same applies to all the other hooks.
287+ In this example, the registered hooks will form chains. These chains run
288+ last-in, first out (LIFO). If both ` foo .mjs ` and ` bar .mjs ` define a ` resolve`
289+ hook, they will be called like so (note the right-to-left):
290+ node's default ← ` ./ foo .mjs ` ← ` ./ bar .mjs `
291+ (starting with ` ./ bar .mjs ` , then ` ./ foo .mjs ` , then the Node.js default).
292+ The same applies to all the other hooks.
290293
291294The registered hooks also affect ` register` itself. In this example,
292- ` second .mjs ` will be resolved and loaded per the hooks registered by
293- ` first . mjs ` . This allows for things like writing hooks in non-JavaScript
294- languages, so long as an earlier registered loader is one that transpiles into
295- JavaScript.
295+ ` bar .mjs ` will be resolved and loaded via the hooks registered by ` foo . mjs `
296+ (because ` foo ` 's hooks will have already been added to the chain). This allows
297+ for things like writing hooks in non-JavaScript languages, so long as
298+ earlier registered hooks transpile into JavaScript.
296299
297300The ` register` method cannot be called from within the module that defines the
298301hooks.
@@ -367,11 +370,11 @@ export async function load(url, context, nextLoad) {
367370}
368371` ` `
369372
370- Hooks are part of a chain, even if that chain consists of only one custom
371- (user-provided) hook and the default hook, which is always present. Hook
373+ Hooks are part of a [ chain][] , even if that chain consists of only one
374+ custom (user-provided) hook and the default hook, which is always present. Hook
372375functions nest: each one must always return a plain object, and chaining happens
373376as a result of each function calling ` next< hookName> ()` , which is a reference to
374- the subsequent loader's hook.
377+ the subsequent loader's hook (in LIFO order) .
375378
376379A hook that returns a value lacking a required property triggers an exception. A
377380hook that returns without calling ` next< hookName> ()` _and_ without returning
@@ -1049,6 +1052,7 @@ returned object contains the following keys:
10491052[` register` ]: #moduleregisterspecifier-parenturl-options
10501053[` string` ]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
10511054[` util .TextDecoder ` ]: util.md#class-utiltextdecoder
1055+ [chain]: #chaining
10521056[hooks]: #customization-hooks
10531057[load hook]: #loadurl-context-nextload
10541058[module wrapper]: modules.md#the-module-wrapper
0 commit comments