@@ -174,7 +174,7 @@ changes:
174
174
175
175
Module resolution and loading can be customized by registering a file which
176
176
exports a set of hooks. This can be done using the [` register` ][] method
177
- imported from ` node: module ` , which you can run before your application code by
177
+ from ` node: module ` , which you can run before your application code by
178
178
using the ` -- import ` flag:
179
179
180
180
` ` ` bash
@@ -203,7 +203,7 @@ node --import some-package/register ./my-app.js
203
203
` ` `
204
204
205
205
Where ` some- package ` has an [` " exports" ` ][] field defining the ` / register`
206
- export to map to a file that calls ` register ()` , like this ` register- hooks .js `
206
+ export to map to a file that calls ` register ()` , like the following ` register- hooks .js `
207
207
example.
208
208
209
209
Using ` -- import ` ensures that the hooks are registered before any application
@@ -235,14 +235,14 @@ import('./my-app.js');
235
235
In this example, we are registering the ` http- to- https` hooks, but they will
236
236
only be available for subsequently imported modules—in this case, ` my- app .js `
237
237
and anything it references via ` import ` or ` require` . If the
238
- ` import(' ./my-app.js' )` had instead been a static ` import ' ./my-app.js' ` , _the
239
- app would already have been loaded_ before the ` http-to-https` hooks were
240
- registered. This is part of the design of ES modules, where static imports are
241
- evaluated from the leaves of the tree first back to the trunk. There can be
238
+ ` import(' ./my-app.js' )` had instead been a static ` import ' ./my-app.js' ` , the
239
+ app would have _already_ been loaded ** before** the ` http-to-https` hooks were
240
+ registered. This due to the ES modules specification , where static imports are
241
+ evaluated from the leaves of the tree first, then back to the trunk. There can be
242
242
static imports _within_ ` my-app.js` , which will not be evaluated until
243
243
` my-app.js` is when it's dynamically imported.
244
244
245
- The ` my-app.js` file can also be CommonJS. Customization hooks will run for any
245
+ ` my-app.js` can also be CommonJS. Customization hooks will run for any
246
246
modules that it references via either ` import` or ` require` .
247
247
248
248
Finally, if all you want to do is register hooks before your app runs and you
@@ -261,7 +261,7 @@ It's possible to call `register` more than once:
261
261
// entrypoint.mjs
262
262
import { register } from ' node:module' ;
263
263
264
- register (new URL ( ' ./first.mjs' , import .meta.url) );
264
+ register (' ./first.mjs' , import .meta.url);
265
265
register (' ./second.mjs' , import .meta.url);
266
266
await import (' ./my-app.mjs' );
267
267
` ` `
@@ -272,7 +272,7 @@ const { register } = require('node:module');
272
272
const { pathToFileURL } = require (' node:url' );
273
273
274
274
const parentURL = pathToFileURL (__filename );
275
- register (new URL ( ' ./first.mjs' , parentURL) );
275
+ register (' ./first.mjs' , parentURL);
276
276
register (' ./second.mjs' , parentURL);
277
277
import (' ./my-app.mjs' );
278
278
` ` `
@@ -296,8 +296,8 @@ like ports.
296
296
import { register } from ' node:module' ;
297
297
import { MessageChannel } from ' node:worker_threads' ;
298
298
299
- // This example showcases how a message channel can be used to
300
- // communicate to the hooks, by sending `port2` to the hooks.
299
+ // This example demonstrates how a message channel can be used to
300
+ // communicate with the hooks, by sending `port2` to the hooks.
301
301
const { port1 , port2 } = new MessageChannel ();
302
302
303
303
port1 .on (' message' , (msg ) => {
@@ -317,7 +317,7 @@ const { pathToFileURL } = require('node:url');
317
317
const { MessageChannel } = require (' node:worker_threads' );
318
318
319
319
// This example showcases how a message channel can be used to
320
- // communicate to the hooks, by sending `port2` to the hooks.
320
+ // communicate with the hooks, by sending `port2` to the hooks.
321
321
const { port1 , port2 } = new MessageChannel ();
322
322
323
323
port1 .on (' message' , (msg ) => {
@@ -396,7 +396,7 @@ either:
396
396
Module customization code:
397
397
398
398
` ` ` mjs
399
- // In the below example this file is referenced as `/ path-to-my-hooks.js`.
399
+ // path-to-my-hooks.js
400
400
401
401
export async function initialize ({ number, port }) {
402
402
port .postMessage (` increment: ${ number + 1 } ` );
@@ -602,7 +602,7 @@ The final value of `format` must be one of the following:
602
602
The value of ` source` is ignored for type ` ' builtin' ` because currently it is
603
603
not possible to replace the value of a Node.js builtin (core) module.
604
604
605
- The value of ` source` can be omitted for type ` ' commonjs' ` :
605
+ Omitting vs providing a ` source` for ` ' commonjs' ` has very different effects :
606
606
607
607
* When a ` source` is provided, all ` require` calls from this module will be
608
608
processed by the ESM loader with registered ` resolve` and ` load` hooks; all
0 commit comments