11cb0ef41Sopenharmony_ci# Modules: ECMAScript modules 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci<!--introduced_in=v8.5.0--> 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci<!-- type=misc --> 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci<!-- YAML 81cb0ef41Sopenharmony_ciadded: v8.5.0 91cb0ef41Sopenharmony_cichanges: 101cb0ef41Sopenharmony_ci - version: v18.20.0 111cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/50140 121cb0ef41Sopenharmony_ci description: Add experimental support for import attributes. 131cb0ef41Sopenharmony_ci - version: v18.19.0 141cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/44710 151cb0ef41Sopenharmony_ci description: Module customization hooks are executed off the main thread. 161cb0ef41Sopenharmony_ci - version: 171cb0ef41Sopenharmony_ci - v18.6.0 181cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42623 191cb0ef41Sopenharmony_ci description: Add support for chaining module customization hooks. 201cb0ef41Sopenharmony_ci - version: 211cb0ef41Sopenharmony_ci - v17.1.0 221cb0ef41Sopenharmony_ci - v16.14.0 231cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/40250 241cb0ef41Sopenharmony_ci description: Add experimental support for import assertions. 251cb0ef41Sopenharmony_ci - version: 261cb0ef41Sopenharmony_ci - v17.0.0 271cb0ef41Sopenharmony_ci - v16.12.0 281cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/37468 291cb0ef41Sopenharmony_ci description: 301cb0ef41Sopenharmony_ci Consolidate customization hooks, removed `getFormat`, `getSource`, 311cb0ef41Sopenharmony_ci `transformSource`, and `getGlobalPreloadCode` hooks 321cb0ef41Sopenharmony_ci added `load` and `globalPreload` hooks 331cb0ef41Sopenharmony_ci allowed returning `format` from either `resolve` or `load` hooks. 341cb0ef41Sopenharmony_ci - version: 351cb0ef41Sopenharmony_ci - v15.3.0 361cb0ef41Sopenharmony_ci - v14.17.0 371cb0ef41Sopenharmony_ci - v12.22.0 381cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35781 391cb0ef41Sopenharmony_ci description: Stabilize modules implementation. 401cb0ef41Sopenharmony_ci - version: 411cb0ef41Sopenharmony_ci - v14.13.0 421cb0ef41Sopenharmony_ci - v12.20.0 431cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35249 441cb0ef41Sopenharmony_ci description: Support for detection of CommonJS named exports. 451cb0ef41Sopenharmony_ci - version: v14.8.0 461cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/34558 471cb0ef41Sopenharmony_ci description: Unflag Top-Level Await. 481cb0ef41Sopenharmony_ci - version: 491cb0ef41Sopenharmony_ci - v14.0.0 501cb0ef41Sopenharmony_ci - v13.14.0 511cb0ef41Sopenharmony_ci - v12.20.0 521cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/31974 531cb0ef41Sopenharmony_ci description: Remove experimental modules warning. 541cb0ef41Sopenharmony_ci - version: 551cb0ef41Sopenharmony_ci - v13.2.0 561cb0ef41Sopenharmony_ci - v12.17.0 571cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/29866 581cb0ef41Sopenharmony_ci description: Loading ECMAScript modules no longer requires a command-line flag. 591cb0ef41Sopenharmony_ci - version: v12.0.0 601cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26745 611cb0ef41Sopenharmony_ci description: 621cb0ef41Sopenharmony_ci Add support for ES modules using `.js` file extension via `package.json` 631cb0ef41Sopenharmony_ci `"type"` field. 641cb0ef41Sopenharmony_ci--> 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci> Stability: 2 - Stable 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci## Introduction 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci<!--name=esm--> 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ciECMAScript modules are [the official standard format][] to package JavaScript 731cb0ef41Sopenharmony_cicode for reuse. Modules are defined using a variety of [`import`][] and 741cb0ef41Sopenharmony_ci[`export`][] statements. 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ciThe following example of an ES module exports a function: 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci```js 791cb0ef41Sopenharmony_ci// addTwo.mjs 801cb0ef41Sopenharmony_cifunction addTwo(num) { 811cb0ef41Sopenharmony_ci return num + 2; 821cb0ef41Sopenharmony_ci} 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ciexport { addTwo }; 851cb0ef41Sopenharmony_ci``` 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ciThe following example of an ES module imports the function from `addTwo.mjs`: 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci```js 901cb0ef41Sopenharmony_ci// app.mjs 911cb0ef41Sopenharmony_ciimport { addTwo } from './addTwo.mjs'; 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ci// Prints: 6 941cb0ef41Sopenharmony_ciconsole.log(addTwo(4)); 951cb0ef41Sopenharmony_ci``` 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ciNode.js fully supports ECMAScript modules as they are currently specified and 981cb0ef41Sopenharmony_ciprovides interoperability between them and its original module format, 991cb0ef41Sopenharmony_ci[CommonJS][]. 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci<!-- Anchors to make sure old links find a target --> 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci<i id="esm_package_json_type_field"></i><i id="esm_package_scope_and_file_extensions"></i><i id="esm_input_type_flag"></i> 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci## Enabling 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci<!-- type=misc --> 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ciNode.js has two module systems: [CommonJS][] modules and ECMAScript modules. 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ciAuthors can tell Node.js to use the ECMAScript modules loader via the `.mjs` 1121cb0ef41Sopenharmony_cifile extension, the `package.json` [`"type"`][] field, the [`--input-type`][] 1131cb0ef41Sopenharmony_ciflag, or the [`--experimental-default-type`][] flag. Outside of those cases, 1141cb0ef41Sopenharmony_ciNode.js will use the CommonJS module loader. See [Determining module system][] 1151cb0ef41Sopenharmony_cifor more details. 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci<!-- Anchors to make sure old links find a target --> 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci<i id="esm_package_entry_points"></i><i id="esm_main_entry_point_export"></i><i id="esm_subpath_exports"></i><i id="esm_package_exports_fallbacks"></i><i id="esm_exports_sugar"></i><i id="esm_conditional_exports"></i><i id="esm_nested_conditions"></i><i id="esm_self_referencing_a_package_using_its_name"></i><i id="esm_internal_package_imports"></i><i id="esm_dual_commonjs_es_module_packages"></i><i id="esm_dual_package_hazard"></i><i id="esm_writing_dual_packages_while_avoiding_or_minimizing_hazards"></i><i id="esm_approach_1_use_an_es_module_wrapper"></i><i id="esm_approach_2_isolate_state"></i> 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci## Packages 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ciThis section was moved to [Modules: Packages](packages.md). 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci## `import` Specifiers 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci### Terminology 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ciThe _specifier_ of an `import` statement is the string after the `from` keyword, 1301cb0ef41Sopenharmony_cie.g. `'node:path'` in `import { sep } from 'node:path'`. Specifiers are also 1311cb0ef41Sopenharmony_ciused in `export from` statements, and as the argument to an `import()` 1321cb0ef41Sopenharmony_ciexpression. 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ciThere are three types of specifiers: 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci* _Relative specifiers_ like `'./startup.js'` or `'../config.mjs'`. They refer 1371cb0ef41Sopenharmony_ci to a path relative to the location of the importing file. _The file extension 1381cb0ef41Sopenharmony_ci is always necessary for these._ 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci* _Bare specifiers_ like `'some-package'` or `'some-package/shuffle'`. They can 1411cb0ef41Sopenharmony_ci refer to the main entry point of a package by the package name, or a 1421cb0ef41Sopenharmony_ci specific feature module within a package prefixed by the package name as per 1431cb0ef41Sopenharmony_ci the examples respectively. _Including the file extension is only necessary 1441cb0ef41Sopenharmony_ci for packages without an [`"exports"`][] field._ 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci* _Absolute specifiers_ like `'file:///opt/nodejs/config.js'`. They refer 1471cb0ef41Sopenharmony_ci directly and explicitly to a full path. 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ciBare specifier resolutions are handled by the [Node.js module 1501cb0ef41Sopenharmony_ciresolution and loading algorithm][]. 1511cb0ef41Sopenharmony_ciAll other specifier resolutions are always only resolved with 1521cb0ef41Sopenharmony_cithe standard relative [URL][] resolution semantics. 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ciLike in CommonJS, module files within packages can be accessed by appending a 1551cb0ef41Sopenharmony_cipath to the package name unless the package's [`package.json`][] contains an 1561cb0ef41Sopenharmony_ci[`"exports"`][] field, in which case files within packages can only be accessed 1571cb0ef41Sopenharmony_civia the paths defined in [`"exports"`][]. 1581cb0ef41Sopenharmony_ci 1591cb0ef41Sopenharmony_ciFor details on these package resolution rules that apply to bare specifiers in 1601cb0ef41Sopenharmony_cithe Node.js module resolution, see the [packages documentation](packages.md). 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ci### Mandatory file extensions 1631cb0ef41Sopenharmony_ci 1641cb0ef41Sopenharmony_ciA file extension must be provided when using the `import` keyword to resolve 1651cb0ef41Sopenharmony_cirelative or absolute specifiers. Directory indexes (e.g. `'./startup/index.js'`) 1661cb0ef41Sopenharmony_cimust also be fully specified. 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ciThis behavior matches how `import` behaves in browser environments, assuming a 1691cb0ef41Sopenharmony_citypically configured server. 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ci### URLs 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ciES modules are resolved and cached as URLs. This means that special characters 1741cb0ef41Sopenharmony_cimust be [percent-encoded][], such as `#` with `%23` and `?` with `%3F`. 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci`file:`, `node:`, and `data:` URL schemes are supported. A specifier like 1771cb0ef41Sopenharmony_ci`'https://example.com/app.js'` is not supported natively in Node.js unless using 1781cb0ef41Sopenharmony_cia [custom HTTPS loader][]. 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci#### `file:` URLs 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ciModules are loaded multiple times if the `import` specifier used to resolve 1831cb0ef41Sopenharmony_cithem has a different query or fragment. 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci```js 1861cb0ef41Sopenharmony_ciimport './foo.mjs?query=1'; // loads ./foo.mjs with query of "?query=1" 1871cb0ef41Sopenharmony_ciimport './foo.mjs?query=2'; // loads ./foo.mjs with query of "?query=2" 1881cb0ef41Sopenharmony_ci``` 1891cb0ef41Sopenharmony_ci 1901cb0ef41Sopenharmony_ciThe volume root may be referenced via `/`, `//`, or `file:///`. Given the 1911cb0ef41Sopenharmony_cidifferences between [URL][] and path resolution (such as percent encoding 1921cb0ef41Sopenharmony_cidetails), it is recommended to use [url.pathToFileURL][] when importing a path. 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ci#### `data:` imports 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_ci<!-- YAML 1971cb0ef41Sopenharmony_ciadded: v12.10.0 1981cb0ef41Sopenharmony_ci--> 1991cb0ef41Sopenharmony_ci 2001cb0ef41Sopenharmony_ci[`data:` URLs][] are supported for importing with the following MIME types: 2011cb0ef41Sopenharmony_ci 2021cb0ef41Sopenharmony_ci* `text/javascript` for ES modules 2031cb0ef41Sopenharmony_ci* `application/json` for JSON 2041cb0ef41Sopenharmony_ci* `application/wasm` for Wasm 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ci```js 2071cb0ef41Sopenharmony_ciimport 'data:text/javascript,console.log("hello!");'; 2081cb0ef41Sopenharmony_ciimport _ from 'data:application/json,"world!"' with { type: 'json' }; 2091cb0ef41Sopenharmony_ci``` 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ci`data:` URLs only resolve [bare specifiers][Terminology] for builtin modules 2121cb0ef41Sopenharmony_ciand [absolute specifiers][Terminology]. Resolving 2131cb0ef41Sopenharmony_ci[relative specifiers][Terminology] does not work because `data:` is not a 2141cb0ef41Sopenharmony_ci[special scheme][]. For example, attempting to load `./foo` 2151cb0ef41Sopenharmony_cifrom `data:text/javascript,import "./foo";` fails to resolve because there 2161cb0ef41Sopenharmony_ciis no concept of relative resolution for `data:` URLs. 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci#### `node:` imports 2191cb0ef41Sopenharmony_ci 2201cb0ef41Sopenharmony_ci<!-- YAML 2211cb0ef41Sopenharmony_ciadded: 2221cb0ef41Sopenharmony_ci - v14.13.1 2231cb0ef41Sopenharmony_ci - v12.20.0 2241cb0ef41Sopenharmony_cichanges: 2251cb0ef41Sopenharmony_ci - version: 2261cb0ef41Sopenharmony_ci - v16.0.0 2271cb0ef41Sopenharmony_ci - v14.18.0 2281cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/37246 2291cb0ef41Sopenharmony_ci description: Added `node:` import support to `require(...)`. 2301cb0ef41Sopenharmony_ci--> 2311cb0ef41Sopenharmony_ci 2321cb0ef41Sopenharmony_ci`node:` URLs are supported as an alternative means to load Node.js builtin 2331cb0ef41Sopenharmony_cimodules. This URL scheme allows for builtin modules to be referenced by valid 2341cb0ef41Sopenharmony_ciabsolute URL strings. 2351cb0ef41Sopenharmony_ci 2361cb0ef41Sopenharmony_ci```js 2371cb0ef41Sopenharmony_ciimport fs from 'node:fs/promises'; 2381cb0ef41Sopenharmony_ci``` 2391cb0ef41Sopenharmony_ci 2401cb0ef41Sopenharmony_ci<a id="import-assertions"></a> 2411cb0ef41Sopenharmony_ci 2421cb0ef41Sopenharmony_ci## Import attributes 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_ci<!-- YAML 2451cb0ef41Sopenharmony_ciadded: 2461cb0ef41Sopenharmony_ci - v17.1.0 2471cb0ef41Sopenharmony_ci - v16.14.0 2481cb0ef41Sopenharmony_cichanges: 2491cb0ef41Sopenharmony_ci - version: v18.20.0 2501cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/50140 2511cb0ef41Sopenharmony_ci description: Switch from Import Assertions to Import Attributes. 2521cb0ef41Sopenharmony_ci--> 2531cb0ef41Sopenharmony_ci 2541cb0ef41Sopenharmony_ci> Stability: 1.1 - Active development 2551cb0ef41Sopenharmony_ci 2561cb0ef41Sopenharmony_ci> This feature was previously named "Import assertions", and using the `assert` 2571cb0ef41Sopenharmony_ci> keyword instead of `with`. Any uses in code of the prior `assert` keyword 2581cb0ef41Sopenharmony_ci> should be updated to use `with` instead. 2591cb0ef41Sopenharmony_ci 2601cb0ef41Sopenharmony_ciThe [Import Attributes proposal][] adds an inline syntax for module import 2611cb0ef41Sopenharmony_cistatements to pass on more information alongside the module specifier. 2621cb0ef41Sopenharmony_ci 2631cb0ef41Sopenharmony_ci```js 2641cb0ef41Sopenharmony_ciimport fooData from './foo.json' with { type: 'json' }; 2651cb0ef41Sopenharmony_ci 2661cb0ef41Sopenharmony_ciconst { default: barData } = 2671cb0ef41Sopenharmony_ci await import('./bar.json', { with: { type: 'json' } }); 2681cb0ef41Sopenharmony_ci``` 2691cb0ef41Sopenharmony_ci 2701cb0ef41Sopenharmony_ciNode.js supports the following `type` values, for which the attribute is 2711cb0ef41Sopenharmony_cimandatory: 2721cb0ef41Sopenharmony_ci 2731cb0ef41Sopenharmony_ci| Attribute `type` | Needed for | 2741cb0ef41Sopenharmony_ci| ---------------- | ---------------- | 2751cb0ef41Sopenharmony_ci| `'json'` | [JSON modules][] | 2761cb0ef41Sopenharmony_ci 2771cb0ef41Sopenharmony_ci## Builtin modules 2781cb0ef41Sopenharmony_ci 2791cb0ef41Sopenharmony_ci[Core modules][] provide named exports of their public API. A 2801cb0ef41Sopenharmony_cidefault export is also provided which is the value of the CommonJS exports. 2811cb0ef41Sopenharmony_ciThe default export can be used for, among other things, modifying the named 2821cb0ef41Sopenharmony_ciexports. Named exports of builtin modules are updated only by calling 2831cb0ef41Sopenharmony_ci[`module.syncBuiltinESMExports()`][]. 2841cb0ef41Sopenharmony_ci 2851cb0ef41Sopenharmony_ci```js 2861cb0ef41Sopenharmony_ciimport EventEmitter from 'node:events'; 2871cb0ef41Sopenharmony_ciconst e = new EventEmitter(); 2881cb0ef41Sopenharmony_ci``` 2891cb0ef41Sopenharmony_ci 2901cb0ef41Sopenharmony_ci```js 2911cb0ef41Sopenharmony_ciimport { readFile } from 'node:fs'; 2921cb0ef41Sopenharmony_cireadFile('./foo.txt', (err, source) => { 2931cb0ef41Sopenharmony_ci if (err) { 2941cb0ef41Sopenharmony_ci console.error(err); 2951cb0ef41Sopenharmony_ci } else { 2961cb0ef41Sopenharmony_ci console.log(source); 2971cb0ef41Sopenharmony_ci } 2981cb0ef41Sopenharmony_ci}); 2991cb0ef41Sopenharmony_ci``` 3001cb0ef41Sopenharmony_ci 3011cb0ef41Sopenharmony_ci```js 3021cb0ef41Sopenharmony_ciimport fs, { readFileSync } from 'node:fs'; 3031cb0ef41Sopenharmony_ciimport { syncBuiltinESMExports } from 'node:module'; 3041cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 3051cb0ef41Sopenharmony_ci 3061cb0ef41Sopenharmony_cifs.readFileSync = () => Buffer.from('Hello, ESM'); 3071cb0ef41Sopenharmony_cisyncBuiltinESMExports(); 3081cb0ef41Sopenharmony_ci 3091cb0ef41Sopenharmony_cifs.readFileSync === readFileSync; 3101cb0ef41Sopenharmony_ci``` 3111cb0ef41Sopenharmony_ci 3121cb0ef41Sopenharmony_ci## `import()` expressions 3131cb0ef41Sopenharmony_ci 3141cb0ef41Sopenharmony_ci[Dynamic `import()`][] is supported in both CommonJS and ES modules. In CommonJS 3151cb0ef41Sopenharmony_cimodules it can be used to load ES modules. 3161cb0ef41Sopenharmony_ci 3171cb0ef41Sopenharmony_ci## `import.meta` 3181cb0ef41Sopenharmony_ci 3191cb0ef41Sopenharmony_ci* {Object} 3201cb0ef41Sopenharmony_ci 3211cb0ef41Sopenharmony_ciThe `import.meta` meta property is an `Object` that contains the following 3221cb0ef41Sopenharmony_ciproperties. 3231cb0ef41Sopenharmony_ci 3241cb0ef41Sopenharmony_ci### `import.meta.url` 3251cb0ef41Sopenharmony_ci 3261cb0ef41Sopenharmony_ci* {string} The absolute `file:` URL of the module. 3271cb0ef41Sopenharmony_ci 3281cb0ef41Sopenharmony_ciThis is defined exactly the same as it is in browsers providing the URL of the 3291cb0ef41Sopenharmony_cicurrent module file. 3301cb0ef41Sopenharmony_ci 3311cb0ef41Sopenharmony_ciThis enables useful patterns such as relative file loading: 3321cb0ef41Sopenharmony_ci 3331cb0ef41Sopenharmony_ci```js 3341cb0ef41Sopenharmony_ciimport { readFileSync } from 'node:fs'; 3351cb0ef41Sopenharmony_ciconst buffer = readFileSync(new URL('./data.proto', import.meta.url)); 3361cb0ef41Sopenharmony_ci``` 3371cb0ef41Sopenharmony_ci 3381cb0ef41Sopenharmony_ci### `import.meta.resolve(specifier)` 3391cb0ef41Sopenharmony_ci 3401cb0ef41Sopenharmony_ci<!-- YAML 3411cb0ef41Sopenharmony_ciadded: 3421cb0ef41Sopenharmony_ci - v13.9.0 3431cb0ef41Sopenharmony_ci - v12.16.2 3441cb0ef41Sopenharmony_cichanges: 3451cb0ef41Sopenharmony_ci - version: v18.19.0 3461cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/49028 3471cb0ef41Sopenharmony_ci description: Unflag `import.meta.resolve``, with `parentURL` parameter still 3481cb0ef41Sopenharmony_ci flagged. 3491cb0ef41Sopenharmony_ci - version: v18.19.0 3501cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/49038 3511cb0ef41Sopenharmony_ci description: This API no longer throws when targeting `file:` URLs that do 3521cb0ef41Sopenharmony_ci not map to an existing file on the local FS. 3531cb0ef41Sopenharmony_ci - version: v18.19.0 3541cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/44710 3551cb0ef41Sopenharmony_ci description: This API now returns a string synchronously instead of a Promise. 3561cb0ef41Sopenharmony_ci - version: 3571cb0ef41Sopenharmony_ci - v16.2.0 3581cb0ef41Sopenharmony_ci - v14.18.0 3591cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/38587 3601cb0ef41Sopenharmony_ci description: Add support for WHATWG `URL` object to `parentURL` parameter. 3611cb0ef41Sopenharmony_ci--> 3621cb0ef41Sopenharmony_ci 3631cb0ef41Sopenharmony_ci> Stability: 1.2 - Release candidate 3641cb0ef41Sopenharmony_ci 3651cb0ef41Sopenharmony_ci* `specifier` {string} The module specifier to resolve relative to the 3661cb0ef41Sopenharmony_ci current module. 3671cb0ef41Sopenharmony_ci* Returns: {string} The absolute URL string that the specifier would resolve to. 3681cb0ef41Sopenharmony_ci 3691cb0ef41Sopenharmony_ci[`import.meta.resolve`][] is a module-relative resolution function scoped to 3701cb0ef41Sopenharmony_cieach module, returning the URL string. 3711cb0ef41Sopenharmony_ci 3721cb0ef41Sopenharmony_ci```js 3731cb0ef41Sopenharmony_ciconst dependencyAsset = import.meta.resolve('component-lib/asset.css'); 3741cb0ef41Sopenharmony_ci// file:///app/node_modules/component-lib/asset.css 3751cb0ef41Sopenharmony_ciimport.meta.resolve('./dep.js'); 3761cb0ef41Sopenharmony_ci// file:///app/dep.js 3771cb0ef41Sopenharmony_ci``` 3781cb0ef41Sopenharmony_ci 3791cb0ef41Sopenharmony_ciAll features of the Node.js module resolution are supported. Dependency 3801cb0ef41Sopenharmony_ciresolutions are subject to the permitted exports resolutions within the package. 3811cb0ef41Sopenharmony_ci 3821cb0ef41Sopenharmony_ci**Caveats**: 3831cb0ef41Sopenharmony_ci 3841cb0ef41Sopenharmony_ci* This can result in synchronous file-system operations, which 3851cb0ef41Sopenharmony_ci can impact performance similarly to `require.resolve`. 3861cb0ef41Sopenharmony_ci* This feature is not available within custom loaders (it would 3871cb0ef41Sopenharmony_ci create a deadlock). 3881cb0ef41Sopenharmony_ci 3891cb0ef41Sopenharmony_ci**Non-standard API**: 3901cb0ef41Sopenharmony_ci 3911cb0ef41Sopenharmony_ciWhen using the `--experimental-import-meta-resolve` flag, that function accepts 3921cb0ef41Sopenharmony_cia second argument: 3931cb0ef41Sopenharmony_ci 3941cb0ef41Sopenharmony_ci* `parent` {string|URL} An optional absolute parent module URL to resolve from. 3951cb0ef41Sopenharmony_ci **Default:** `import.meta.url` 3961cb0ef41Sopenharmony_ci 3971cb0ef41Sopenharmony_ci## Interoperability with CommonJS 3981cb0ef41Sopenharmony_ci 3991cb0ef41Sopenharmony_ci### `import` statements 4001cb0ef41Sopenharmony_ci 4011cb0ef41Sopenharmony_ciAn `import` statement can reference an ES module or a CommonJS module. 4021cb0ef41Sopenharmony_ci`import` statements are permitted only in ES modules, but dynamic [`import()`][] 4031cb0ef41Sopenharmony_ciexpressions are supported in CommonJS for loading ES modules. 4041cb0ef41Sopenharmony_ci 4051cb0ef41Sopenharmony_ciWhen importing [CommonJS modules](#commonjs-namespaces), the 4061cb0ef41Sopenharmony_ci`module.exports` object is provided as the default export. Named exports may be 4071cb0ef41Sopenharmony_ciavailable, provided by static analysis as a convenience for better ecosystem 4081cb0ef41Sopenharmony_cicompatibility. 4091cb0ef41Sopenharmony_ci 4101cb0ef41Sopenharmony_ci### `require` 4111cb0ef41Sopenharmony_ci 4121cb0ef41Sopenharmony_ciThe CommonJS module `require` always treats the files it references as CommonJS. 4131cb0ef41Sopenharmony_ci 4141cb0ef41Sopenharmony_ciUsing `require` to load an ES module is not supported because ES modules have 4151cb0ef41Sopenharmony_ciasynchronous execution. Instead, use [`import()`][] to load an ES module 4161cb0ef41Sopenharmony_cifrom a CommonJS module. 4171cb0ef41Sopenharmony_ci 4181cb0ef41Sopenharmony_ci### CommonJS Namespaces 4191cb0ef41Sopenharmony_ci 4201cb0ef41Sopenharmony_ciCommonJS modules consist of a `module.exports` object which can be of any type. 4211cb0ef41Sopenharmony_ci 4221cb0ef41Sopenharmony_ciWhen importing a CommonJS module, it can be reliably imported using the ES 4231cb0ef41Sopenharmony_cimodule default import or its corresponding sugar syntax: 4241cb0ef41Sopenharmony_ci 4251cb0ef41Sopenharmony_ci<!-- eslint-disable no-duplicate-imports --> 4261cb0ef41Sopenharmony_ci 4271cb0ef41Sopenharmony_ci```js 4281cb0ef41Sopenharmony_ciimport { default as cjs } from 'cjs'; 4291cb0ef41Sopenharmony_ci 4301cb0ef41Sopenharmony_ci// The following import statement is "syntax sugar" (equivalent but sweeter) 4311cb0ef41Sopenharmony_ci// for `{ default as cjsSugar }` in the above import statement: 4321cb0ef41Sopenharmony_ciimport cjsSugar from 'cjs'; 4331cb0ef41Sopenharmony_ci 4341cb0ef41Sopenharmony_ciconsole.log(cjs); 4351cb0ef41Sopenharmony_ciconsole.log(cjs === cjsSugar); 4361cb0ef41Sopenharmony_ci// Prints: 4371cb0ef41Sopenharmony_ci// <module.exports> 4381cb0ef41Sopenharmony_ci// true 4391cb0ef41Sopenharmony_ci``` 4401cb0ef41Sopenharmony_ci 4411cb0ef41Sopenharmony_ciThe ECMAScript Module Namespace representation of a CommonJS module is always 4421cb0ef41Sopenharmony_cia namespace with a `default` export key pointing to the CommonJS 4431cb0ef41Sopenharmony_ci`module.exports` value. 4441cb0ef41Sopenharmony_ci 4451cb0ef41Sopenharmony_ciThis Module Namespace Exotic Object can be directly observed either when using 4461cb0ef41Sopenharmony_ci`import * as m from 'cjs'` or a dynamic import: 4471cb0ef41Sopenharmony_ci 4481cb0ef41Sopenharmony_ci<!-- eslint-skip --> 4491cb0ef41Sopenharmony_ci 4501cb0ef41Sopenharmony_ci```js 4511cb0ef41Sopenharmony_ciimport * as m from 'cjs'; 4521cb0ef41Sopenharmony_ciconsole.log(m); 4531cb0ef41Sopenharmony_ciconsole.log(m === await import('cjs')); 4541cb0ef41Sopenharmony_ci// Prints: 4551cb0ef41Sopenharmony_ci// [Module] { default: <module.exports> } 4561cb0ef41Sopenharmony_ci// true 4571cb0ef41Sopenharmony_ci``` 4581cb0ef41Sopenharmony_ci 4591cb0ef41Sopenharmony_ciFor better compatibility with existing usage in the JS ecosystem, Node.js 4601cb0ef41Sopenharmony_ciin addition attempts to determine the CommonJS named exports of every imported 4611cb0ef41Sopenharmony_ciCommonJS module to provide them as separate ES module exports using a static 4621cb0ef41Sopenharmony_cianalysis process. 4631cb0ef41Sopenharmony_ci 4641cb0ef41Sopenharmony_ciFor example, consider a CommonJS module written: 4651cb0ef41Sopenharmony_ci 4661cb0ef41Sopenharmony_ci```cjs 4671cb0ef41Sopenharmony_ci// cjs.cjs 4681cb0ef41Sopenharmony_ciexports.name = 'exported'; 4691cb0ef41Sopenharmony_ci``` 4701cb0ef41Sopenharmony_ci 4711cb0ef41Sopenharmony_ciThe preceding module supports named imports in ES modules: 4721cb0ef41Sopenharmony_ci 4731cb0ef41Sopenharmony_ci<!-- eslint-disable no-duplicate-imports --> 4741cb0ef41Sopenharmony_ci 4751cb0ef41Sopenharmony_ci```js 4761cb0ef41Sopenharmony_ciimport { name } from './cjs.cjs'; 4771cb0ef41Sopenharmony_ciconsole.log(name); 4781cb0ef41Sopenharmony_ci// Prints: 'exported' 4791cb0ef41Sopenharmony_ci 4801cb0ef41Sopenharmony_ciimport cjs from './cjs.cjs'; 4811cb0ef41Sopenharmony_ciconsole.log(cjs); 4821cb0ef41Sopenharmony_ci// Prints: { name: 'exported' } 4831cb0ef41Sopenharmony_ci 4841cb0ef41Sopenharmony_ciimport * as m from './cjs.cjs'; 4851cb0ef41Sopenharmony_ciconsole.log(m); 4861cb0ef41Sopenharmony_ci// Prints: [Module] { default: { name: 'exported' }, name: 'exported' } 4871cb0ef41Sopenharmony_ci``` 4881cb0ef41Sopenharmony_ci 4891cb0ef41Sopenharmony_ciAs can be seen from the last example of the Module Namespace Exotic Object being 4901cb0ef41Sopenharmony_cilogged, the `name` export is copied off of the `module.exports` object and set 4911cb0ef41Sopenharmony_cidirectly on the ES module namespace when the module is imported. 4921cb0ef41Sopenharmony_ci 4931cb0ef41Sopenharmony_ciLive binding updates or new exports added to `module.exports` are not detected 4941cb0ef41Sopenharmony_cifor these named exports. 4951cb0ef41Sopenharmony_ci 4961cb0ef41Sopenharmony_ciThe detection of named exports is based on common syntax patterns but does not 4971cb0ef41Sopenharmony_cialways correctly detect named exports. In these cases, using the default 4981cb0ef41Sopenharmony_ciimport form described above can be a better option. 4991cb0ef41Sopenharmony_ci 5001cb0ef41Sopenharmony_ciNamed exports detection covers many common export patterns, reexport patterns 5011cb0ef41Sopenharmony_ciand build tool and transpiler outputs. See [cjs-module-lexer][] for the exact 5021cb0ef41Sopenharmony_cisemantics implemented. 5031cb0ef41Sopenharmony_ci 5041cb0ef41Sopenharmony_ci### Differences between ES modules and CommonJS 5051cb0ef41Sopenharmony_ci 5061cb0ef41Sopenharmony_ci#### No `require`, `exports`, or `module.exports` 5071cb0ef41Sopenharmony_ci 5081cb0ef41Sopenharmony_ciIn most cases, the ES module `import` can be used to load CommonJS modules. 5091cb0ef41Sopenharmony_ci 5101cb0ef41Sopenharmony_ciIf needed, a `require` function can be constructed within an ES module using 5111cb0ef41Sopenharmony_ci[`module.createRequire()`][]. 5121cb0ef41Sopenharmony_ci 5131cb0ef41Sopenharmony_ci#### No `__filename` or `__dirname` 5141cb0ef41Sopenharmony_ci 5151cb0ef41Sopenharmony_ciThese CommonJS variables are not available in ES modules. 5161cb0ef41Sopenharmony_ci 5171cb0ef41Sopenharmony_ci`__filename` and `__dirname` use cases can be replicated via 5181cb0ef41Sopenharmony_ci[`import.meta.url`][]. 5191cb0ef41Sopenharmony_ci 5201cb0ef41Sopenharmony_ci#### No Addon Loading 5211cb0ef41Sopenharmony_ci 5221cb0ef41Sopenharmony_ci[Addons][] are not currently supported with ES module imports. 5231cb0ef41Sopenharmony_ci 5241cb0ef41Sopenharmony_ciThey can instead be loaded with [`module.createRequire()`][] or 5251cb0ef41Sopenharmony_ci[`process.dlopen`][]. 5261cb0ef41Sopenharmony_ci 5271cb0ef41Sopenharmony_ci#### No `require.resolve` 5281cb0ef41Sopenharmony_ci 5291cb0ef41Sopenharmony_ciRelative resolution can be handled via `new URL('./local', import.meta.url)`. 5301cb0ef41Sopenharmony_ci 5311cb0ef41Sopenharmony_ciFor a complete `require.resolve` replacement, there is the 5321cb0ef41Sopenharmony_ci[import.meta.resolve][] API. 5331cb0ef41Sopenharmony_ci 5341cb0ef41Sopenharmony_ciAlternatively `module.createRequire()` can be used. 5351cb0ef41Sopenharmony_ci 5361cb0ef41Sopenharmony_ci#### No `NODE_PATH` 5371cb0ef41Sopenharmony_ci 5381cb0ef41Sopenharmony_ci`NODE_PATH` is not part of resolving `import` specifiers. Please use symlinks 5391cb0ef41Sopenharmony_ciif this behavior is desired. 5401cb0ef41Sopenharmony_ci 5411cb0ef41Sopenharmony_ci#### No `require.extensions` 5421cb0ef41Sopenharmony_ci 5431cb0ef41Sopenharmony_ci`require.extensions` is not used by `import`. Module customization hooks can 5441cb0ef41Sopenharmony_ciprovide a replacement. 5451cb0ef41Sopenharmony_ci 5461cb0ef41Sopenharmony_ci#### No `require.cache` 5471cb0ef41Sopenharmony_ci 5481cb0ef41Sopenharmony_ci`require.cache` is not used by `import` as the ES module loader has its own 5491cb0ef41Sopenharmony_ciseparate cache. 5501cb0ef41Sopenharmony_ci 5511cb0ef41Sopenharmony_ci<i id="esm_experimental_json_modules"></i> 5521cb0ef41Sopenharmony_ci 5531cb0ef41Sopenharmony_ci## JSON modules 5541cb0ef41Sopenharmony_ci 5551cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 5561cb0ef41Sopenharmony_ci 5571cb0ef41Sopenharmony_ciJSON files can be referenced by `import`: 5581cb0ef41Sopenharmony_ci 5591cb0ef41Sopenharmony_ci```js 5601cb0ef41Sopenharmony_ciimport packageConfig from './package.json' with { type: 'json' }; 5611cb0ef41Sopenharmony_ci``` 5621cb0ef41Sopenharmony_ci 5631cb0ef41Sopenharmony_ciThe `with { type: 'json' }` syntax is mandatory; see [Import Attributes][]. 5641cb0ef41Sopenharmony_ci 5651cb0ef41Sopenharmony_ciThe imported JSON only exposes a `default` export. There is no support for named 5661cb0ef41Sopenharmony_ciexports. A cache entry is created in the CommonJS cache to avoid duplication. 5671cb0ef41Sopenharmony_ciThe same object is returned in CommonJS if the JSON module has already been 5681cb0ef41Sopenharmony_ciimported from the same path. 5691cb0ef41Sopenharmony_ci 5701cb0ef41Sopenharmony_ci<i id="esm_experimental_wasm_modules"></i> 5711cb0ef41Sopenharmony_ci 5721cb0ef41Sopenharmony_ci## Wasm modules 5731cb0ef41Sopenharmony_ci 5741cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 5751cb0ef41Sopenharmony_ci 5761cb0ef41Sopenharmony_ciImporting WebAssembly modules is supported under the 5771cb0ef41Sopenharmony_ci`--experimental-wasm-modules` flag, allowing any `.wasm` files to be 5781cb0ef41Sopenharmony_ciimported as normal modules while also supporting their module imports. 5791cb0ef41Sopenharmony_ci 5801cb0ef41Sopenharmony_ciThis integration is in line with the 5811cb0ef41Sopenharmony_ci[ES Module Integration Proposal for WebAssembly][]. 5821cb0ef41Sopenharmony_ci 5831cb0ef41Sopenharmony_ciFor example, an `index.mjs` containing: 5841cb0ef41Sopenharmony_ci 5851cb0ef41Sopenharmony_ci```js 5861cb0ef41Sopenharmony_ciimport * as M from './module.wasm'; 5871cb0ef41Sopenharmony_ciconsole.log(M); 5881cb0ef41Sopenharmony_ci``` 5891cb0ef41Sopenharmony_ci 5901cb0ef41Sopenharmony_ciexecuted under: 5911cb0ef41Sopenharmony_ci 5921cb0ef41Sopenharmony_ci```bash 5931cb0ef41Sopenharmony_cinode --experimental-wasm-modules index.mjs 5941cb0ef41Sopenharmony_ci``` 5951cb0ef41Sopenharmony_ci 5961cb0ef41Sopenharmony_ciwould provide the exports interface for the instantiation of `module.wasm`. 5971cb0ef41Sopenharmony_ci 5981cb0ef41Sopenharmony_ci<i id="esm_experimental_top_level_await"></i> 5991cb0ef41Sopenharmony_ci 6001cb0ef41Sopenharmony_ci## Top-level `await` 6011cb0ef41Sopenharmony_ci 6021cb0ef41Sopenharmony_ci<!-- YAML 6031cb0ef41Sopenharmony_ciadded: v14.8.0 6041cb0ef41Sopenharmony_ci--> 6051cb0ef41Sopenharmony_ci 6061cb0ef41Sopenharmony_ciThe `await` keyword may be used in the top level body of an ECMAScript module. 6071cb0ef41Sopenharmony_ci 6081cb0ef41Sopenharmony_ciAssuming an `a.mjs` with 6091cb0ef41Sopenharmony_ci 6101cb0ef41Sopenharmony_ci```js 6111cb0ef41Sopenharmony_ciexport const five = await Promise.resolve(5); 6121cb0ef41Sopenharmony_ci``` 6131cb0ef41Sopenharmony_ci 6141cb0ef41Sopenharmony_ciAnd a `b.mjs` with 6151cb0ef41Sopenharmony_ci 6161cb0ef41Sopenharmony_ci```js 6171cb0ef41Sopenharmony_ciimport { five } from './a.mjs'; 6181cb0ef41Sopenharmony_ci 6191cb0ef41Sopenharmony_ciconsole.log(five); // Logs `5` 6201cb0ef41Sopenharmony_ci``` 6211cb0ef41Sopenharmony_ci 6221cb0ef41Sopenharmony_ci```bash 6231cb0ef41Sopenharmony_cinode b.mjs # works 6241cb0ef41Sopenharmony_ci``` 6251cb0ef41Sopenharmony_ci 6261cb0ef41Sopenharmony_ciIf a top level `await` expression never resolves, the `node` process will exit 6271cb0ef41Sopenharmony_ciwith a `13` [status code][]. 6281cb0ef41Sopenharmony_ci 6291cb0ef41Sopenharmony_ci```js 6301cb0ef41Sopenharmony_ciimport { spawn } from 'node:child_process'; 6311cb0ef41Sopenharmony_ciimport { execPath } from 'node:process'; 6321cb0ef41Sopenharmony_ci 6331cb0ef41Sopenharmony_cispawn(execPath, [ 6341cb0ef41Sopenharmony_ci '--input-type=module', 6351cb0ef41Sopenharmony_ci '--eval', 6361cb0ef41Sopenharmony_ci // Never-resolving Promise: 6371cb0ef41Sopenharmony_ci 'await new Promise(() => {})', 6381cb0ef41Sopenharmony_ci]).once('exit', (code) => { 6391cb0ef41Sopenharmony_ci console.log(code); // Logs `13` 6401cb0ef41Sopenharmony_ci}); 6411cb0ef41Sopenharmony_ci``` 6421cb0ef41Sopenharmony_ci 6431cb0ef41Sopenharmony_ci## HTTPS and HTTP imports 6441cb0ef41Sopenharmony_ci 6451cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 6461cb0ef41Sopenharmony_ci 6471cb0ef41Sopenharmony_ciImporting network based modules using `https:` and `http:` is supported under 6481cb0ef41Sopenharmony_cithe `--experimental-network-imports` flag. This allows web browser-like imports 6491cb0ef41Sopenharmony_cito work in Node.js with a few differences due to application stability and 6501cb0ef41Sopenharmony_cisecurity concerns that are different when running in a privileged environment 6511cb0ef41Sopenharmony_ciinstead of a browser sandbox. 6521cb0ef41Sopenharmony_ci 6531cb0ef41Sopenharmony_ci### Imports are limited to HTTP/1 6541cb0ef41Sopenharmony_ci 6551cb0ef41Sopenharmony_ciAutomatic protocol negotiation for HTTP/2 and HTTP/3 is not yet supported. 6561cb0ef41Sopenharmony_ci 6571cb0ef41Sopenharmony_ci### HTTP is limited to loopback addresses 6581cb0ef41Sopenharmony_ci 6591cb0ef41Sopenharmony_ci`http:` is vulnerable to man-in-the-middle attacks and is not allowed to be 6601cb0ef41Sopenharmony_ciused for addresses outside of the IPv4 address `127.0.0.0/8` (`127.0.0.1` to 6611cb0ef41Sopenharmony_ci`127.255.255.255`) and the IPv6 address `::1`. Support for `http:` is intended 6621cb0ef41Sopenharmony_cito be used for local development. 6631cb0ef41Sopenharmony_ci 6641cb0ef41Sopenharmony_ci### Authentication is never sent to the destination server. 6651cb0ef41Sopenharmony_ci 6661cb0ef41Sopenharmony_ci`Authorization`, `Cookie`, and `Proxy-Authorization` headers are not sent to the 6671cb0ef41Sopenharmony_ciserver. Avoid including user info in parts of imported URLs. A security model 6681cb0ef41Sopenharmony_cifor safely using these on the server is being worked on. 6691cb0ef41Sopenharmony_ci 6701cb0ef41Sopenharmony_ci### CORS is never checked on the destination server 6711cb0ef41Sopenharmony_ci 6721cb0ef41Sopenharmony_ciCORS is designed to allow a server to limit the consumers of an API to a 6731cb0ef41Sopenharmony_cispecific set of hosts. This is not supported as it does not make sense for a 6741cb0ef41Sopenharmony_ciserver-based implementation. 6751cb0ef41Sopenharmony_ci 6761cb0ef41Sopenharmony_ci### Cannot load non-network dependencies 6771cb0ef41Sopenharmony_ci 6781cb0ef41Sopenharmony_ciThese modules cannot access other modules that are not over `http:` or `https:`. 6791cb0ef41Sopenharmony_ciTo still access local modules while avoiding the security concern, pass in 6801cb0ef41Sopenharmony_cireferences to the local dependencies: 6811cb0ef41Sopenharmony_ci 6821cb0ef41Sopenharmony_ci```mjs 6831cb0ef41Sopenharmony_ci// file.mjs 6841cb0ef41Sopenharmony_ciimport worker_threads from 'node:worker_threads'; 6851cb0ef41Sopenharmony_ciimport { configure, resize } from 'https://example.com/imagelib.mjs'; 6861cb0ef41Sopenharmony_ciconfigure({ worker_threads }); 6871cb0ef41Sopenharmony_ci``` 6881cb0ef41Sopenharmony_ci 6891cb0ef41Sopenharmony_ci```mjs 6901cb0ef41Sopenharmony_ci// https://example.com/imagelib.mjs 6911cb0ef41Sopenharmony_cilet worker_threads; 6921cb0ef41Sopenharmony_ciexport function configure(opts) { 6931cb0ef41Sopenharmony_ci worker_threads = opts.worker_threads; 6941cb0ef41Sopenharmony_ci} 6951cb0ef41Sopenharmony_ciexport function resize(img, size) { 6961cb0ef41Sopenharmony_ci // Perform resizing in worker_thread to avoid main thread blocking 6971cb0ef41Sopenharmony_ci} 6981cb0ef41Sopenharmony_ci``` 6991cb0ef41Sopenharmony_ci 7001cb0ef41Sopenharmony_ci### Network-based loading is not enabled by default 7011cb0ef41Sopenharmony_ci 7021cb0ef41Sopenharmony_ciFor now, the `--experimental-network-imports` flag is required to enable loading 7031cb0ef41Sopenharmony_ciresources over `http:` or `https:`. In the future, a different mechanism will be 7041cb0ef41Sopenharmony_ciused to enforce this. Opt-in is required to prevent transitive dependencies 7051cb0ef41Sopenharmony_ciinadvertently using potentially mutable state that could affect reliability 7061cb0ef41Sopenharmony_ciof Node.js applications. 7071cb0ef41Sopenharmony_ci 7081cb0ef41Sopenharmony_ci<i id="esm_experimental_loaders"></i> 7091cb0ef41Sopenharmony_ci 7101cb0ef41Sopenharmony_ci## Loaders 7111cb0ef41Sopenharmony_ci 7121cb0ef41Sopenharmony_ciThe former Loaders documentation is now at 7131cb0ef41Sopenharmony_ci[Modules: Customization hooks][Module customization hooks]. 7141cb0ef41Sopenharmony_ci 7151cb0ef41Sopenharmony_ci## Resolution and loading algorithm 7161cb0ef41Sopenharmony_ci 7171cb0ef41Sopenharmony_ci### Features 7181cb0ef41Sopenharmony_ci 7191cb0ef41Sopenharmony_ciThe default resolver has the following properties: 7201cb0ef41Sopenharmony_ci 7211cb0ef41Sopenharmony_ci* FileURL-based resolution as is used by ES modules 7221cb0ef41Sopenharmony_ci* Relative and absolute URL resolution 7231cb0ef41Sopenharmony_ci* No default extensions 7241cb0ef41Sopenharmony_ci* No folder mains 7251cb0ef41Sopenharmony_ci* Bare specifier package resolution lookup through node\_modules 7261cb0ef41Sopenharmony_ci* Does not fail on unknown extensions or protocols 7271cb0ef41Sopenharmony_ci* Can optionally provide a hint of the format to the loading phase 7281cb0ef41Sopenharmony_ci 7291cb0ef41Sopenharmony_ciThe default loader has the following properties 7301cb0ef41Sopenharmony_ci 7311cb0ef41Sopenharmony_ci* Support for builtin module loading via `node:` URLs 7321cb0ef41Sopenharmony_ci* Support for "inline" module loading via `data:` URLs 7331cb0ef41Sopenharmony_ci* Support for `file:` module loading 7341cb0ef41Sopenharmony_ci* Fails on any other URL protocol 7351cb0ef41Sopenharmony_ci* Fails on unknown extensions for `file:` loading 7361cb0ef41Sopenharmony_ci (supports only `.cjs`, `.js`, and `.mjs`) 7371cb0ef41Sopenharmony_ci 7381cb0ef41Sopenharmony_ci### Resolution algorithm 7391cb0ef41Sopenharmony_ci 7401cb0ef41Sopenharmony_ciThe algorithm to load an ES module specifier is given through the 7411cb0ef41Sopenharmony_ci**ESM\_RESOLVE** method below. It returns the resolved URL for a 7421cb0ef41Sopenharmony_cimodule specifier relative to a parentURL. 7431cb0ef41Sopenharmony_ci 7441cb0ef41Sopenharmony_ciThe resolution algorithm determines the full resolved URL for a module 7451cb0ef41Sopenharmony_ciload, along with its suggested module format. The resolution algorithm 7461cb0ef41Sopenharmony_cidoes not determine whether the resolved URL protocol can be loaded, 7471cb0ef41Sopenharmony_cior whether the file extensions are permitted, instead these validations 7481cb0ef41Sopenharmony_ciare applied by Node.js during the load phase 7491cb0ef41Sopenharmony_ci(for example, if it was asked to load a URL that has a protocol that is 7501cb0ef41Sopenharmony_cinot `file:`, `data:`, `node:`, or if `--experimental-network-imports` 7511cb0ef41Sopenharmony_ciis enabled, `https:`). 7521cb0ef41Sopenharmony_ci 7531cb0ef41Sopenharmony_ciThe algorithm also tries to determine the format of the file based 7541cb0ef41Sopenharmony_cion the extension (see `ESM_FILE_FORMAT` algorithm below). If it does 7551cb0ef41Sopenharmony_cinot recognize the file extension (eg if it is not `.mjs`, `.cjs`, or 7561cb0ef41Sopenharmony_ci`.json`), then a format of `undefined` is returned, 7571cb0ef41Sopenharmony_ciwhich will throw during the load phase. 7581cb0ef41Sopenharmony_ci 7591cb0ef41Sopenharmony_ciThe algorithm to determine the module format of a resolved URL is 7601cb0ef41Sopenharmony_ciprovided by **ESM\_FILE\_FORMAT**, which returns the unique module 7611cb0ef41Sopenharmony_ciformat for any file. The _"module"_ format is returned for an ECMAScript 7621cb0ef41Sopenharmony_ciModule, while the _"commonjs"_ format is used to indicate loading through the 7631cb0ef41Sopenharmony_cilegacy CommonJS loader. Additional formats such as _"addon"_ can be extended in 7641cb0ef41Sopenharmony_cifuture updates. 7651cb0ef41Sopenharmony_ci 7661cb0ef41Sopenharmony_ciIn the following algorithms, all subroutine errors are propagated as errors 7671cb0ef41Sopenharmony_ciof these top-level routines unless stated otherwise. 7681cb0ef41Sopenharmony_ci 7691cb0ef41Sopenharmony_ci_defaultConditions_ is the conditional environment name array, 7701cb0ef41Sopenharmony_ci`["node", "import"]`. 7711cb0ef41Sopenharmony_ci 7721cb0ef41Sopenharmony_ciThe resolver can throw the following errors: 7731cb0ef41Sopenharmony_ci 7741cb0ef41Sopenharmony_ci* _Invalid Module Specifier_: Module specifier is an invalid URL, package name 7751cb0ef41Sopenharmony_ci or package subpath specifier. 7761cb0ef41Sopenharmony_ci* _Invalid Package Configuration_: package.json configuration is invalid or 7771cb0ef41Sopenharmony_ci contains an invalid configuration. 7781cb0ef41Sopenharmony_ci* _Invalid Package Target_: Package exports or imports define a target module 7791cb0ef41Sopenharmony_ci for the package that is an invalid type or string target. 7801cb0ef41Sopenharmony_ci* _Package Path Not Exported_: Package exports do not define or permit a target 7811cb0ef41Sopenharmony_ci subpath in the package for the given module. 7821cb0ef41Sopenharmony_ci* _Package Import Not Defined_: Package imports do not define the specifier. 7831cb0ef41Sopenharmony_ci* _Module Not Found_: The package or module requested does not exist. 7841cb0ef41Sopenharmony_ci* _Unsupported Directory Import_: The resolved path corresponds to a directory, 7851cb0ef41Sopenharmony_ci which is not a supported target for module imports. 7861cb0ef41Sopenharmony_ci 7871cb0ef41Sopenharmony_ci### Resolution Algorithm Specification 7881cb0ef41Sopenharmony_ci 7891cb0ef41Sopenharmony_ci**ESM\_RESOLVE**(_specifier_, _parentURL_) 7901cb0ef41Sopenharmony_ci 7911cb0ef41Sopenharmony_ci> 1. Let _resolved_ be **undefined**. 7921cb0ef41Sopenharmony_ci> 2. If _specifier_ is a valid URL, then 7931cb0ef41Sopenharmony_ci> 1. Set _resolved_ to the result of parsing and reserializing 7941cb0ef41Sopenharmony_ci> _specifier_ as a URL. 7951cb0ef41Sopenharmony_ci> 3. Otherwise, if _specifier_ starts with _"/"_, _"./"_, or _"../"_, then 7961cb0ef41Sopenharmony_ci> 1. Set _resolved_ to the URL resolution of _specifier_ relative to 7971cb0ef41Sopenharmony_ci> _parentURL_. 7981cb0ef41Sopenharmony_ci> 4. Otherwise, if _specifier_ starts with _"#"_, then 7991cb0ef41Sopenharmony_ci> 1. Set _resolved_ to the result of 8001cb0ef41Sopenharmony_ci> **PACKAGE\_IMPORTS\_RESOLVE**(_specifier_, 8011cb0ef41Sopenharmony_ci> _parentURL_, _defaultConditions_). 8021cb0ef41Sopenharmony_ci> 5. Otherwise, 8031cb0ef41Sopenharmony_ci> 1. Note: _specifier_ is now a bare specifier. 8041cb0ef41Sopenharmony_ci> 2. Set _resolved_ the result of 8051cb0ef41Sopenharmony_ci> **PACKAGE\_RESOLVE**(_specifier_, _parentURL_). 8061cb0ef41Sopenharmony_ci> 6. Let _format_ be **undefined**. 8071cb0ef41Sopenharmony_ci> 7. If _resolved_ is a _"file:"_ URL, then 8081cb0ef41Sopenharmony_ci> 1. If _resolved_ contains any percent encodings of _"/"_ or _"\\"_ (_"%2F"_ 8091cb0ef41Sopenharmony_ci> and _"%5C"_ respectively), then 8101cb0ef41Sopenharmony_ci> 1. Throw an _Invalid Module Specifier_ error. 8111cb0ef41Sopenharmony_ci> 2. If the file at _resolved_ is a directory, then 8121cb0ef41Sopenharmony_ci> 1. Throw an _Unsupported Directory Import_ error. 8131cb0ef41Sopenharmony_ci> 3. If the file at _resolved_ does not exist, then 8141cb0ef41Sopenharmony_ci> 1. Throw a _Module Not Found_ error. 8151cb0ef41Sopenharmony_ci> 4. Set _resolved_ to the real path of _resolved_, maintaining the 8161cb0ef41Sopenharmony_ci> same URL querystring and fragment components. 8171cb0ef41Sopenharmony_ci> 5. Set _format_ to the result of **ESM\_FILE\_FORMAT**(_resolved_). 8181cb0ef41Sopenharmony_ci> 8. Otherwise, 8191cb0ef41Sopenharmony_ci> 1. Set _format_ the module format of the content type associated with the 8201cb0ef41Sopenharmony_ci> URL _resolved_. 8211cb0ef41Sopenharmony_ci> 9. Return _format_ and _resolved_ to the loading phase 8221cb0ef41Sopenharmony_ci 8231cb0ef41Sopenharmony_ci**PACKAGE\_RESOLVE**(_packageSpecifier_, _parentURL_) 8241cb0ef41Sopenharmony_ci 8251cb0ef41Sopenharmony_ci> 1. Let _packageName_ be **undefined**. 8261cb0ef41Sopenharmony_ci> 2. If _packageSpecifier_ is an empty string, then 8271cb0ef41Sopenharmony_ci> 1. Throw an _Invalid Module Specifier_ error. 8281cb0ef41Sopenharmony_ci> 3. If _packageSpecifier_ is a Node.js builtin module name, then 8291cb0ef41Sopenharmony_ci> 1. Return the string _"node:"_ concatenated with _packageSpecifier_. 8301cb0ef41Sopenharmony_ci> 4. If _packageSpecifier_ does not start with _"@"_, then 8311cb0ef41Sopenharmony_ci> 1. Set _packageName_ to the substring of _packageSpecifier_ until the first 8321cb0ef41Sopenharmony_ci> _"/"_ separator or the end of the string. 8331cb0ef41Sopenharmony_ci> 5. Otherwise, 8341cb0ef41Sopenharmony_ci> 1. If _packageSpecifier_ does not contain a _"/"_ separator, then 8351cb0ef41Sopenharmony_ci> 1. Throw an _Invalid Module Specifier_ error. 8361cb0ef41Sopenharmony_ci> 2. Set _packageName_ to the substring of _packageSpecifier_ 8371cb0ef41Sopenharmony_ci> until the second _"/"_ separator or the end of the string. 8381cb0ef41Sopenharmony_ci> 6. If _packageName_ starts with _"."_ or contains _"\\"_ or _"%"_, then 8391cb0ef41Sopenharmony_ci> 1. Throw an _Invalid Module Specifier_ error. 8401cb0ef41Sopenharmony_ci> 7. Let _packageSubpath_ be _"."_ concatenated with the substring of 8411cb0ef41Sopenharmony_ci> _packageSpecifier_ from the position at the length of _packageName_. 8421cb0ef41Sopenharmony_ci> 8. If _packageSubpath_ ends in _"/"_, then 8431cb0ef41Sopenharmony_ci> 1. Throw an _Invalid Module Specifier_ error. 8441cb0ef41Sopenharmony_ci> 9. Let _selfUrl_ be the result of 8451cb0ef41Sopenharmony_ci> **PACKAGE\_SELF\_RESOLVE**(_packageName_, _packageSubpath_, _parentURL_). 8461cb0ef41Sopenharmony_ci> 10. If _selfUrl_ is not **undefined**, return _selfUrl_. 8471cb0ef41Sopenharmony_ci> 11. While _parentURL_ is not the file system root, 8481cb0ef41Sopenharmony_ci> 1. Let _packageURL_ be the URL resolution of _"node\_modules/"_ 8491cb0ef41Sopenharmony_ci> concatenated with _packageSpecifier_, relative to _parentURL_. 8501cb0ef41Sopenharmony_ci> 2. Set _parentURL_ to the parent folder URL of _parentURL_. 8511cb0ef41Sopenharmony_ci> 3. If the folder at _packageURL_ does not exist, then 8521cb0ef41Sopenharmony_ci> 1. Continue the next loop iteration. 8531cb0ef41Sopenharmony_ci> 4. Let _pjson_ be the result of **READ\_PACKAGE\_JSON**(_packageURL_). 8541cb0ef41Sopenharmony_ci> 5. If _pjson_ is not **null** and _pjson_._exports_ is not **null** or 8551cb0ef41Sopenharmony_ci> **undefined**, then 8561cb0ef41Sopenharmony_ci> 1. Return the result of **PACKAGE\_EXPORTS\_RESOLVE**(_packageURL_, 8571cb0ef41Sopenharmony_ci> _packageSubpath_, _pjson.exports_, _defaultConditions_). 8581cb0ef41Sopenharmony_ci> 6. Otherwise, if _packageSubpath_ is equal to _"."_, then 8591cb0ef41Sopenharmony_ci> 1. If _pjson.main_ is a string, then 8601cb0ef41Sopenharmony_ci> 1. Return the URL resolution of _main_ in _packageURL_. 8611cb0ef41Sopenharmony_ci> 7. Otherwise, 8621cb0ef41Sopenharmony_ci> 1. Return the URL resolution of _packageSubpath_ in _packageURL_. 8631cb0ef41Sopenharmony_ci> 12. Throw a _Module Not Found_ error. 8641cb0ef41Sopenharmony_ci 8651cb0ef41Sopenharmony_ci**PACKAGE\_SELF\_RESOLVE**(_packageName_, _packageSubpath_, _parentURL_) 8661cb0ef41Sopenharmony_ci 8671cb0ef41Sopenharmony_ci> 1. Let _packageURL_ be the result of **LOOKUP\_PACKAGE\_SCOPE**(_parentURL_). 8681cb0ef41Sopenharmony_ci> 2. If _packageURL_ is **null**, then 8691cb0ef41Sopenharmony_ci> 1. Return **undefined**. 8701cb0ef41Sopenharmony_ci> 3. Let _pjson_ be the result of **READ\_PACKAGE\_JSON**(_packageURL_). 8711cb0ef41Sopenharmony_ci> 4. If _pjson_ is **null** or if _pjson_._exports_ is **null** or 8721cb0ef41Sopenharmony_ci> **undefined**, then 8731cb0ef41Sopenharmony_ci> 1. Return **undefined**. 8741cb0ef41Sopenharmony_ci> 5. If _pjson.name_ is equal to _packageName_, then 8751cb0ef41Sopenharmony_ci> 1. Return the result of **PACKAGE\_EXPORTS\_RESOLVE**(_packageURL_, 8761cb0ef41Sopenharmony_ci> _packageSubpath_, _pjson.exports_, _defaultConditions_). 8771cb0ef41Sopenharmony_ci> 6. Otherwise, return **undefined**. 8781cb0ef41Sopenharmony_ci 8791cb0ef41Sopenharmony_ci**PACKAGE\_EXPORTS\_RESOLVE**(_packageURL_, _subpath_, _exports_, _conditions_) 8801cb0ef41Sopenharmony_ci 8811cb0ef41Sopenharmony_ci> 1. If _exports_ is an Object with both a key starting with _"."_ and a key not 8821cb0ef41Sopenharmony_ci> starting with _"."_, throw an _Invalid Package Configuration_ error. 8831cb0ef41Sopenharmony_ci> 2. If _subpath_ is equal to _"."_, then 8841cb0ef41Sopenharmony_ci> 1. Let _mainExport_ be **undefined**. 8851cb0ef41Sopenharmony_ci> 2. If _exports_ is a String or Array, or an Object containing no keys 8861cb0ef41Sopenharmony_ci> starting with _"."_, then 8871cb0ef41Sopenharmony_ci> 1. Set _mainExport_ to _exports_. 8881cb0ef41Sopenharmony_ci> 3. Otherwise if _exports_ is an Object containing a _"."_ property, then 8891cb0ef41Sopenharmony_ci> 1. Set _mainExport_ to _exports_\[_"."_]. 8901cb0ef41Sopenharmony_ci> 4. If _mainExport_ is not **undefined**, then 8911cb0ef41Sopenharmony_ci> 1. Let _resolved_ be the result of **PACKAGE\_TARGET\_RESOLVE**( 8921cb0ef41Sopenharmony_ci> _packageURL_, _mainExport_, **null**, **false**, _conditions_). 8931cb0ef41Sopenharmony_ci> 2. If _resolved_ is not **null** or **undefined**, return _resolved_. 8941cb0ef41Sopenharmony_ci> 3. Otherwise, if _exports_ is an Object and all keys of _exports_ start with 8951cb0ef41Sopenharmony_ci> _"."_, then 8961cb0ef41Sopenharmony_ci> 1. Let _matchKey_ be the string _"./"_ concatenated with _subpath_. 8971cb0ef41Sopenharmony_ci> 2. Let _resolved_ be the result of **PACKAGE\_IMPORTS\_EXPORTS\_RESOLVE**( 8981cb0ef41Sopenharmony_ci> _matchKey_, _exports_, _packageURL_, **false**, _conditions_). 8991cb0ef41Sopenharmony_ci> 3. If _resolved_ is not **null** or **undefined**, return _resolved_. 9001cb0ef41Sopenharmony_ci> 4. Throw a _Package Path Not Exported_ error. 9011cb0ef41Sopenharmony_ci 9021cb0ef41Sopenharmony_ci**PACKAGE\_IMPORTS\_RESOLVE**(_specifier_, _parentURL_, _conditions_) 9031cb0ef41Sopenharmony_ci 9041cb0ef41Sopenharmony_ci> 1. Assert: _specifier_ begins with _"#"_. 9051cb0ef41Sopenharmony_ci> 2. If _specifier_ is exactly equal to _"#"_ or starts with _"#/"_, then 9061cb0ef41Sopenharmony_ci> 1. Throw an _Invalid Module Specifier_ error. 9071cb0ef41Sopenharmony_ci> 3. Let _packageURL_ be the result of **LOOKUP\_PACKAGE\_SCOPE**(_parentURL_). 9081cb0ef41Sopenharmony_ci> 4. If _packageURL_ is not **null**, then 9091cb0ef41Sopenharmony_ci> 1. Let _pjson_ be the result of **READ\_PACKAGE\_JSON**(_packageURL_). 9101cb0ef41Sopenharmony_ci> 2. If _pjson.imports_ is a non-null Object, then 9111cb0ef41Sopenharmony_ci> 1. Let _resolved_ be the result of 9121cb0ef41Sopenharmony_ci> **PACKAGE\_IMPORTS\_EXPORTS\_RESOLVE**( 9131cb0ef41Sopenharmony_ci> _specifier_, _pjson.imports_, _packageURL_, **true**, _conditions_). 9141cb0ef41Sopenharmony_ci> 2. If _resolved_ is not **null** or **undefined**, return _resolved_. 9151cb0ef41Sopenharmony_ci> 5. Throw a _Package Import Not Defined_ error. 9161cb0ef41Sopenharmony_ci 9171cb0ef41Sopenharmony_ci**PACKAGE\_IMPORTS\_EXPORTS\_RESOLVE**(_matchKey_, _matchObj_, _packageURL_, 9181cb0ef41Sopenharmony_ci_isImports_, _conditions_) 9191cb0ef41Sopenharmony_ci 9201cb0ef41Sopenharmony_ci> 1. If _matchKey_ is a key of _matchObj_ and does not contain _"\*"_, then 9211cb0ef41Sopenharmony_ci> 1. Let _target_ be the value of _matchObj_\[_matchKey_]. 9221cb0ef41Sopenharmony_ci> 2. Return the result of **PACKAGE\_TARGET\_RESOLVE**(_packageURL_, 9231cb0ef41Sopenharmony_ci> _target_, **null**, _isImports_, _conditions_). 9241cb0ef41Sopenharmony_ci> 2. Let _expansionKeys_ be the list of keys of _matchObj_ containing only a 9251cb0ef41Sopenharmony_ci> single _"\*"_, sorted by the sorting function **PATTERN\_KEY\_COMPARE** 9261cb0ef41Sopenharmony_ci> which orders in descending order of specificity. 9271cb0ef41Sopenharmony_ci> 3. For each key _expansionKey_ in _expansionKeys_, do 9281cb0ef41Sopenharmony_ci> 1. Let _patternBase_ be the substring of _expansionKey_ up to but excluding 9291cb0ef41Sopenharmony_ci> the first _"\*"_ character. 9301cb0ef41Sopenharmony_ci> 2. If _matchKey_ starts with but is not equal to _patternBase_, then 9311cb0ef41Sopenharmony_ci> 1. Let _patternTrailer_ be the substring of _expansionKey_ from the 9321cb0ef41Sopenharmony_ci> index after the first _"\*"_ character. 9331cb0ef41Sopenharmony_ci> 2. If _patternTrailer_ has zero length, or if _matchKey_ ends with 9341cb0ef41Sopenharmony_ci> _patternTrailer_ and the length of _matchKey_ is greater than or 9351cb0ef41Sopenharmony_ci> equal to the length of _expansionKey_, then 9361cb0ef41Sopenharmony_ci> 1. Let _target_ be the value of _matchObj_\[_expansionKey_]. 9371cb0ef41Sopenharmony_ci> 2. Let _patternMatch_ be the substring of _matchKey_ starting at the 9381cb0ef41Sopenharmony_ci> index of the length of _patternBase_ up to the length of 9391cb0ef41Sopenharmony_ci> _matchKey_ minus the length of _patternTrailer_. 9401cb0ef41Sopenharmony_ci> 3. Return the result of **PACKAGE\_TARGET\_RESOLVE**(_packageURL_, 9411cb0ef41Sopenharmony_ci> _target_, _patternMatch_, _isImports_, _conditions_). 9421cb0ef41Sopenharmony_ci> 4. Return **null**. 9431cb0ef41Sopenharmony_ci 9441cb0ef41Sopenharmony_ci**PATTERN\_KEY\_COMPARE**(_keyA_, _keyB_) 9451cb0ef41Sopenharmony_ci 9461cb0ef41Sopenharmony_ci> 1. Assert: _keyA_ ends with _"/"_ or contains only a single _"\*"_. 9471cb0ef41Sopenharmony_ci> 2. Assert: _keyB_ ends with _"/"_ or contains only a single _"\*"_. 9481cb0ef41Sopenharmony_ci> 3. Let _baseLengthA_ be the index of _"\*"_ in _keyA_ plus one, if _keyA_ 9491cb0ef41Sopenharmony_ci> contains _"\*"_, or the length of _keyA_ otherwise. 9501cb0ef41Sopenharmony_ci> 4. Let _baseLengthB_ be the index of _"\*"_ in _keyB_ plus one, if _keyB_ 9511cb0ef41Sopenharmony_ci> contains _"\*"_, or the length of _keyB_ otherwise. 9521cb0ef41Sopenharmony_ci> 5. If _baseLengthA_ is greater than _baseLengthB_, return -1. 9531cb0ef41Sopenharmony_ci> 6. If _baseLengthB_ is greater than _baseLengthA_, return 1. 9541cb0ef41Sopenharmony_ci> 7. If _keyA_ does not contain _"\*"_, return 1. 9551cb0ef41Sopenharmony_ci> 8. If _keyB_ does not contain _"\*"_, return -1. 9561cb0ef41Sopenharmony_ci> 9. If the length of _keyA_ is greater than the length of _keyB_, return -1. 9571cb0ef41Sopenharmony_ci> 10. If the length of _keyB_ is greater than the length of _keyA_, return 1. 9581cb0ef41Sopenharmony_ci> 11. Return 0. 9591cb0ef41Sopenharmony_ci 9601cb0ef41Sopenharmony_ci**PACKAGE\_TARGET\_RESOLVE**(_packageURL_, _target_, _patternMatch_, 9611cb0ef41Sopenharmony_ci_isImports_, _conditions_) 9621cb0ef41Sopenharmony_ci 9631cb0ef41Sopenharmony_ci> 1. If _target_ is a String, then 9641cb0ef41Sopenharmony_ci> 1. If _target_ does not start with _"./"_, then 9651cb0ef41Sopenharmony_ci> 1. If _isImports_ is **false**, or if _target_ starts with _"../"_ or 9661cb0ef41Sopenharmony_ci> _"/"_, or if _target_ is a valid URL, then 9671cb0ef41Sopenharmony_ci> 1. Throw an _Invalid Package Target_ error. 9681cb0ef41Sopenharmony_ci> 2. If _patternMatch_ is a String, then 9691cb0ef41Sopenharmony_ci> 1. Return **PACKAGE\_RESOLVE**(_target_ with every instance of _"\*"_ 9701cb0ef41Sopenharmony_ci> replaced by _patternMatch_, _packageURL_ + _"/"_). 9711cb0ef41Sopenharmony_ci> 3. Return **PACKAGE\_RESOLVE**(_target_, _packageURL_ + _"/"_). 9721cb0ef41Sopenharmony_ci> 2. If _target_ split on _"/"_ or _"\\"_ contains any _""_, _"."_, _".."_, 9731cb0ef41Sopenharmony_ci> or _"node\_modules"_ segments after the first _"."_ segment, case 9741cb0ef41Sopenharmony_ci> insensitive and including percent encoded variants, throw an _Invalid 9751cb0ef41Sopenharmony_ci> Package Target_ error. 9761cb0ef41Sopenharmony_ci> 3. Let _resolvedTarget_ be the URL resolution of the concatenation of 9771cb0ef41Sopenharmony_ci> _packageURL_ and _target_. 9781cb0ef41Sopenharmony_ci> 4. Assert: _resolvedTarget_ is contained in _packageURL_. 9791cb0ef41Sopenharmony_ci> 5. If _patternMatch_ is **null**, then 9801cb0ef41Sopenharmony_ci> 1. Return _resolvedTarget_. 9811cb0ef41Sopenharmony_ci> 6. If _patternMatch_ split on _"/"_ or _"\\"_ contains any _""_, _"."_, 9821cb0ef41Sopenharmony_ci> _".."_, or _"node\_modules"_ segments, case insensitive and including 9831cb0ef41Sopenharmony_ci> percent encoded variants, throw an _Invalid Module Specifier_ error. 9841cb0ef41Sopenharmony_ci> 7. Return the URL resolution of _resolvedTarget_ with every instance of 9851cb0ef41Sopenharmony_ci> _"\*"_ replaced with _patternMatch_. 9861cb0ef41Sopenharmony_ci> 2. Otherwise, if _target_ is a non-null Object, then 9871cb0ef41Sopenharmony_ci> 1. If _exports_ contains any index property keys, as defined in ECMA-262 9881cb0ef41Sopenharmony_ci> [6.1.7 Array Index][], throw an _Invalid Package Configuration_ error. 9891cb0ef41Sopenharmony_ci> 2. For each property _p_ of _target_, in object insertion order as, 9901cb0ef41Sopenharmony_ci> 1. If _p_ equals _"default"_ or _conditions_ contains an entry for _p_, 9911cb0ef41Sopenharmony_ci> then 9921cb0ef41Sopenharmony_ci> 1. Let _targetValue_ be the value of the _p_ property in _target_. 9931cb0ef41Sopenharmony_ci> 2. Let _resolved_ be the result of **PACKAGE\_TARGET\_RESOLVE**( 9941cb0ef41Sopenharmony_ci> _packageURL_, _targetValue_, _patternMatch_, _isImports_, 9951cb0ef41Sopenharmony_ci> _conditions_). 9961cb0ef41Sopenharmony_ci> 3. If _resolved_ is equal to **undefined**, continue the loop. 9971cb0ef41Sopenharmony_ci> 4. Return _resolved_. 9981cb0ef41Sopenharmony_ci> 3. Return **undefined**. 9991cb0ef41Sopenharmony_ci> 3. Otherwise, if _target_ is an Array, then 10001cb0ef41Sopenharmony_ci> 1. If \_target.length is zero, return **null**. 10011cb0ef41Sopenharmony_ci> 2. For each item _targetValue_ in _target_, do 10021cb0ef41Sopenharmony_ci> 1. Let _resolved_ be the result of **PACKAGE\_TARGET\_RESOLVE**( 10031cb0ef41Sopenharmony_ci> _packageURL_, _targetValue_, _patternMatch_, _isImports_, 10041cb0ef41Sopenharmony_ci> _conditions_), continuing the loop on any _Invalid Package Target_ 10051cb0ef41Sopenharmony_ci> error. 10061cb0ef41Sopenharmony_ci> 2. If _resolved_ is **undefined**, continue the loop. 10071cb0ef41Sopenharmony_ci> 3. Return _resolved_. 10081cb0ef41Sopenharmony_ci> 3. Return or throw the last fallback resolution **null** return or error. 10091cb0ef41Sopenharmony_ci> 4. Otherwise, if _target_ is _null_, return **null**. 10101cb0ef41Sopenharmony_ci> 5. Otherwise throw an _Invalid Package Target_ error. 10111cb0ef41Sopenharmony_ci 10121cb0ef41Sopenharmony_ci**ESM\_FILE\_FORMAT**(_url_) 10131cb0ef41Sopenharmony_ci 10141cb0ef41Sopenharmony_ci> 1. Assert: _url_ corresponds to an existing file. 10151cb0ef41Sopenharmony_ci> 2. If _url_ ends in _".mjs"_, then 10161cb0ef41Sopenharmony_ci> 1. Return _"module"_. 10171cb0ef41Sopenharmony_ci> 3. If _url_ ends in _".cjs"_, then 10181cb0ef41Sopenharmony_ci> 1. Return _"commonjs"_. 10191cb0ef41Sopenharmony_ci> 4. If _url_ ends in _".json"_, then 10201cb0ef41Sopenharmony_ci> 1. Return _"json"_. 10211cb0ef41Sopenharmony_ci> 5. Let _packageURL_ be the result of **LOOKUP\_PACKAGE\_SCOPE**(_url_). 10221cb0ef41Sopenharmony_ci> 6. Let _pjson_ be the result of **READ\_PACKAGE\_JSON**(_packageURL_). 10231cb0ef41Sopenharmony_ci> 7. If _pjson?.type_ exists and is _"module"_, then 10241cb0ef41Sopenharmony_ci> 1. If _url_ ends in _".js"_ or has no file extension, then 10251cb0ef41Sopenharmony_ci> 1. If `--experimental-wasm-modules` is enabled and the file at _url_ 10261cb0ef41Sopenharmony_ci> contains the header for a WebAssembly module, then 10271cb0ef41Sopenharmony_ci> 1. Return _"wasm"_. 10281cb0ef41Sopenharmony_ci> 2. Otherwise, 10291cb0ef41Sopenharmony_ci> 1. Return _"module"_. 10301cb0ef41Sopenharmony_ci> 2. Return **undefined**. 10311cb0ef41Sopenharmony_ci> 8. Otherwise, 10321cb0ef41Sopenharmony_ci> 1. Return **undefined**. 10331cb0ef41Sopenharmony_ci 10341cb0ef41Sopenharmony_ci**LOOKUP\_PACKAGE\_SCOPE**(_url_) 10351cb0ef41Sopenharmony_ci 10361cb0ef41Sopenharmony_ci> 1. Let _scopeURL_ be _url_. 10371cb0ef41Sopenharmony_ci> 2. While _scopeURL_ is not the file system root, 10381cb0ef41Sopenharmony_ci> 1. Set _scopeURL_ to the parent URL of _scopeURL_. 10391cb0ef41Sopenharmony_ci> 2. If _scopeURL_ ends in a _"node\_modules"_ path segment, return **null**. 10401cb0ef41Sopenharmony_ci> 3. Let _pjsonURL_ be the resolution of _"package.json"_ within 10411cb0ef41Sopenharmony_ci> _scopeURL_. 10421cb0ef41Sopenharmony_ci> 4. if the file at _pjsonURL_ exists, then 10431cb0ef41Sopenharmony_ci> 1. Return _scopeURL_. 10441cb0ef41Sopenharmony_ci> 3. Return **null**. 10451cb0ef41Sopenharmony_ci 10461cb0ef41Sopenharmony_ci**READ\_PACKAGE\_JSON**(_packageURL_) 10471cb0ef41Sopenharmony_ci 10481cb0ef41Sopenharmony_ci> 1. Let _pjsonURL_ be the resolution of _"package.json"_ within _packageURL_. 10491cb0ef41Sopenharmony_ci> 2. If the file at _pjsonURL_ does not exist, then 10501cb0ef41Sopenharmony_ci> 1. Return **null**. 10511cb0ef41Sopenharmony_ci> 3. If the file at _packageURL_ does not parse as valid JSON, then 10521cb0ef41Sopenharmony_ci> 1. Throw an _Invalid Package Configuration_ error. 10531cb0ef41Sopenharmony_ci> 4. Return the parsed JSON source of the file at _pjsonURL_. 10541cb0ef41Sopenharmony_ci 10551cb0ef41Sopenharmony_ci### Customizing ESM specifier resolution algorithm 10561cb0ef41Sopenharmony_ci 10571cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 10581cb0ef41Sopenharmony_ci 10591cb0ef41Sopenharmony_ci> Do not rely on this flag. We plan to remove it once the 10601cb0ef41Sopenharmony_ci> [Module customization hooks][] have advanced to the point that equivalent 10611cb0ef41Sopenharmony_ci> functionality can be achieved via custom hooks. 10621cb0ef41Sopenharmony_ci 10631cb0ef41Sopenharmony_ciThe current specifier resolution does not support all default behavior of 10641cb0ef41Sopenharmony_cithe CommonJS loader. One of the behavior differences is automatic resolution 10651cb0ef41Sopenharmony_ciof file extensions and the ability to import directories that have an index 10661cb0ef41Sopenharmony_cifile. 10671cb0ef41Sopenharmony_ci 10681cb0ef41Sopenharmony_ciThe `--experimental-specifier-resolution=[mode]` flag can be used to customize 10691cb0ef41Sopenharmony_cithe extension resolution algorithm. The default mode is `explicit`, which 10701cb0ef41Sopenharmony_cirequires the full path to a module be provided to the loader. To enable the 10711cb0ef41Sopenharmony_ciautomatic extension resolution and importing from directories that include an 10721cb0ef41Sopenharmony_ciindex file use the `node` mode. 10731cb0ef41Sopenharmony_ci 10741cb0ef41Sopenharmony_ci```console 10751cb0ef41Sopenharmony_ci$ node index.mjs 10761cb0ef41Sopenharmony_cisuccess! 10771cb0ef41Sopenharmony_ci$ node index # Failure! 10781cb0ef41Sopenharmony_ciError: Cannot find module 10791cb0ef41Sopenharmony_ci$ node --experimental-specifier-resolution=node index 10801cb0ef41Sopenharmony_cisuccess! 10811cb0ef41Sopenharmony_ci``` 10821cb0ef41Sopenharmony_ci 10831cb0ef41Sopenharmony_ci<!-- Note: The cjs-module-lexer link should be kept in-sync with the deps version --> 10841cb0ef41Sopenharmony_ci 10851cb0ef41Sopenharmony_ci[6.1.7 Array Index]: https://tc39.es/ecma262/#integer-index 10861cb0ef41Sopenharmony_ci[Addons]: addons.md 10871cb0ef41Sopenharmony_ci[CommonJS]: modules.md 10881cb0ef41Sopenharmony_ci[Core modules]: modules.md#core-modules 10891cb0ef41Sopenharmony_ci[Determining module system]: packages.md#determining-module-system 10901cb0ef41Sopenharmony_ci[Dynamic `import()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import 10911cb0ef41Sopenharmony_ci[ES Module Integration Proposal for WebAssembly]: https://github.com/webassembly/esm-integration 10921cb0ef41Sopenharmony_ci[Import Attributes]: #import-attributes 10931cb0ef41Sopenharmony_ci[Import Attributes proposal]: https://github.com/tc39/proposal-import-attributes 10941cb0ef41Sopenharmony_ci[JSON modules]: #json-modules 10951cb0ef41Sopenharmony_ci[Module customization hooks]: module.md#customization-hooks 10961cb0ef41Sopenharmony_ci[Node.js Module Resolution And Loading Algorithm]: #resolution-algorithm-specification 10971cb0ef41Sopenharmony_ci[Terminology]: #terminology 10981cb0ef41Sopenharmony_ci[URL]: https://url.spec.whatwg.org/ 10991cb0ef41Sopenharmony_ci[`"exports"`]: packages.md#exports 11001cb0ef41Sopenharmony_ci[`"type"`]: packages.md#type 11011cb0ef41Sopenharmony_ci[`--experimental-default-type`]: cli.md#--experimental-default-typetype 11021cb0ef41Sopenharmony_ci[`--input-type`]: cli.md#--input-typetype 11031cb0ef41Sopenharmony_ci[`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs 11041cb0ef41Sopenharmony_ci[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export 11051cb0ef41Sopenharmony_ci[`import()`]: #import-expressions 11061cb0ef41Sopenharmony_ci[`import.meta.resolve`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve 11071cb0ef41Sopenharmony_ci[`import.meta.url`]: #importmetaurl 11081cb0ef41Sopenharmony_ci[`import`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import 11091cb0ef41Sopenharmony_ci[`module.createRequire()`]: module.md#modulecreaterequirefilename 11101cb0ef41Sopenharmony_ci[`module.syncBuiltinESMExports()`]: module.md#modulesyncbuiltinesmexports 11111cb0ef41Sopenharmony_ci[`package.json`]: packages.md#nodejs-packagejson-field-definitions 11121cb0ef41Sopenharmony_ci[`process.dlopen`]: process.md#processdlopenmodule-filename-flags 11131cb0ef41Sopenharmony_ci[cjs-module-lexer]: https://github.com/nodejs/cjs-module-lexer/tree/1.2.2 11141cb0ef41Sopenharmony_ci[custom https loader]: module.md#import-from-https 11151cb0ef41Sopenharmony_ci[import.meta.resolve]: #importmetaresolvespecifier 11161cb0ef41Sopenharmony_ci[percent-encoded]: url.md#percent-encoding-in-urls 11171cb0ef41Sopenharmony_ci[special scheme]: https://url.spec.whatwg.org/#special-scheme 11181cb0ef41Sopenharmony_ci[status code]: process.md#exit-codes 11191cb0ef41Sopenharmony_ci[the official standard format]: https://tc39.github.io/ecma262/#sec-modules 11201cb0ef41Sopenharmony_ci[url.pathToFileURL]: url.md#urlpathtofileurlpath 1121