1# Modules: ECMAScript modules 2 3<!--introduced_in=v8.5.0--> 4<!-- type=misc --> 5<!-- YAML 6added: v8.5.0 7changes: 8 - version: 9 - v12.20.0 10 pr-url: https://github.com/nodejs/node/pull/35249 11 description: Support for detection of CommonJS named exports. 12 - version: v12.20.0 13 pr-url: https://github.com/nodejs/node/pull/31974 14 description: Remove experimental modules warning. 15 - version: 16 - v12.17.0 17 pr-url: https://github.com/nodejs/node/pull/29866 18 description: Loading ECMAScript modules no longer requires a command-line flag. 19 - version: v12.0.0 20 pr-url: https://github.com/nodejs/node/pull/26745 21 description: 22 Add support for ES modules using `.js` file extension via `package.json` 23 `"type"` field. 24--> 25 26> Stability: 1 - Experimental 27 28## Introduction 29 30<!--name=esm--> 31 32ECMAScript modules are [the official standard format][] to package JavaScript 33code for reuse. Modules are defined using a variety of [`import`][] and 34[`export`][] statements. 35 36The following example of an ES module exports a function: 37 38```js 39// addTwo.mjs 40function addTwo(num) { 41 return num + 2; 42} 43 44export { addTwo }; 45``` 46 47The following example of an ES module imports the function from `addTwo.mjs`: 48 49```js 50// app.mjs 51import { addTwo } from './addTwo.mjs'; 52 53// Prints: 6 54console.log(addTwo(4)); 55``` 56 57Node.js fully supports ECMAScript modules as they are currently specified and 58provides limited interoperability between them and the existing module format, 59[CommonJS][]. 60 61Node.js contains support for ES Modules based upon the 62[Node.js EP for ES Modules][] and the [ECMAScript-modules implementation][]. 63 64Expect major changes in the implementation including interoperability support, 65specifier resolution, and default behavior. 66 67<!-- Anchors to make sure old links find a target --> 68<i id="#esm_package_json_type_field"></i> 69<i id="#esm_package_scope_and_file_extensions"></i> 70<i id="#esm_input_type_flag"></i> 71 72## Enabling 73 74<!-- type=misc --> 75 76Node.js treats JavaScript code as CommonJS modules by default. 77Authors can tell Node.js to treat JavaScript code as ECMAScript modules 78via the `.mjs` file extension, the `package.json` [`"type"`][] field, or the 79`--input-type` flag. See 80[Modules: Packages](packages.html#packages_determining_module_system) for more 81details. 82 83<!-- Anchors to make sure old links find a target --> 84<i id="esm_package_entry_points"></i> 85<i id="esm_main_entry_point_export"></i> 86<i id="esm_subpath_exports"></i> 87<i id="esm_package_exports_fallbacks"></i> 88<i id="esm_exports_sugar"></i> 89<i id="esm_conditional_exports"></i> 90<i id="esm_nested_conditions"></i> 91<i id="esm_self_referencing_a_package_using_its_name"></i> 92<i id="esm_internal_package_imports"></i> 93<i id="esm_dual_commonjs_es_module_packages"></i> 94<i id="esm_dual_package_hazard"></i> 95<i id="esm_writing_dual_packages_while_avoiding_or_minimizing_hazards"></i> 96<i id="esm_approach_1_use_an_es_module_wrapper"></i> 97<i id="esm_approach_2_isolate_state"></i> 98 99## Packages 100 101This section was moved to [Modules: Packages](packages.html). 102 103## `import` Specifiers 104 105### Terminology 106 107The _specifier_ of an `import` statement is the string after the `from` keyword, 108e.g. `'path'` in `import { sep } from 'path'`. Specifiers are also used in 109`export from` statements, and as the argument to an `import()` expression. 110 111There are four types of specifiers: 112 113* _Bare specifiers_ like `'some-package'`. They refer to an entry point of a 114 package by the package name. 115 116* _Deep import specifiers_ like `'some-package/lib/shuffle.mjs'`. They refer to 117 a path within a package prefixed by the package name. 118 119* _Relative specifiers_ like `'./startup.js'` or `'../config.mjs'`. They refer 120 to a path relative to the location of the importing file. 121 122* _Absolute specifiers_ like `'file:///opt/nodejs/config.js'`. They refer 123 directly and explicitly to a full path. 124 125Bare specifiers, and the bare specifier portion of deep import specifiers, are 126strings; but everything else in a specifier is a URL. 127 128`file:`, `node:`, and `data:` URLs are supported. A specifier like 129`'https://example.com/app.js'` may be supported by browsers but it is not 130supported in Node.js. 131 132Specifiers may not begin with `/` or `//`. These are reserved for potential 133future use. The root of the current volume may be referenced via `file:///`. 134 135#### `node:` Imports 136 137<!-- YAML 138added: v12.20.0 139--> 140 141`node:` URLs are supported as a means to load Node.js builtin modules. This 142URL scheme allows for builtin modules to be referenced by valid absolute URL 143strings. 144 145```js 146import fs from 'node:fs/promises'; 147``` 148 149#### `data:` Imports 150 151<!-- YAML 152added: v12.10.0 153--> 154 155[`data:` URLs][] are supported for importing with the following MIME types: 156 157* `text/javascript` for ES Modules 158* `application/json` for JSON 159* `application/wasm` for Wasm 160 161`data:` URLs only resolve [_Bare specifiers_][Terminology] for builtin modules 162and [_Absolute specifiers_][Terminology]. Resolving 163[_Relative specifiers_][Terminology] does not work because `data:` is not a 164[special scheme][]. For example, attempting to load `./foo` 165from `data:text/javascript,import "./foo";` fails to resolve because there 166is no concept of relative resolution for `data:` URLs. An example of a `data:` 167URLs being used is: 168 169```js 170import 'data:text/javascript,console.log("hello!");'; 171import _ from 'data:application/json,"world!"'; 172``` 173 174## `import.meta` 175 176* {Object} 177 178The `import.meta` metaproperty is an `Object` that contains the following 179property: 180 181* `url` {string} The absolute `file:` URL of the module. 182 183## Differences between ES modules and CommonJS 184 185### Mandatory file extensions 186 187A file extension must be provided when using the `import` keyword. Directory 188indexes (e.g. `'./startup/index.js'`) must also be fully specified. 189 190This behavior matches how `import` behaves in browser environments, assuming a 191typically configured server. 192 193### No `NODE_PATH` 194 195`NODE_PATH` is not part of resolving `import` specifiers. Please use symlinks 196if this behavior is desired. 197 198### No `require`, `exports`, `module.exports`, `__filename`, `__dirname` 199 200These CommonJS variables are not available in ES modules. 201 202`require` can be imported into an ES module using [`module.createRequire()`][]. 203 204Equivalents of `__filename` and `__dirname` can be created inside of each file 205via [`import.meta.url`][]. 206 207```js 208import { fileURLToPath } from 'url'; 209import { dirname } from 'path'; 210 211const __filename = fileURLToPath(import.meta.url); 212const __dirname = dirname(__filename); 213``` 214 215### No `require.resolve` 216 217Former use cases relying on `require.resolve` to determine the resolved path 218of a module can be supported via `import.meta.resolve`, which is experimental 219and supported via the `--experimental-import-meta-resolve` flag: 220 221```js 222(async () => { 223 const dependencyAsset = await import.meta.resolve('component-lib/asset.css'); 224})(); 225``` 226 227`import.meta.resolve` also accepts a second argument which is the parent module 228from which to resolve from: 229 230```js 231(async () => { 232 // Equivalent to import.meta.resolve('./dep') 233 await import.meta.resolve('./dep', import.meta.url); 234})(); 235``` 236 237This function is asynchronous because the ES module resolver in Node.js is 238asynchronous. With the introduction of [Top-Level Await][], these use cases 239will be easier as they won't require an async function wrapper. 240 241### No `require.extensions` 242 243`require.extensions` is not used by `import`. The expectation is that loader 244hooks can provide this workflow in the future. 245 246### No `require.cache` 247 248`require.cache` is not used by `import`. It has a separate cache. 249 250### URL-based paths 251 252ES modules are resolved and cached based upon 253[URL](https://url.spec.whatwg.org/) semantics. This means that files containing 254special characters such as `#` and `?` need to be escaped. 255 256Modules are loaded multiple times if the `import` specifier used to resolve 257them has a different query or fragment. 258 259```js 260import './foo.mjs?query=1'; // loads ./foo.mjs with query of "?query=1" 261import './foo.mjs?query=2'; // loads ./foo.mjs with query of "?query=2" 262``` 263 264For now, only modules using the `file:` protocol can be loaded. 265 266## Interoperability with CommonJS 267 268### `require` 269 270`require` always treats the files it references as CommonJS. This applies 271whether `require` is used the traditional way within a CommonJS environment, or 272in an ES module environment using [`module.createRequire()`][]. 273 274To include an ES module into CommonJS, use [`import()`][]. 275 276### `import` statements 277 278An `import` statement can reference an ES module or a CommonJS module. 279`import` statements are permitted only in ES modules. For similar functionality 280in CommonJS, see [`import()`][]. 281 282When importing [CommonJS modules](#esm_commonjs_namespaces), the 283`module.exports` object is provided as the default export. Named exports may be 284available, provided by static analysis as a convenience for better ecosystem 285compatibility. 286 287Additional experimental flags are available for importing 288[Wasm modules](#esm_experimental_wasm_modules) or 289[JSON modules](#esm_experimental_json_modules). For importing native modules or 290JSON modules unflagged, see [`module.createRequire()`][]. 291 292The _specifier_ of an `import` statement (the string after the `from` keyword) 293can either be an URL-style relative path like `'./file.mjs'` or a package name 294like `'fs'`. 295 296Like in CommonJS, files within packages can be accessed by appending a path to 297the package name; unless the package’s [`package.json`][] contains an 298[`"exports"`][] field, in which case files within packages need to be accessed 299via the path defined in [`"exports"`][]. 300 301```js 302import { sin, cos } from 'geometry/trigonometry-functions.mjs'; 303``` 304 305### `import()` expressions 306 307[Dynamic `import()`][] is supported in both CommonJS and ES modules. It can be 308used to include ES module files from CommonJS code. 309 310## CommonJS Namespaces 311 312CommonJS modules consist of a `module.exports` object which can be of any type. 313 314When importing a CommonJS module, it can be reliably imported using the ES 315module default import or its corresponding sugar syntax: 316 317<!-- eslint-disable no-duplicate-imports --> 318```js 319import { default as cjs } from 'cjs'; 320 321// The following import statement is "syntax sugar" (equivalent but sweeter) 322// for `{ default as cjsSugar }` in the above import statement: 323import cjsSugar from 'cjs'; 324 325console.log(cjs); 326console.log(cjs === cjsSugar); 327// Prints: 328// <module.exports> 329// true 330``` 331 332The ECMAScript Module Namespace representation of a CommonJS module is always 333a namespace with a `default` export key pointing to the CommonJS 334`module.exports` value. 335 336This Module Namespace Exotic Object can be directly observed either when using 337`import * as m from 'cjs'` or a dynamic import: 338 339<!-- eslint-skip --> 340```js 341import * as m from 'cjs'; 342console.log(m); 343console.log(m === await import('cjs')); 344// Prints: 345// [Module] { default: <module.exports> } 346// true 347``` 348 349For better compatibility with existing usage in the JS ecosystem, Node.js 350in addition attempts to determine the CommonJS named exports of every imported 351CommonJS module to provide them as separate ES module exports using a static 352analysis process. 353 354For example, consider a CommonJS module written: 355 356```js 357// cjs.cjs 358exports.name = 'exported'; 359``` 360 361The preceding module supports named imports in ES modules: 362 363<!-- eslint-disable no-duplicate-imports --> 364```js 365import { name } from './cjs.cjs'; 366console.log(name); 367// Prints: 'exported' 368 369import cjs from './cjs.cjs'; 370console.log(cjs); 371// Prints: { name: 'exported' } 372 373import * as m from './cjs.cjs'; 374console.log(m); 375// Prints: [Module] { default: { name: 'exported' }, name: 'exported' } 376``` 377 378As can be seen from the last example of the Module Namespace Exotic Object being 379logged, the `name` export is copied off of the `module.exports` object and set 380directly on the ES module namespace when the module is imported. 381 382Live binding updates or new exports added to `module.exports` are not detected 383for these named exports. 384 385The detection of named exports is based on common syntax patterns but does not 386always correctly detect named exports. In these cases, using the default 387import form described above can be a better option. 388 389Named exports detection covers many common export patterns, reexport patterns 390and build tool and transpiler outputs. See [cjs-module-lexer][] for the exact 391semantics implemented. 392 393## Builtin modules 394 395[Core modules][] provide named exports of their public API. A 396default export is also provided which is the value of the CommonJS exports. 397The default export can be used for, among other things, modifying the named 398exports. Named exports of builtin modules are updated only by calling 399[`module.syncBuiltinESMExports()`][]. 400 401```js 402import EventEmitter from 'events'; 403const e = new EventEmitter(); 404``` 405 406```js 407import { readFile } from 'fs'; 408readFile('./foo.txt', (err, source) => { 409 if (err) { 410 console.error(err); 411 } else { 412 console.log(source); 413 } 414}); 415``` 416 417```js 418import fs, { readFileSync } from 'fs'; 419import { syncBuiltinESMExports } from 'module'; 420 421fs.readFileSync = () => Buffer.from('Hello, ESM'); 422syncBuiltinESMExports(); 423 424fs.readFileSync === readFileSync; 425``` 426 427## CommonJS, JSON, and native modules 428 429CommonJS, JSON, and native modules can be used with 430[`module.createRequire()`][]. 431 432```js 433// cjs.cjs 434module.exports = 'cjs'; 435 436// esm.mjs 437import { createRequire } from 'module'; 438 439const require = createRequire(import.meta.url); 440 441const cjs = require('./cjs.cjs'); 442cjs === 'cjs'; // true 443``` 444 445## Experimental JSON modules 446 447Currently importing JSON modules are only supported in the `commonjs` mode 448and are loaded using the CJS loader. [WHATWG JSON modules specification][] are 449still being standardized, and are experimentally supported by including the 450additional flag `--experimental-json-modules` when running Node.js. 451 452When the `--experimental-json-modules` flag is included, both the 453`commonjs` and `module` mode use the new experimental JSON 454loader. The imported JSON only exposes a `default`. There is no 455support for named exports. A cache entry is created in the CommonJS 456cache to avoid duplication. The same object is returned in 457CommonJS if the JSON module has already been imported from the 458same path. 459 460Assuming an `index.mjs` with 461 462<!-- eslint-skip --> 463```js 464import packageConfig from './package.json'; 465``` 466 467The `--experimental-json-modules` flag is needed for the module 468to work. 469 470```bash 471node index.mjs # fails 472node --experimental-json-modules index.mjs # works 473``` 474 475## Experimental Wasm modules 476 477Importing Web Assembly modules is supported under the 478`--experimental-wasm-modules` flag, allowing any `.wasm` files to be 479imported as normal modules while also supporting their module imports. 480 481This integration is in line with the 482[ES Module Integration Proposal for Web Assembly][]. 483 484For example, an `index.mjs` containing: 485 486```js 487import * as M from './module.wasm'; 488console.log(M); 489``` 490 491executed under: 492 493```bash 494node --experimental-wasm-modules index.mjs 495``` 496 497would provide the exports interface for the instantiation of `module.wasm`. 498 499## Experimental loaders 500 501**Note: This API is currently being redesigned and will still change.** 502 503<!-- type=misc --> 504 505To customize the default module resolution, loader hooks can optionally be 506provided via a `--experimental-loader ./loader-name.mjs` argument to Node.js. 507 508When hooks are used they only apply to ES module loading and not to any 509CommonJS modules loaded. 510 511### Hooks 512 513#### `resolve(specifier, context, defaultResolve)` 514 515> Note: The loaders API is being redesigned. This hook may disappear or its 516> signature may change. Do not rely on the API described below. 517 518* `specifier` {string} 519* `context` {Object} 520 * `conditions` {string[]} 521 * `parentURL` {string} 522* `defaultResolve` {Function} 523* Returns: {Object} 524 * `url` {string} 525 526The `resolve` hook returns the resolved file URL for a given module specifier 527and parent URL. The module specifier is the string in an `import` statement or 528`import()` expression, and the parent URL is the URL of the module that imported 529this one, or `undefined` if this is the main entry point for the application. 530 531The `conditions` property on the `context` is an array of conditions for 532[Conditional exports][] that apply to this resolution request. They can be used 533for looking up conditional mappings elsewhere or to modify the list when calling 534the default resolution logic. 535 536The current [package exports conditions][Conditional Exports] are always in 537the `context.conditions` array passed into the hook. To guarantee _default 538Node.js module specifier resolution behavior_ when calling `defaultResolve`, the 539`context.conditions` array passed to it _must_ include _all_ elements of the 540`context.conditions` array originally passed into the `resolve` hook. 541 542```js 543/** 544 * @param {string} specifier 545 * @param {{ 546 * conditions: !Array<string>, 547 * parentURL: !(string | undefined), 548 * }} context 549 * @param {Function} defaultResolve 550 * @returns {Promise<{ url: string }>} 551 */ 552export async function resolve(specifier, context, defaultResolve) { 553 const { parentURL = null } = context; 554 if (Math.random() > 0.5) { // Some condition. 555 // For some or all specifiers, do some custom logic for resolving. 556 // Always return an object of the form {url: <string>}. 557 return { 558 url: parentURL ? 559 new URL(specifier, parentURL).href : 560 new URL(specifier).href, 561 }; 562 } 563 if (Math.random() < 0.5) { // Another condition. 564 // When calling `defaultResolve`, the arguments can be modified. In this 565 // case it's adding another value for matching conditional exports. 566 return defaultResolve(specifier, { 567 ...context, 568 conditions: [...context.conditions, 'another-condition'], 569 }); 570 } 571 // Defer to Node.js for all other specifiers. 572 return defaultResolve(specifier, context, defaultResolve); 573} 574``` 575 576#### `getFormat(url, context, defaultGetFormat)` 577 578> Note: The loaders API is being redesigned. This hook may disappear or its 579> signature may change. Do not rely on the API described below. 580 581* `url` {string} 582* `context` {Object} 583* `defaultGetFormat` {Function} 584* Returns: {Object} 585 * `format` {string} 586 587The `getFormat` hook provides a way to define a custom method of determining how 588a URL should be interpreted. The `format` returned also affects what the 589acceptable forms of source values are for a module when parsing. This can be one 590of the following: 591 592| `format` | Description | Acceptable Types For `source` Returned by `getSource` or `transformSource` | 593| ------------ | ------------------------------ | -------------------------------------------------------------------------- | 594| `'builtin'` | Load a Node.js builtin module | Not applicable | 595| `'dynamic'` | Use a [dynamic instantiate hook][] | Not applicable | 596| `'commonjs'` | Load a Node.js CommonJS module | Not applicable | 597| `'json'` | Load a JSON file | { [`string`][], [`ArrayBuffer`][], [`TypedArray`][] } | 598| `'module'` | Load an ES module | { [`string`][], [`ArrayBuffer`][], [`TypedArray`][] } | 599| `'wasm'` | Load a WebAssembly module | { [`ArrayBuffer`][], [`TypedArray`][] } | 600 601Note: These types all correspond to classes defined in ECMAScript. 602 603* The specific [`ArrayBuffer`][] object is a [`SharedArrayBuffer`][]. 604* The specific [`TypedArray`][] object is a [`Uint8Array`][]. 605 606Note: If the source value of a text-based format (i.e., `'json'`, `'module'`) is 607not a string, it is converted to a string using [`util.TextDecoder`][]. 608 609```js 610/** 611 * @param {string} url 612 * @param {Object} context (currently empty) 613 * @param {Function} defaultGetFormat 614 * @returns {Promise<{ format: string }>} 615 */ 616export async function getFormat(url, context, defaultGetFormat) { 617 if (Math.random() > 0.5) { // Some condition. 618 // For some or all URLs, do some custom logic for determining format. 619 // Always return an object of the form {format: <string>}, where the 620 // format is one of the strings in the preceding table. 621 return { 622 format: 'module', 623 }; 624 } 625 // Defer to Node.js for all other URLs. 626 return defaultGetFormat(url, context, defaultGetFormat); 627} 628``` 629 630#### `getSource(url, context, defaultGetSource)` 631 632> Note: The loaders API is being redesigned. This hook may disappear or its 633> signature may change. Do not rely on the API described below. 634 635* `url` {string} 636* `context` {Object} 637 * `format` {string} 638* `defaultGetSource` {Function} 639* Returns: {Object} 640 * `source` {string|SharedArrayBuffer|Uint8Array} 641 642The `getSource` hook provides a way to define a custom method for retrieving 643the source code of an ES module specifier. This would allow a loader to 644potentially avoid reading files from disk. 645 646```js 647/** 648 * @param {string} url 649 * @param {{ format: string }} context 650 * @param {Function} defaultGetSource 651 * @returns {Promise<{ source: !(string | SharedArrayBuffer | Uint8Array) }>} 652 */ 653export async function getSource(url, context, defaultGetSource) { 654 const { format } = context; 655 if (Math.random() > 0.5) { // Some condition. 656 // For some or all URLs, do some custom logic for retrieving the source. 657 // Always return an object of the form {source: <string|buffer>}. 658 return { 659 source: '...', 660 }; 661 } 662 // Defer to Node.js for all other URLs. 663 return defaultGetSource(url, context, defaultGetSource); 664} 665``` 666 667#### `transformSource(source, context, defaultTransformSource)` 668 669```console 670NODE_OPTIONS='--experimental-loader ./custom-loader.mjs' node x.js 671``` 672 673> Note: The loaders API is being redesigned. This hook may disappear or its 674> signature may change. Do not rely on the API described below. 675 676* `source` {string|SharedArrayBuffer|Uint8Array} 677* `context` {Object} 678 * `format` {string} 679 * `url` {string} 680* Returns: {Object} 681 * `source` {string|SharedArrayBuffer|Uint8Array} 682 683The `transformSource` hook provides a way to modify the source code of a loaded 684ES module file after the source string has been loaded but before Node.js has 685done anything with it. 686 687If this hook is used to convert unknown-to-Node.js file types into executable 688JavaScript, a resolve hook is also necessary in order to register any 689unknown-to-Node.js file extensions. See the [transpiler loader example][] below. 690 691```js 692/** 693 * @param {!(string | SharedArrayBuffer | Uint8Array)} source 694 * @param {{ 695 * format: string, 696 * url: string, 697 * }} context 698 * @param {Function} defaultTransformSource 699 * @returns {Promise<{ source: !(string | SharedArrayBuffer | Uint8Array) }>} 700 */ 701export async function transformSource(source, context, defaultTransformSource) { 702 const { url, format } = context; 703 if (Math.random() > 0.5) { // Some condition. 704 // For some or all URLs, do some custom logic for modifying the source. 705 // Always return an object of the form {source: <string|buffer>}. 706 return { 707 source: '...', 708 }; 709 } 710 // Defer to Node.js for all other sources. 711 return defaultTransformSource(source, context, defaultTransformSource); 712} 713``` 714 715#### `getGlobalPreloadCode()` 716 717> Note: The loaders API is being redesigned. This hook may disappear or its 718> signature may change. Do not rely on the API described below. 719 720* Returns: {string} 721 722Sometimes it might be necessary to run some code inside of the same global scope 723that the application runs in. This hook allows the return of a string that is 724run as sloppy-mode script on startup. 725 726Similar to how CommonJS wrappers work, the code runs in an implicit function 727scope. The only argument is a `require`-like function that can be used to load 728builtins like "fs": `getBuiltin(request: string)`. 729 730If the code needs more advanced `require` features, it has to construct 731its own `require` using `module.createRequire()`. 732 733```js 734/** 735 * @returns {string} Code to run before application startup 736 */ 737export function getGlobalPreloadCode() { 738 return `\ 739globalThis.someInjectedProperty = 42; 740console.log('I just set some globals!'); 741 742const { createRequire } = getBuiltin('module'); 743 744const require = createRequire(process.cwd() + '/<preload>'); 745// [...] 746`; 747} 748``` 749 750#### <code>dynamicInstantiate</code> hook 751 752> Note: The loaders API is being redesigned. This hook may disappear or its 753> signature may change. Do not rely on the API described below. 754 755To create a custom dynamic module that doesn't correspond to one of the 756existing `format` interpretations, the `dynamicInstantiate` hook can be used. 757This hook is called only for modules that return `format: 'dynamic'` from 758the `getFormat` hook. 759 760```js 761/** 762 * @param {string} url 763 * @returns {object} response 764 * @returns {array} response.exports 765 * @returns {function} response.execute 766 */ 767export async function dynamicInstantiate(url) { 768 return { 769 exports: ['customExportName'], 770 execute: (exports) => { 771 // Get and set functions provided for pre-allocated export names 772 exports.customExportName.set('value'); 773 } 774 }; 775} 776``` 777 778With the list of module exports provided upfront, the `execute` function will 779then be called at the exact point of module evaluation order for that module 780in the import tree. 781 782### Examples 783 784The various loader hooks can be used together to accomplish wide-ranging 785customizations of Node.js’ code loading and evaluation behaviors. 786 787#### HTTPS loader 788 789In current Node.js, specifiers starting with `https://` are unsupported. The 790loader below registers hooks to enable rudimentary support for such specifiers. 791While this may seem like a significant improvement to Node.js core 792functionality, there are substantial downsides to actually using this loader: 793performance is much slower than loading files from disk, there is no caching, 794and there is no security. 795 796```js 797// https-loader.mjs 798import { get } from 'https'; 799 800export function resolve(specifier, context, defaultResolve) { 801 const { parentURL = null } = context; 802 803 // Normally Node.js would error on specifiers starting with 'https://', so 804 // this hook intercepts them and converts them into absolute URLs to be 805 // passed along to the later hooks below. 806 if (specifier.startsWith('https://')) { 807 return { 808 url: specifier 809 }; 810 } else if (parentURL && parentURL.startsWith('https://')) { 811 return { 812 url: new URL(specifier, parentURL).href 813 }; 814 } 815 816 // Let Node.js handle all other specifiers. 817 return defaultResolve(specifier, context, defaultResolve); 818} 819 820export function getFormat(url, context, defaultGetFormat) { 821 // This loader assumes all network-provided JavaScript is ES module code. 822 if (url.startsWith('https://')) { 823 return { 824 format: 'module' 825 }; 826 } 827 828 // Let Node.js handle all other URLs. 829 return defaultGetFormat(url, context, defaultGetFormat); 830} 831 832export function getSource(url, context, defaultGetSource) { 833 // For JavaScript to be loaded over the network, we need to fetch and 834 // return it. 835 if (url.startsWith('https://')) { 836 return new Promise((resolve, reject) => { 837 get(url, (res) => { 838 let data = ''; 839 res.on('data', (chunk) => data += chunk); 840 res.on('end', () => resolve({ source: data })); 841 }).on('error', (err) => reject(err)); 842 }); 843 } 844 845 // Let Node.js handle all other URLs. 846 return defaultGetSource(url, context, defaultGetSource); 847} 848``` 849 850```js 851// main.mjs 852import { VERSION } from 'https://coffeescript.org/browser-compiler-modern/coffeescript.js'; 853 854console.log(VERSION); 855``` 856 857With the preceding loader, running 858`node --experimental-loader ./https-loader.mjs ./main.mjs` 859prints the current version of CoffeeScript per the module at the URL in 860`main.mjs`. 861 862#### Transpiler loader 863 864Sources that are in formats Node.js doesn’t understand can be converted into 865JavaScript using the [`transformSource` hook][]. Before that hook gets called, 866however, other hooks need to tell Node.js not to throw an error on unknown file 867types; and to tell Node.js how to load this new file type. 868 869This is less performant than transpiling source files before running 870Node.js; a transpiler loader should only be used for development and testing 871purposes. 872 873```js 874// coffeescript-loader.mjs 875import { URL, pathToFileURL } from 'url'; 876import CoffeeScript from 'coffeescript'; 877 878const baseURL = pathToFileURL(`${process.cwd()}/`).href; 879 880// CoffeeScript files end in .coffee, .litcoffee or .coffee.md. 881const extensionsRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/; 882 883export function resolve(specifier, context, defaultResolve) { 884 const { parentURL = baseURL } = context; 885 886 // Node.js normally errors on unknown file extensions, so return a URL for 887 // specifiers ending in the CoffeeScript file extensions. 888 if (extensionsRegex.test(specifier)) { 889 return { 890 url: new URL(specifier, parentURL).href 891 }; 892 } 893 894 // Let Node.js handle all other specifiers. 895 return defaultResolve(specifier, context, defaultResolve); 896} 897 898export function getFormat(url, context, defaultGetFormat) { 899 // Now that we patched resolve to let CoffeeScript URLs through, we need to 900 // tell Node.js what format such URLs should be interpreted as. For the 901 // purposes of this loader, all CoffeeScript URLs are ES modules. 902 if (extensionsRegex.test(url)) { 903 return { 904 format: 'module' 905 }; 906 } 907 908 // Let Node.js handle all other URLs. 909 return defaultGetFormat(url, context, defaultGetFormat); 910} 911 912export function transformSource(source, context, defaultTransformSource) { 913 const { url, format } = context; 914 915 if (extensionsRegex.test(url)) { 916 return { 917 source: CoffeeScript.compile(source, { bare: true }) 918 }; 919 } 920 921 // Let Node.js handle all other sources. 922 return defaultTransformSource(source, context, defaultTransformSource); 923} 924``` 925 926```coffee 927# main.coffee 928import { scream } from './scream.coffee' 929console.log scream 'hello, world' 930 931import { version } from 'process' 932console.log "Brought to you by Node.js version #{version}" 933``` 934 935```coffee 936# scream.coffee 937export scream = (str) -> str.toUpperCase() 938``` 939 940With the preceding loader, running 941`node --experimental-loader ./coffeescript-loader.mjs main.coffee` 942causes `main.coffee` to be turned into JavaScript after its source code is 943loaded from disk but before Node.js executes it; and so on for any `.coffee`, 944`.litcoffee` or `.coffee.md` files referenced via `import` statements of any 945loaded file. 946 947## Resolution algorithm 948 949### Features 950 951The resolver has the following properties: 952 953* FileURL-based resolution as is used by ES modules 954* Support for builtin module loading 955* Relative and absolute URL resolution 956* No default extensions 957* No folder mains 958* Bare specifier package resolution lookup through node_modules 959 960### Resolver algorithm 961 962The algorithm to load an ES module specifier is given through the 963**ESM_RESOLVE** method below. It returns the resolved URL for a 964module specifier relative to a parentURL. 965 966The algorithm to determine the module format of a resolved URL is 967provided by **ESM_FORMAT**, which returns the unique module 968format for any file. The _"module"_ format is returned for an ECMAScript 969Module, while the _"commonjs"_ format is used to indicate loading through the 970legacy CommonJS loader. Additional formats such as _"addon"_ can be extended in 971future updates. 972 973In the following algorithms, all subroutine errors are propagated as errors 974of these top-level routines unless stated otherwise. 975 976_defaultConditions_ is the conditional environment name array, 977`["node", "import"]`. 978 979The resolver can throw the following errors: 980* _Invalid Module Specifier_: Module specifier is an invalid URL, package name 981 or package subpath specifier. 982* _Invalid Package Configuration_: package.json configuration is invalid or 983 contains an invalid configuration. 984* _Invalid Package Target_: Package exports or imports define a target module 985 for the package that is an invalid type or string target. 986* _Package Path Not Exported_: Package exports do not define or permit a target 987 subpath in the package for the given module. 988* _Package Import Not Defined_: Package imports do not define the specifier. 989* _Module Not Found_: The package or module requested does not exist. 990 991### Resolver Algorithm Specification 992 993**ESM_RESOLVE**(_specifier_, _parentURL_) 994 995> 1. Let _resolved_ be **undefined**. 996> 1. If _specifier_ is a valid URL, then 997> 1. Set _resolved_ to the result of parsing and reserializing 998> _specifier_ as a URL. 999> 1. Otherwise, if _specifier_ starts with _"/"_, _"./"_ or _"../"_, then 1000> 1. Set _resolved_ to the URL resolution of _specifier_ relative to 1001> _parentURL_. 1002> 1. Otherwise, if _specifier_ starts with _"#"_, then 1003> 1. Set _resolved_ to the destructured value of the result of 1004> **PACKAGE_IMPORTS_RESOLVE**(_specifier_, _parentURL_, 1005> _defaultConditions_). 1006> 1. Otherwise, 1007> 1. Note: _specifier_ is now a bare specifier. 1008> 1. Set _resolved_ the result of 1009> **PACKAGE_RESOLVE**(_specifier_, _parentURL_). 1010> 1. If _resolved_ contains any percent encodings of _"/"_ or _"\\"_ (_"%2f"_ 1011> and _"%5C"_ respectively), then 1012> 1. Throw an _Invalid Module Specifier_ error. 1013> 1. If the file at _resolved_ is a directory, then 1014> 1. Throw an _Unsupported Directory Import_ error. 1015> 1. If the file at _resolved_ does not exist, then 1016> 1. Throw a _Module Not Found_ error. 1017> 1. Set _resolved_ to the real path of _resolved_. 1018> 1. Let _format_ be the result of **ESM_FORMAT**(_resolved_). 1019> 1. Load _resolved_ as module format, _format_. 1020> 1. Return _resolved_. 1021 1022**PACKAGE_RESOLVE**(_packageSpecifier_, _parentURL_) 1023 1024> 1. Let _packageName_ be **undefined**. 1025> 1. If _packageSpecifier_ is an empty string, then 1026> 1. Throw an _Invalid Module Specifier_ error. 1027> 1. If _packageSpecifier_ does not start with _"@"_, then 1028> 1. Set _packageName_ to the substring of _packageSpecifier_ until the first 1029> _"/"_ separator or the end of the string. 1030> 1. Otherwise, 1031> 1. If _packageSpecifier_ does not contain a _"/"_ separator, then 1032> 1. Throw an _Invalid Module Specifier_ error. 1033> 1. Set _packageName_ to the substring of _packageSpecifier_ 1034> until the second _"/"_ separator or the end of the string. 1035> 1. If _packageName_ starts with _"."_ or contains _"\\"_ or _"%"_, then 1036> 1. Throw an _Invalid Module Specifier_ error. 1037> 1. Let _packageSubpath_ be _"."_ concatenated with the substring of 1038> _packageSpecifier_ from the position at the length of _packageName_. 1039> 1. Let _selfUrl_ be the result of 1040> **PACKAGE_SELF_RESOLVE**(_packageName_, _packageSubpath_, _parentURL_). 1041> 1. If _selfUrl_ is not **undefined**, return _selfUrl_. 1042> 1. If _packageSubpath_ is _"."_ and _packageName_ is a Node.js builtin 1043> module, then 1044> 1. Return the string _"node:"_ concatenated with _packageSpecifier_. 1045> 1. While _parentURL_ is not the file system root, 1046> 1. Let _packageURL_ be the URL resolution of _"node_modules/"_ 1047> concatenated with _packageSpecifier_, relative to _parentURL_. 1048> 1. Set _parentURL_ to the parent folder URL of _parentURL_. 1049> 1. If the folder at _packageURL_ does not exist, then 1050> 1. Set _parentURL_ to the parent URL path of _parentURL_. 1051> 1. Continue the next loop iteration. 1052> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_). 1053> 1. If _pjson_ is not **null** and _pjson_._exports_ is not **null** or 1054> **undefined**, then 1055> 1. Let _exports_ be _pjson.exports_. 1056> 1. Return the _resolved_ destructured value of the result of 1057> **PACKAGE_EXPORTS_RESOLVE**(_packageURL_, _packageSubpath_, 1058> _pjson.exports_, _defaultConditions_). 1059> 1. Otherwise, if _packageSubpath_ is equal to _"."_, then 1060> 1. Return the result applying the legacy **LOAD_AS_DIRECTORY** 1061> CommonJS resolver to _packageURL_, throwing a _Module Not Found_ 1062> error for no resolution. 1063> 1. Otherwise, 1064> 1. Return the URL resolution of _packageSubpath_ in _packageURL_. 1065> 1. Throw a _Module Not Found_ error. 1066 1067**PACKAGE_SELF_RESOLVE**(_packageName_, _packageSubpath_, _parentURL_) 1068 1069> 1. Let _packageURL_ be the result of **READ_PACKAGE_SCOPE**(_parentURL_). 1070> 1. If _packageURL_ is **null**, then 1071> 1. Return **undefined**. 1072> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_). 1073> 1. If _pjson_ is **null** or if _pjson_._exports_ is **null** or 1074> **undefined**, then 1075> 1. Return **undefined**. 1076> 1. If _pjson.name_ is equal to _packageName_, then 1077> 1. Return the _resolved_ destructured value of the result of 1078> **PACKAGE_EXPORTS_RESOLVE**(_packageURL_, _subpath_, _pjson.exports_, 1079> _defaultConditions_). 1080> 1. Otherwise, return **undefined**. 1081 1082**PACKAGE_EXPORTS_RESOLVE**(_packageURL_, _subpath_, _exports_, _conditions_) 1083 1084> 1. If _exports_ is an Object with both a key starting with _"."_ and a key not 1085> starting with _"."_, throw an _Invalid Package Configuration_ error. 1086> 1. If _subpath_ is equal to _"."_, then 1087> 1. Let _mainExport_ be **undefined**. 1088> 1. If _exports_ is a String or Array, or an Object containing no keys 1089> starting with _"."_, then 1090> 1. Set _mainExport_ to _exports_. 1091> 1. Otherwise if _exports_ is an Object containing a _"."_ property, then 1092> 1. Set _mainExport_ to _exports_\[_"."_\]. 1093> 1. If _mainExport_ is not **undefined**, then 1094> 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**( 1095> _packageURL_, _mainExport_, _""_, **false**, **false**, 1096> _conditions_). 1097> 1. If _resolved_ is not **null** or **undefined**, then 1098> 1. Return _resolved_. 1099> 1. Otherwise, if _exports_ is an Object and all keys of _exports_ start with 1100> _"."_, then 1101> 1. Let _matchKey_ be the string _"./"_ concatenated with _subpath_. 1102> 1. Let _resolvedMatch_ be result of **PACKAGE_IMPORTS_EXPORTS_RESOLVE**( 1103> _matchKey_, _exports_, _packageURL_, **false**, _conditions_). 1104> 1. If _resolvedMatch_._resolve_ is not **null** or **undefined**, then 1105> 1. Return _resolvedMatch_. 1106> 1. Throw a _Package Path Not Exported_ error. 1107 1108**PACKAGE_IMPORTS_RESOLVE**(_specifier_, _parentURL_, _conditions_) 1109 1110> 1. Assert: _specifier_ begins with _"#"_. 1111> 1. If _specifier_ is exactly equal to _"#"_ or starts with _"#/"_, then 1112> 1. Throw an _Invalid Module Specifier_ error. 1113> 1. Let _packageURL_ be the result of **READ_PACKAGE_SCOPE**(_parentURL_). 1114> 1. If _packageURL_ is not **null**, then 1115> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_). 1116> 1. If _pjson.imports_ is a non-null Object, then 1117> 1. Let _resolvedMatch_ be the result of 1118> **PACKAGE_IMPORTS_EXPORTS_RESOLVE**(_specifier_, _pjson.imports_, 1119> _packageURL_, **true**, _conditions_). 1120> 1. If _resolvedMatch_._resolve_ is not **null** or **undefined**, then 1121> 1. Return _resolvedMatch_. 1122> 1. Throw a _Package Import Not Defined_ error. 1123 1124**PACKAGE_IMPORTS_EXPORTS_RESOLVE**(_matchKey_, _matchObj_, _packageURL_, 1125_isImports_, _conditions_) 1126 1127> 1. If _matchKey_ is a key of _matchObj_, and does not end in _"*"_, then 1128> 1. Let _target_ be the value of _matchObj_\[_matchKey_\]. 1129> 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**( 1130> _packageURL_, _target_, _""_, **false**, _isImports_, _conditions_). 1131> 1. Return the object _{ resolved, exact: **true** }_. 1132> 1. Let _expansionKeys_ be the list of keys of _matchObj_ ending in _"/"_ 1133> or _"*"_, sorted by length descending. 1134> 1. For each key _expansionKey_ in _expansionKeys_, do 1135> 1. If _expansionKey_ ends in _"*"_ and _matchKey_ starts with but is 1136> not equal to the substring of _expansionKey_ excluding the last _"*"_ 1137> character, then 1138> 1. Let _target_ be the value of _matchObj_\[_expansionKey_\]. 1139> 1. Let _subpath_ be the substring of _matchKey_ starting at the 1140> index of the length of _expansionKey_ minus one. 1141> 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**( 1142> _packageURL_, _target_, _subpath_, **true**, _isImports_, 1143> _conditions_). 1144> 1. Return the object _{ resolved, exact: **true** }_. 1145> 1. If _matchKey_ starts with _expansionKey_, then 1146> 1. Let _target_ be the value of _matchObj_\[_expansionKey_\]. 1147> 1. Let _subpath_ be the substring of _matchKey_ starting at the 1148> index of the length of _expansionKey_. 1149> 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**( 1150> _packageURL_, _target_, _subpath_, **false**, _isImports_, 1151> _conditions_). 1152> 1. Return the object _{ resolved, exact: **false** }_. 1153> 1. Return the object _{ resolved: **null**, exact: **true** }_. 1154 1155**PACKAGE_TARGET_RESOLVE**(_packageURL_, _target_, _subpath_, _pattern_, 1156_internal_, _conditions_) 1157 1158> 1. If _target_ is a String, then 1159> 1. If _pattern_ is **false**, _subpath_ has non-zero length and _target_ 1160> does not end with _"/"_, throw an _Invalid Module Specifier_ error. 1161> 1. If _target_ does not start with _"./"_, then 1162> 1. If _internal_ is **true** and _target_ does not start with _"../"_ or 1163> _"/"_ and is not a valid URL, then 1164> 1. If _pattern_ is **true**, then 1165> 1. Return **PACKAGE_RESOLVE**(_target_ with every instance of 1166> _"*"_ replaced by _subpath_, _packageURL_ + _"/"_)_. 1167> 1. Return **PACKAGE_RESOLVE**(_target_ + _subpath_, 1168> _packageURL_ + _"/"_)_. 1169> 1. Otherwise, throw an _Invalid Package Target_ error. 1170> 1. If _target_ split on _"/"_ or _"\\"_ contains any _"."_, _".."_ or 1171> _"node_modules"_ segments after the first segment, throw an 1172> _Invalid Package Target_ error. 1173> 1. Let _resolvedTarget_ be the URL resolution of the concatenation of 1174> _packageURL_ and _target_. 1175> 1. Assert: _resolvedTarget_ is contained in _packageURL_. 1176> 1. If _subpath_ split on _"/"_ or _"\\"_ contains any _"."_, _".."_ or 1177> _"node_modules"_ segments, throw an _Invalid Module Specifier_ error. 1178> 1. If _pattern_ is **true**, then 1179> 1. Return the URL resolution of _resolvedTarget_ with every instance of 1180> _"*"_ replaced with _subpath_. 1181> 1. Otherwise, 1182> 1. Return the URL resolution of the concatenation of _subpath_ and 1183> _resolvedTarget_. 1184> 1. Otherwise, if _target_ is a non-null Object, then 1185> 1. If _exports_ contains any index property keys, as defined in ECMA-262 1186> [6.1.7 Array Index][], throw an _Invalid Package Configuration_ error. 1187> 1. For each property _p_ of _target_, in object insertion order as, 1188> 1. If _p_ equals _"default"_ or _conditions_ contains an entry for _p_, 1189> then 1190> 1. Let _targetValue_ be the value of the _p_ property in _target_. 1191> 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**( 1192> _packageURL_, _targetValue_, _subpath_, _pattern_, _internal_, 1193> _conditions_). 1194> 1. If _resolved_ is equal to **undefined**, continue the loop. 1195> 1. Return _resolved_. 1196> 1. Return **undefined**. 1197> 1. Otherwise, if _target_ is an Array, then 1198> 1. If _target.length is zero, return **null**. 1199> 1. For each item _targetValue_ in _target_, do 1200> 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**( 1201> _packageURL_, _targetValue_, _subpath_, _pattern_, _internal_, 1202> _conditions_), continuing the loop on any _Invalid Package Target_ 1203> error. 1204> 1. If _resolved_ is **undefined**, continue the loop. 1205> 1. Return _resolved_. 1206> 1. Return or throw the last fallback resolution **null** return or error. 1207> 1. Otherwise, if _target_ is _null_, return **null**. 1208> 1. Otherwise throw an _Invalid Package Target_ error. 1209 1210**ESM_FORMAT**(_url_) 1211 1212> 1. Assert: _url_ corresponds to an existing file. 1213> 1. Let _pjson_ be the result of **READ_PACKAGE_SCOPE**(_url_). 1214> 1. If _url_ ends in _".mjs"_, then 1215> 1. Return _"module"_. 1216> 1. If _url_ ends in _".cjs"_, then 1217> 1. Return _"commonjs"_. 1218> 1. If _pjson?.type_ exists and is _"module"_, then 1219> 1. If _url_ ends in _".js"_, then 1220> 1. Return _"module"_. 1221> 1. Throw an _Unsupported File Extension_ error. 1222> 1. Otherwise, 1223> 1. Throw an _Unsupported File Extension_ error. 1224 1225**READ_PACKAGE_SCOPE**(_url_) 1226 1227> 1. Let _scopeURL_ be _url_. 1228> 1. While _scopeURL_ is not the file system root, 1229> 1. Set _scopeURL_ to the parent URL of _scopeURL_. 1230> 1. If _scopeURL_ ends in a _"node_modules"_ path segment, return **null**. 1231> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_scopeURL_). 1232> 1. If _pjson_ is not **null**, then 1233> 1. Return _pjson_. 1234> 1. Return **null**. 1235 1236**READ_PACKAGE_JSON**(_packageURL_) 1237 1238> 1. Let _pjsonURL_ be the resolution of _"package.json"_ within _packageURL_. 1239> 1. If the file at _pjsonURL_ does not exist, then 1240> 1. Return **null**. 1241> 1. If the file at _packageURL_ does not parse as valid JSON, then 1242> 1. Throw an _Invalid Package Configuration_ error. 1243> 1. Return the parsed JSON source of the file at _pjsonURL_. 1244 1245### Customizing ESM specifier resolution algorithm 1246 1247The current specifier resolution does not support all default behavior of 1248the CommonJS loader. One of the behavior differences is automatic resolution 1249of file extensions and the ability to import directories that have an index 1250file. 1251 1252The `--experimental-specifier-resolution=[mode]` flag can be used to customize 1253the extension resolution algorithm. The default mode is `explicit`, which 1254requires the full path to a module be provided to the loader. To enable the 1255automatic extension resolution and importing from directories that include an 1256index file use the `node` mode. 1257 1258```console 1259$ node index.mjs 1260success! 1261$ node index # Failure! 1262Error: Cannot find module 1263$ node --experimental-specifier-resolution=node index 1264success! 1265``` 1266 1267<!-- Note: The cjs-module-lexer link should be kept in-sync with the deps version --> 1268[CommonJS]: modules.html 1269[Conditional exports]: packages.html#packages_conditional_exports 1270[Dynamic `import()`]: https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports 1271[ECMAScript-modules implementation]: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md 1272[ES Module Integration Proposal for Web Assembly]: https://github.com/webassembly/esm-integration 1273[Node.js EP for ES Modules]: https://github.com/nodejs/node-eps/blob/master/002-es-modules.md 1274[Terminology]: #esm_terminology 1275[WHATWG JSON modules specification]: https://html.spec.whatwg.org/#creating-a-json-module-script 1276[`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs 1277[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export 1278[`import()`]: #esm_import_expressions 1279[`import.meta.url`]: #esm_import_meta 1280[`import`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import 1281[`module.createRequire()`]: module.html#module_module_createrequire_filename 1282[`module.syncBuiltinESMExports()`]: module.html#module_module_syncbuiltinesmexports 1283[`transformSource` hook]: #esm_transformsource_source_context_defaulttransformsource 1284[`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer 1285[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer 1286[`string`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String 1287[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 1288[`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array 1289[dynamic instantiate hook]: #esm_code_dynamicinstantiate_code_hook 1290[`util.TextDecoder`]: util.md#util_class_util_textdecoder 1291[cjs-module-lexer]: https://github.com/guybedford/cjs-module-lexer/tree/1.0.0 1292[special scheme]: https://url.spec.whatwg.org/#special-scheme 1293[the official standard format]: https://tc39.github.io/ecma262/#sec-modules 1294[transpiler loader example]: #esm_transpiler_loader 1295[6.1.7 Array Index]: https://tc39.es/ecma262/#integer-index 1296[Top-Level Await]: https://github.com/tc39/proposal-top-level-await 1297[Core modules]: modules.html#modules_core_modules 1298[`package.json`]: packages.html#packages_node_js_package_json_field_definitions 1299[`"exports"`]: packages.html#packages_exports 1300[`"type"`]: packages.html#packages_type 1301