11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<html lang="en"> 31cb0ef41Sopenharmony_ci<head> 41cb0ef41Sopenharmony_ci <meta charset="utf-8"> 51cb0ef41Sopenharmony_ci <meta name="viewport" content="width=device-width"> 61cb0ef41Sopenharmony_ci <meta name="nodejs.org:node-version" content="v18.20.1"> 71cb0ef41Sopenharmony_ci <title>Modules: ECMAScript modules | Node.js v18.20.1 Documentation</title> 81cb0ef41Sopenharmony_ci <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 91cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/style.css"> 101cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/hljs.css"> 111cb0ef41Sopenharmony_ci <link rel="canonical" href="https://nodejs.org/api/esm.html"> 121cb0ef41Sopenharmony_ci <script async defer src="assets/api.js" type="text/javascript"></script> 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci</head> 151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-esm"> 161cb0ef41Sopenharmony_ci <div id="content" class="clearfix"> 171cb0ef41Sopenharmony_ci <div id="column2" class="interior"> 181cb0ef41Sopenharmony_ci <div id="intro" class="interior"> 191cb0ef41Sopenharmony_ci <a href="/" title="Go back to the home page"> 201cb0ef41Sopenharmony_ci Node.js 211cb0ef41Sopenharmony_ci </a> 221cb0ef41Sopenharmony_ci </div> 231cb0ef41Sopenharmony_ci <ul> 241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 261cb0ef41Sopenharmony_ci</ul> 271cb0ef41Sopenharmony_ci<hr class="line"> 281cb0ef41Sopenharmony_ci<ul> 291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm active">Modules: ECMAScript modules</a></li> 581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 891cb0ef41Sopenharmony_ci</ul> 901cb0ef41Sopenharmony_ci<hr class="line"> 911cb0ef41Sopenharmony_ci<ul> 921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 931cb0ef41Sopenharmony_ci</ul> 941cb0ef41Sopenharmony_ci </div> 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci <div id="column1" data-id="esm" class="interior"> 971cb0ef41Sopenharmony_ci <header class="header"> 981cb0ef41Sopenharmony_ci <div class="header-container"> 991cb0ef41Sopenharmony_ci <h1>Node.js v18.20.1 documentation</h1> 1001cb0ef41Sopenharmony_ci <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 1011cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 1021cb0ef41Sopenharmony_ci <path fill="none" d="M0 0h24v24H0z" /> 1031cb0ef41Sopenharmony_ci <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 1041cb0ef41Sopenharmony_ci <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 1051cb0ef41Sopenharmony_ci </svg> 1061cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 1071cb0ef41Sopenharmony_ci <path d="M0 0h24v24H0z" fill="none" /> 1081cb0ef41Sopenharmony_ci <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 1091cb0ef41Sopenharmony_ci </svg> 1101cb0ef41Sopenharmony_ci </button> 1111cb0ef41Sopenharmony_ci </div> 1121cb0ef41Sopenharmony_ci <div id="gtoc"> 1131cb0ef41Sopenharmony_ci <ul> 1141cb0ef41Sopenharmony_ci <li class="pinned-header">Node.js v18.20.1</li> 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci <li class="picker-header"> 1171cb0ef41Sopenharmony_ci <a href="#"> 1181cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1191cb0ef41Sopenharmony_ci Table of contents 1201cb0ef41Sopenharmony_ci </a> 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci <div class="picker"><div class="toc"><ul> 1231cb0ef41Sopenharmony_ci<li><a href="#modules-ecmascript-modules">Modules: ECMAScript modules</a> 1241cb0ef41Sopenharmony_ci<ul> 1251cb0ef41Sopenharmony_ci<li><a href="#introduction">Introduction</a></li> 1261cb0ef41Sopenharmony_ci<li><a href="#enabling">Enabling</a></li> 1271cb0ef41Sopenharmony_ci<li><a href="#packages">Packages</a></li> 1281cb0ef41Sopenharmony_ci<li><a href="#import-specifiers"><code>import</code> Specifiers</a> 1291cb0ef41Sopenharmony_ci<ul> 1301cb0ef41Sopenharmony_ci<li><a href="#terminology">Terminology</a></li> 1311cb0ef41Sopenharmony_ci<li><a href="#mandatory-file-extensions">Mandatory file extensions</a></li> 1321cb0ef41Sopenharmony_ci<li><a href="#urls">URLs</a> 1331cb0ef41Sopenharmony_ci<ul> 1341cb0ef41Sopenharmony_ci<li><a href="#file-urls"><code>file:</code> URLs</a></li> 1351cb0ef41Sopenharmony_ci<li><a href="#data-imports"><code>data:</code> imports</a></li> 1361cb0ef41Sopenharmony_ci<li><a href="#node-imports"><code>node:</code> imports</a></li> 1371cb0ef41Sopenharmony_ci</ul> 1381cb0ef41Sopenharmony_ci</li> 1391cb0ef41Sopenharmony_ci</ul> 1401cb0ef41Sopenharmony_ci</li> 1411cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#import-attributes">Import attributes</a></span></li> 1421cb0ef41Sopenharmony_ci<li><a href="#builtin-modules">Builtin modules</a></li> 1431cb0ef41Sopenharmony_ci<li><a href="#import-expressions"><code>import()</code> expressions</a></li> 1441cb0ef41Sopenharmony_ci<li><a href="#importmeta"><code>import.meta</code></a> 1451cb0ef41Sopenharmony_ci<ul> 1461cb0ef41Sopenharmony_ci<li><a href="#importmetaurl"><code>import.meta.url</code></a></li> 1471cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#importmetaresolvespecifier"><code>import.meta.resolve(specifier)</code></a></span></li> 1481cb0ef41Sopenharmony_ci</ul> 1491cb0ef41Sopenharmony_ci</li> 1501cb0ef41Sopenharmony_ci<li><a href="#interoperability-with-commonjs">Interoperability with CommonJS</a> 1511cb0ef41Sopenharmony_ci<ul> 1521cb0ef41Sopenharmony_ci<li><a href="#import-statements"><code>import</code> statements</a></li> 1531cb0ef41Sopenharmony_ci<li><a href="#require"><code>require</code></a></li> 1541cb0ef41Sopenharmony_ci<li><a href="#commonjs-namespaces">CommonJS Namespaces</a></li> 1551cb0ef41Sopenharmony_ci<li><a href="#differences-between-es-modules-and-commonjs">Differences between ES modules and CommonJS</a> 1561cb0ef41Sopenharmony_ci<ul> 1571cb0ef41Sopenharmony_ci<li><a href="#no-require-exports-or-moduleexports">No <code>require</code>, <code>exports</code>, or <code>module.exports</code></a></li> 1581cb0ef41Sopenharmony_ci<li><a href="#no-__filename-or-__dirname">No <code>__filename</code> or <code>__dirname</code></a></li> 1591cb0ef41Sopenharmony_ci<li><a href="#no-addon-loading">No Addon Loading</a></li> 1601cb0ef41Sopenharmony_ci<li><a href="#no-requireresolve">No <code>require.resolve</code></a></li> 1611cb0ef41Sopenharmony_ci<li><a href="#no-node_path">No <code>NODE_PATH</code></a></li> 1621cb0ef41Sopenharmony_ci<li><a href="#no-requireextensions">No <code>require.extensions</code></a></li> 1631cb0ef41Sopenharmony_ci<li><a href="#no-requirecache">No <code>require.cache</code></a></li> 1641cb0ef41Sopenharmony_ci</ul> 1651cb0ef41Sopenharmony_ci</li> 1661cb0ef41Sopenharmony_ci</ul> 1671cb0ef41Sopenharmony_ci</li> 1681cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#json-modules">JSON modules</a></span></li> 1691cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#wasm-modules">Wasm modules</a></span></li> 1701cb0ef41Sopenharmony_ci<li><a href="#top-level-await">Top-level <code>await</code></a></li> 1711cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#https-and-http-imports">HTTPS and HTTP imports</a></span> 1721cb0ef41Sopenharmony_ci<ul> 1731cb0ef41Sopenharmony_ci<li><a href="#imports-are-limited-to-http1">Imports are limited to HTTP/1</a></li> 1741cb0ef41Sopenharmony_ci<li><a href="#http-is-limited-to-loopback-addresses">HTTP is limited to loopback addresses</a></li> 1751cb0ef41Sopenharmony_ci<li><a href="#authentication-is-never-sent-to-the-destination-server">Authentication is never sent to the destination server.</a></li> 1761cb0ef41Sopenharmony_ci<li><a href="#cors-is-never-checked-on-the-destination-server">CORS is never checked on the destination server</a></li> 1771cb0ef41Sopenharmony_ci<li><a href="#cannot-load-non-network-dependencies">Cannot load non-network dependencies</a></li> 1781cb0ef41Sopenharmony_ci<li><a href="#network-based-loading-is-not-enabled-by-default">Network-based loading is not enabled by default</a></li> 1791cb0ef41Sopenharmony_ci</ul> 1801cb0ef41Sopenharmony_ci</li> 1811cb0ef41Sopenharmony_ci<li><a href="#loaders">Loaders</a></li> 1821cb0ef41Sopenharmony_ci<li><a href="#resolution-and-loading-algorithm">Resolution and loading algorithm</a> 1831cb0ef41Sopenharmony_ci<ul> 1841cb0ef41Sopenharmony_ci<li><a href="#features">Features</a></li> 1851cb0ef41Sopenharmony_ci<li><a href="#resolution-algorithm">Resolution algorithm</a></li> 1861cb0ef41Sopenharmony_ci<li><a href="#resolution-algorithm-specification">Resolution Algorithm Specification</a></li> 1871cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#customizing-esm-specifier-resolution-algorithm">Customizing ESM specifier resolution algorithm</a></span></li> 1881cb0ef41Sopenharmony_ci</ul> 1891cb0ef41Sopenharmony_ci</li> 1901cb0ef41Sopenharmony_ci</ul> 1911cb0ef41Sopenharmony_ci</li> 1921cb0ef41Sopenharmony_ci</ul></div></div> 1931cb0ef41Sopenharmony_ci </li> 1941cb0ef41Sopenharmony_ci 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_ci <li class="picker-header"> 1971cb0ef41Sopenharmony_ci <a href="#"> 1981cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1991cb0ef41Sopenharmony_ci Index 2001cb0ef41Sopenharmony_ci </a> 2011cb0ef41Sopenharmony_ci 2021cb0ef41Sopenharmony_ci <div class="picker"><ul> 2031cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 2041cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ci <li> 2071cb0ef41Sopenharmony_ci <a href="index.html">Index</a> 2081cb0ef41Sopenharmony_ci </li> 2091cb0ef41Sopenharmony_ci </ul> 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ci<hr class="line"> 2121cb0ef41Sopenharmony_ci<ul> 2131cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 2141cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 2151cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 2161cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 2171cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 2181cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 2191cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 2201cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 2211cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 2221cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 2231cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 2241cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 2251cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 2261cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 2271cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 2281cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 2291cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 2301cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 2311cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 2321cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 2331cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 2341cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 2351cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 2361cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 2371cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 2381cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 2391cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 2401cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 2411cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm active">Modules: ECMAScript modules</a></li> 2421cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 2431cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 2441cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 2451cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 2461cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 2471cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 2481cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 2491cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 2501cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 2511cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 2521cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 2531cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 2541cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 2551cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 2561cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 2571cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 2581cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 2591cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 2601cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 2611cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 2621cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 2631cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 2641cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 2651cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 2661cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 2671cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 2681cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 2691cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 2701cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 2711cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 2721cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 2731cb0ef41Sopenharmony_ci</ul> 2741cb0ef41Sopenharmony_ci<hr class="line"> 2751cb0ef41Sopenharmony_ci<ul> 2761cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 2771cb0ef41Sopenharmony_ci</ul></div> 2781cb0ef41Sopenharmony_ci </li> 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_ci 2811cb0ef41Sopenharmony_ci <li class="picker-header"> 2821cb0ef41Sopenharmony_ci <a href="#"> 2831cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2841cb0ef41Sopenharmony_ci Other versions 2851cb0ef41Sopenharmony_ci </a> 2861cb0ef41Sopenharmony_ci <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/esm.html">21.x</a></li> 2871cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/esm.html">20.x <b>LTS</b></a></li> 2881cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/esm.html">19.x</a></li> 2891cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/esm.html">18.x <b>LTS</b></a></li> 2901cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/esm.html">17.x</a></li> 2911cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/esm.html">16.x</a></li> 2921cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/esm.html">15.x</a></li> 2931cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/esm.html">14.x</a></li> 2941cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/esm.html">13.x</a></li> 2951cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/esm.html">12.x</a></li> 2961cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/esm.html">11.x</a></li> 2971cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/esm.html">10.x</a></li> 2981cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/esm.html">9.x</a></li> 2991cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/esm.html">8.x</a></li></ol></div> 3001cb0ef41Sopenharmony_ci </li> 3011cb0ef41Sopenharmony_ci 3021cb0ef41Sopenharmony_ci <li class="picker-header"> 3031cb0ef41Sopenharmony_ci <a href="#"> 3041cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 3051cb0ef41Sopenharmony_ci Options 3061cb0ef41Sopenharmony_ci </a> 3071cb0ef41Sopenharmony_ci 3081cb0ef41Sopenharmony_ci <div class="picker"> 3091cb0ef41Sopenharmony_ci <ul> 3101cb0ef41Sopenharmony_ci <li> 3111cb0ef41Sopenharmony_ci <a href="all.html">View on single page</a> 3121cb0ef41Sopenharmony_ci </li> 3131cb0ef41Sopenharmony_ci <li> 3141cb0ef41Sopenharmony_ci <a href="esm.json">View as JSON</a> 3151cb0ef41Sopenharmony_ci </li> 3161cb0ef41Sopenharmony_ci <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/esm.md">Edit on GitHub</a></li> 3171cb0ef41Sopenharmony_ci </ul> 3181cb0ef41Sopenharmony_ci </div> 3191cb0ef41Sopenharmony_ci </li> 3201cb0ef41Sopenharmony_ci </ul> 3211cb0ef41Sopenharmony_ci </div> 3221cb0ef41Sopenharmony_ci <hr> 3231cb0ef41Sopenharmony_ci </header> 3241cb0ef41Sopenharmony_ci 3251cb0ef41Sopenharmony_ci <details id="toc" open><summary>Table of contents</summary><ul> 3261cb0ef41Sopenharmony_ci<li><a href="#modules-ecmascript-modules">Modules: ECMAScript modules</a> 3271cb0ef41Sopenharmony_ci<ul> 3281cb0ef41Sopenharmony_ci<li><a href="#introduction">Introduction</a></li> 3291cb0ef41Sopenharmony_ci<li><a href="#enabling">Enabling</a></li> 3301cb0ef41Sopenharmony_ci<li><a href="#packages">Packages</a></li> 3311cb0ef41Sopenharmony_ci<li><a href="#import-specifiers"><code>import</code> Specifiers</a> 3321cb0ef41Sopenharmony_ci<ul> 3331cb0ef41Sopenharmony_ci<li><a href="#terminology">Terminology</a></li> 3341cb0ef41Sopenharmony_ci<li><a href="#mandatory-file-extensions">Mandatory file extensions</a></li> 3351cb0ef41Sopenharmony_ci<li><a href="#urls">URLs</a> 3361cb0ef41Sopenharmony_ci<ul> 3371cb0ef41Sopenharmony_ci<li><a href="#file-urls"><code>file:</code> URLs</a></li> 3381cb0ef41Sopenharmony_ci<li><a href="#data-imports"><code>data:</code> imports</a></li> 3391cb0ef41Sopenharmony_ci<li><a href="#node-imports"><code>node:</code> imports</a></li> 3401cb0ef41Sopenharmony_ci</ul> 3411cb0ef41Sopenharmony_ci</li> 3421cb0ef41Sopenharmony_ci</ul> 3431cb0ef41Sopenharmony_ci</li> 3441cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#import-attributes">Import attributes</a></span></li> 3451cb0ef41Sopenharmony_ci<li><a href="#builtin-modules">Builtin modules</a></li> 3461cb0ef41Sopenharmony_ci<li><a href="#import-expressions"><code>import()</code> expressions</a></li> 3471cb0ef41Sopenharmony_ci<li><a href="#importmeta"><code>import.meta</code></a> 3481cb0ef41Sopenharmony_ci<ul> 3491cb0ef41Sopenharmony_ci<li><a href="#importmetaurl"><code>import.meta.url</code></a></li> 3501cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#importmetaresolvespecifier"><code>import.meta.resolve(specifier)</code></a></span></li> 3511cb0ef41Sopenharmony_ci</ul> 3521cb0ef41Sopenharmony_ci</li> 3531cb0ef41Sopenharmony_ci<li><a href="#interoperability-with-commonjs">Interoperability with CommonJS</a> 3541cb0ef41Sopenharmony_ci<ul> 3551cb0ef41Sopenharmony_ci<li><a href="#import-statements"><code>import</code> statements</a></li> 3561cb0ef41Sopenharmony_ci<li><a href="#require"><code>require</code></a></li> 3571cb0ef41Sopenharmony_ci<li><a href="#commonjs-namespaces">CommonJS Namespaces</a></li> 3581cb0ef41Sopenharmony_ci<li><a href="#differences-between-es-modules-and-commonjs">Differences between ES modules and CommonJS</a> 3591cb0ef41Sopenharmony_ci<ul> 3601cb0ef41Sopenharmony_ci<li><a href="#no-require-exports-or-moduleexports">No <code>require</code>, <code>exports</code>, or <code>module.exports</code></a></li> 3611cb0ef41Sopenharmony_ci<li><a href="#no-__filename-or-__dirname">No <code>__filename</code> or <code>__dirname</code></a></li> 3621cb0ef41Sopenharmony_ci<li><a href="#no-addon-loading">No Addon Loading</a></li> 3631cb0ef41Sopenharmony_ci<li><a href="#no-requireresolve">No <code>require.resolve</code></a></li> 3641cb0ef41Sopenharmony_ci<li><a href="#no-node_path">No <code>NODE_PATH</code></a></li> 3651cb0ef41Sopenharmony_ci<li><a href="#no-requireextensions">No <code>require.extensions</code></a></li> 3661cb0ef41Sopenharmony_ci<li><a href="#no-requirecache">No <code>require.cache</code></a></li> 3671cb0ef41Sopenharmony_ci</ul> 3681cb0ef41Sopenharmony_ci</li> 3691cb0ef41Sopenharmony_ci</ul> 3701cb0ef41Sopenharmony_ci</li> 3711cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#json-modules">JSON modules</a></span></li> 3721cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#wasm-modules">Wasm modules</a></span></li> 3731cb0ef41Sopenharmony_ci<li><a href="#top-level-await">Top-level <code>await</code></a></li> 3741cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#https-and-http-imports">HTTPS and HTTP imports</a></span> 3751cb0ef41Sopenharmony_ci<ul> 3761cb0ef41Sopenharmony_ci<li><a href="#imports-are-limited-to-http1">Imports are limited to HTTP/1</a></li> 3771cb0ef41Sopenharmony_ci<li><a href="#http-is-limited-to-loopback-addresses">HTTP is limited to loopback addresses</a></li> 3781cb0ef41Sopenharmony_ci<li><a href="#authentication-is-never-sent-to-the-destination-server">Authentication is never sent to the destination server.</a></li> 3791cb0ef41Sopenharmony_ci<li><a href="#cors-is-never-checked-on-the-destination-server">CORS is never checked on the destination server</a></li> 3801cb0ef41Sopenharmony_ci<li><a href="#cannot-load-non-network-dependencies">Cannot load non-network dependencies</a></li> 3811cb0ef41Sopenharmony_ci<li><a href="#network-based-loading-is-not-enabled-by-default">Network-based loading is not enabled by default</a></li> 3821cb0ef41Sopenharmony_ci</ul> 3831cb0ef41Sopenharmony_ci</li> 3841cb0ef41Sopenharmony_ci<li><a href="#loaders">Loaders</a></li> 3851cb0ef41Sopenharmony_ci<li><a href="#resolution-and-loading-algorithm">Resolution and loading algorithm</a> 3861cb0ef41Sopenharmony_ci<ul> 3871cb0ef41Sopenharmony_ci<li><a href="#features">Features</a></li> 3881cb0ef41Sopenharmony_ci<li><a href="#resolution-algorithm">Resolution algorithm</a></li> 3891cb0ef41Sopenharmony_ci<li><a href="#resolution-algorithm-specification">Resolution Algorithm Specification</a></li> 3901cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#customizing-esm-specifier-resolution-algorithm">Customizing ESM specifier resolution algorithm</a></span></li> 3911cb0ef41Sopenharmony_ci</ul> 3921cb0ef41Sopenharmony_ci</li> 3931cb0ef41Sopenharmony_ci</ul> 3941cb0ef41Sopenharmony_ci</li> 3951cb0ef41Sopenharmony_ci</ul></details> 3961cb0ef41Sopenharmony_ci 3971cb0ef41Sopenharmony_ci <div id="apicontent"> 3981cb0ef41Sopenharmony_ci <h2>Modules: ECMAScript modules<span><a class="mark" href="#modules-ecmascript-modules" id="modules-ecmascript-modules">#</a></span><a aria-hidden="true" class="legacy" id="esm_modules_ecmascript_modules"></a></h2> 3991cb0ef41Sopenharmony_ci 4001cb0ef41Sopenharmony_ci 4011cb0ef41Sopenharmony_ci<div class="api_metadata"> 4021cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 4031cb0ef41Sopenharmony_ci<table> 4041cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 4051cb0ef41Sopenharmony_ci<tr><td>v18.20.0</td> 4061cb0ef41Sopenharmony_ci<td><p>Add experimental support for import attributes.</p></td></tr> 4071cb0ef41Sopenharmony_ci<tr><td>v18.19.0</td> 4081cb0ef41Sopenharmony_ci<td><p>Module customization hooks are executed off the main thread.</p></td></tr> 4091cb0ef41Sopenharmony_ci<tr><td>v18.6.0</td> 4101cb0ef41Sopenharmony_ci<td><p>Add support for chaining module customization hooks.</p></td></tr> 4111cb0ef41Sopenharmony_ci<tr><td>v17.1.0, v16.14.0</td> 4121cb0ef41Sopenharmony_ci<td><p>Add experimental support for import assertions.</p></td></tr> 4131cb0ef41Sopenharmony_ci<tr><td>v17.0.0, v16.12.0</td> 4141cb0ef41Sopenharmony_ci<td><p>Consolidate customization hooks, removed <code>getFormat</code>, <code>getSource</code>, <code>transformSource</code>, and <code>getGlobalPreloadCode</code> hooks added <code>load</code> and <code>globalPreload</code> hooks allowed returning <code>format</code> from either <code>resolve</code> or <code>load</code> hooks.</p></td></tr> 4151cb0ef41Sopenharmony_ci<tr><td>v14.8.0</td> 4161cb0ef41Sopenharmony_ci<td><p>Unflag Top-Level Await.</p></td></tr> 4171cb0ef41Sopenharmony_ci<tr><td>v15.3.0, v14.17.0, v12.22.0</td> 4181cb0ef41Sopenharmony_ci<td><p>Stabilize modules implementation.</p></td></tr> 4191cb0ef41Sopenharmony_ci<tr><td>v14.13.0, v12.20.0</td> 4201cb0ef41Sopenharmony_ci<td><p>Support for detection of CommonJS named exports.</p></td></tr> 4211cb0ef41Sopenharmony_ci<tr><td>v14.0.0, v13.14.0, v12.20.0</td> 4221cb0ef41Sopenharmony_ci<td><p>Remove experimental modules warning.</p></td></tr> 4231cb0ef41Sopenharmony_ci<tr><td>v13.2.0, v12.17.0</td> 4241cb0ef41Sopenharmony_ci<td><p>Loading ECMAScript modules no longer requires a command-line flag.</p></td></tr> 4251cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 4261cb0ef41Sopenharmony_ci<td><p>Add support for ES modules using <code>.js</code> file extension via <code>package.json</code> <code>"type"</code> field.</p></td></tr> 4271cb0ef41Sopenharmony_ci<tr><td>v8.5.0</td> 4281cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.5.0</span></p></td></tr> 4291cb0ef41Sopenharmony_ci</tbody></table> 4301cb0ef41Sopenharmony_ci</details> 4311cb0ef41Sopenharmony_ci</div> 4321cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 4331cb0ef41Sopenharmony_ci<section><h3>Introduction<span><a class="mark" href="#introduction" id="introduction">#</a></span><a aria-hidden="true" class="legacy" id="esm_introduction"></a></h3> 4341cb0ef41Sopenharmony_ci 4351cb0ef41Sopenharmony_ci<p>ECMAScript modules are <a href="https://tc39.github.io/ecma262/#sec-modules">the official standard format</a> to package JavaScript 4361cb0ef41Sopenharmony_cicode for reuse. Modules are defined using a variety of <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import"><code>import</code></a> and 4371cb0ef41Sopenharmony_ci<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export"><code>export</code></a> statements.</p> 4381cb0ef41Sopenharmony_ci<p>The following example of an ES module exports a function:</p> 4391cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// addTwo.mjs</span> 4401cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">addTwo</span>(<span class="hljs-params">num</span>) { 4411cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> num + <span class="hljs-number">2</span>; 4421cb0ef41Sopenharmony_ci} 4431cb0ef41Sopenharmony_ci 4441cb0ef41Sopenharmony_ci<span class="hljs-keyword">export</span> { addTwo };</code> <button class="copy-button">copy</button></pre> 4451cb0ef41Sopenharmony_ci<p>The following example of an ES module imports the function from <code>addTwo.mjs</code>:</p> 4461cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// app.mjs</span> 4471cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { addTwo } <span class="hljs-keyword">from</span> <span class="hljs-string">'./addTwo.mjs'</span>; 4481cb0ef41Sopenharmony_ci 4491cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 6</span> 4501cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">addTwo</span>(<span class="hljs-number">4</span>));</code> <button class="copy-button">copy</button></pre> 4511cb0ef41Sopenharmony_ci<p>Node.js fully supports ECMAScript modules as they are currently specified and 4521cb0ef41Sopenharmony_ciprovides interoperability between them and its original module format, 4531cb0ef41Sopenharmony_ci<a href="modules.html">CommonJS</a>.</p> 4541cb0ef41Sopenharmony_ci<!-- Anchors to make sure old links find a target --> 4551cb0ef41Sopenharmony_ci<p><i id="esm_package_json_type_field"></i><i id="esm_package_scope_and_file_extensions"></i><i id="esm_input_type_flag"></i></p> 4561cb0ef41Sopenharmony_ci</section><section><h3>Enabling<span><a class="mark" href="#enabling" id="enabling">#</a></span><a aria-hidden="true" class="legacy" id="esm_enabling"></a></h3> 4571cb0ef41Sopenharmony_ci 4581cb0ef41Sopenharmony_ci<p>Node.js has two module systems: <a href="modules.html">CommonJS</a> modules and ECMAScript modules.</p> 4591cb0ef41Sopenharmony_ci<p>Authors can tell Node.js to use the ECMAScript modules loader via the <code>.mjs</code> 4601cb0ef41Sopenharmony_cifile extension, the <code>package.json</code> <a href="packages.html#type"><code>"type"</code></a> field, the <a href="cli.html#--input-typetype"><code>--input-type</code></a> 4611cb0ef41Sopenharmony_ciflag, or the <a href="cli.html#--experimental-default-typetype"><code>--experimental-default-type</code></a> flag. Outside of those cases, 4621cb0ef41Sopenharmony_ciNode.js will use the CommonJS module loader. See <a href="packages.html#determining-module-system">Determining module system</a> 4631cb0ef41Sopenharmony_cifor more details.</p> 4641cb0ef41Sopenharmony_ci<!-- Anchors to make sure old links find a target --> 4651cb0ef41Sopenharmony_ci<p><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></p> 4661cb0ef41Sopenharmony_ci</section><section><h3>Packages<span><a class="mark" href="#packages" id="packages">#</a></span><a aria-hidden="true" class="legacy" id="esm_packages"></a></h3> 4671cb0ef41Sopenharmony_ci<p>This section was moved to <a href="packages.html">Modules: Packages</a>.</p> 4681cb0ef41Sopenharmony_ci</section><section><h3><code>import</code> Specifiers<span><a class="mark" href="#import-specifiers" id="import-specifiers">#</a></span><a aria-hidden="true" class="legacy" id="esm_import_specifiers"></a></h3> 4691cb0ef41Sopenharmony_ci<h4>Terminology<span><a class="mark" href="#terminology" id="terminology">#</a></span><a aria-hidden="true" class="legacy" id="esm_terminology"></a></h4> 4701cb0ef41Sopenharmony_ci<p>The <em>specifier</em> of an <code>import</code> statement is the string after the <code>from</code> keyword, 4711cb0ef41Sopenharmony_cie.g. <code>'node:path'</code> in <code>import { sep } from 'node:path'</code>. Specifiers are also 4721cb0ef41Sopenharmony_ciused in <code>export from</code> statements, and as the argument to an <code>import()</code> 4731cb0ef41Sopenharmony_ciexpression.</p> 4741cb0ef41Sopenharmony_ci<p>There are three types of specifiers:</p> 4751cb0ef41Sopenharmony_ci<ul> 4761cb0ef41Sopenharmony_ci<li> 4771cb0ef41Sopenharmony_ci<p><em>Relative specifiers</em> like <code>'./startup.js'</code> or <code>'../config.mjs'</code>. They refer 4781cb0ef41Sopenharmony_cito a path relative to the location of the importing file. <em>The file extension 4791cb0ef41Sopenharmony_ciis always necessary for these.</em></p> 4801cb0ef41Sopenharmony_ci</li> 4811cb0ef41Sopenharmony_ci<li> 4821cb0ef41Sopenharmony_ci<p><em>Bare specifiers</em> like <code>'some-package'</code> or <code>'some-package/shuffle'</code>. They can 4831cb0ef41Sopenharmony_cirefer to the main entry point of a package by the package name, or a 4841cb0ef41Sopenharmony_cispecific feature module within a package prefixed by the package name as per 4851cb0ef41Sopenharmony_cithe examples respectively. <em>Including the file extension is only necessary 4861cb0ef41Sopenharmony_cifor packages without an <a href="packages.html#exports"><code>"exports"</code></a> field.</em></p> 4871cb0ef41Sopenharmony_ci</li> 4881cb0ef41Sopenharmony_ci<li> 4891cb0ef41Sopenharmony_ci<p><em>Absolute specifiers</em> like <code>'file:///opt/nodejs/config.js'</code>. They refer 4901cb0ef41Sopenharmony_cidirectly and explicitly to a full path.</p> 4911cb0ef41Sopenharmony_ci</li> 4921cb0ef41Sopenharmony_ci</ul> 4931cb0ef41Sopenharmony_ci<p>Bare specifier resolutions are handled by the <a href="#resolution-algorithm-specification">Node.js module 4941cb0ef41Sopenharmony_ciresolution and loading algorithm</a>. 4951cb0ef41Sopenharmony_ciAll other specifier resolutions are always only resolved with 4961cb0ef41Sopenharmony_cithe standard relative <a href="https://url.spec.whatwg.org/">URL</a> resolution semantics.</p> 4971cb0ef41Sopenharmony_ci<p>Like in CommonJS, module files within packages can be accessed by appending a 4981cb0ef41Sopenharmony_cipath to the package name unless the package's <a href="packages.html#nodejs-packagejson-field-definitions"><code>package.json</code></a> contains an 4991cb0ef41Sopenharmony_ci<a href="packages.html#exports"><code>"exports"</code></a> field, in which case files within packages can only be accessed 5001cb0ef41Sopenharmony_civia the paths defined in <a href="packages.html#exports"><code>"exports"</code></a>.</p> 5011cb0ef41Sopenharmony_ci<p>For details on these package resolution rules that apply to bare specifiers in 5021cb0ef41Sopenharmony_cithe Node.js module resolution, see the <a href="packages.html">packages documentation</a>.</p> 5031cb0ef41Sopenharmony_ci<h4>Mandatory file extensions<span><a class="mark" href="#mandatory-file-extensions" id="mandatory-file-extensions">#</a></span><a aria-hidden="true" class="legacy" id="esm_mandatory_file_extensions"></a></h4> 5041cb0ef41Sopenharmony_ci<p>A file extension must be provided when using the <code>import</code> keyword to resolve 5051cb0ef41Sopenharmony_cirelative or absolute specifiers. Directory indexes (e.g. <code>'./startup/index.js'</code>) 5061cb0ef41Sopenharmony_cimust also be fully specified.</p> 5071cb0ef41Sopenharmony_ci<p>This behavior matches how <code>import</code> behaves in browser environments, assuming a 5081cb0ef41Sopenharmony_citypically configured server.</p> 5091cb0ef41Sopenharmony_ci<h4>URLs<span><a class="mark" href="#urls" id="urls">#</a></span><a aria-hidden="true" class="legacy" id="esm_urls"></a></h4> 5101cb0ef41Sopenharmony_ci<p>ES modules are resolved and cached as URLs. This means that special characters 5111cb0ef41Sopenharmony_cimust be <a href="url.html#percent-encoding-in-urls">percent-encoded</a>, such as <code>#</code> with <code>%23</code> and <code>?</code> with <code>%3F</code>.</p> 5121cb0ef41Sopenharmony_ci<p><code>file:</code>, <code>node:</code>, and <code>data:</code> URL schemes are supported. A specifier like 5131cb0ef41Sopenharmony_ci<code>'https://example.com/app.js'</code> is not supported natively in Node.js unless using 5141cb0ef41Sopenharmony_cia <a href="module.html#import-from-https">custom HTTPS loader</a>.</p> 5151cb0ef41Sopenharmony_ci<h5><code>file:</code> URLs<span><a class="mark" href="#file-urls" id="file-urls">#</a></span><a aria-hidden="true" class="legacy" id="esm_file_urls"></a></h5> 5161cb0ef41Sopenharmony_ci<p>Modules are loaded multiple times if the <code>import</code> specifier used to resolve 5171cb0ef41Sopenharmony_cithem has a different query or fragment.</p> 5181cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'./foo.mjs?query=1'</span>; <span class="hljs-comment">// loads ./foo.mjs with query of "?query=1"</span> 5191cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> <span class="hljs-string">'./foo.mjs?query=2'</span>; <span class="hljs-comment">// loads ./foo.mjs with query of "?query=2"</span></code> <button class="copy-button">copy</button></pre> 5201cb0ef41Sopenharmony_ci<p>The volume root may be referenced via <code>/</code>, <code>//</code>, or <code>file:///</code>. Given the 5211cb0ef41Sopenharmony_cidifferences between <a href="https://url.spec.whatwg.org/">URL</a> and path resolution (such as percent encoding 5221cb0ef41Sopenharmony_cidetails), it is recommended to use <a href="url.html#urlpathtofileurlpath">url.pathToFileURL</a> when importing a path.</p> 5231cb0ef41Sopenharmony_ci<h5><code>data:</code> imports<span><a class="mark" href="#data-imports" id="data-imports">#</a></span><a aria-hidden="true" class="legacy" id="esm_data_imports"></a></h5> 5241cb0ef41Sopenharmony_ci<div class="api_metadata"> 5251cb0ef41Sopenharmony_ci<span>Added in: v12.10.0</span> 5261cb0ef41Sopenharmony_ci</div> 5271cb0ef41Sopenharmony_ci<p><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs"><code>data:</code> URLs</a> are supported for importing with the following MIME types:</p> 5281cb0ef41Sopenharmony_ci<ul> 5291cb0ef41Sopenharmony_ci<li><code>text/javascript</code> for ES modules</li> 5301cb0ef41Sopenharmony_ci<li><code>application/json</code> for JSON</li> 5311cb0ef41Sopenharmony_ci<li><code>application/wasm</code> for Wasm</li> 5321cb0ef41Sopenharmony_ci</ul> 5331cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'data:text/javascript,console.log("hello!");'</span>; 5341cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> _ <span class="hljs-keyword">from</span> <span class="hljs-string">'data:application/json,"world!"'</span> <span class="hljs-keyword">with</span> { <span class="hljs-attr">type</span>: <span class="hljs-string">'json'</span> };</code> <button class="copy-button">copy</button></pre> 5351cb0ef41Sopenharmony_ci<p><code>data:</code> URLs only resolve <a href="#terminology">bare specifiers</a> for builtin modules 5361cb0ef41Sopenharmony_ciand <a href="#terminology">absolute specifiers</a>. Resolving 5371cb0ef41Sopenharmony_ci<a href="#terminology">relative specifiers</a> does not work because <code>data:</code> is not a 5381cb0ef41Sopenharmony_ci<a href="https://url.spec.whatwg.org/#special-scheme">special scheme</a>. For example, attempting to load <code>./foo</code> 5391cb0ef41Sopenharmony_cifrom <code>data:text/javascript,import "./foo";</code> fails to resolve because there 5401cb0ef41Sopenharmony_ciis no concept of relative resolution for <code>data:</code> URLs.</p> 5411cb0ef41Sopenharmony_ci<h5><code>node:</code> imports<span><a class="mark" href="#node-imports" id="node-imports">#</a></span><a aria-hidden="true" class="legacy" id="esm_node_imports"></a></h5> 5421cb0ef41Sopenharmony_ci<div class="api_metadata"> 5431cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 5441cb0ef41Sopenharmony_ci<table> 5451cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 5461cb0ef41Sopenharmony_ci<tr><td>v16.0.0, v14.18.0</td> 5471cb0ef41Sopenharmony_ci<td><p>Added <code>node:</code> import support to <code>require(...)</code>.</p></td></tr> 5481cb0ef41Sopenharmony_ci<tr><td>v14.13.1, v12.20.0</td> 5491cb0ef41Sopenharmony_ci<td><p><span>Added in: v14.13.1, v12.20.0</span></p></td></tr> 5501cb0ef41Sopenharmony_ci</tbody></table> 5511cb0ef41Sopenharmony_ci</details> 5521cb0ef41Sopenharmony_ci</div> 5531cb0ef41Sopenharmony_ci<p><code>node:</code> URLs are supported as an alternative means to load Node.js builtin 5541cb0ef41Sopenharmony_cimodules. This URL scheme allows for builtin modules to be referenced by valid 5551cb0ef41Sopenharmony_ciabsolute URL strings.</p> 5561cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">import</span> fs <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;</code> <button class="copy-button">copy</button></pre> 5571cb0ef41Sopenharmony_ci<p><a id="import-assertions"></a></p> 5581cb0ef41Sopenharmony_ci</section><section><h3>Import attributes<span><a class="mark" href="#import-attributes" id="import-attributes">#</a></span><a aria-hidden="true" class="legacy" id="esm_import_attributes"></a></h3> 5591cb0ef41Sopenharmony_ci<div class="api_metadata"> 5601cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 5611cb0ef41Sopenharmony_ci<table> 5621cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 5631cb0ef41Sopenharmony_ci<tr><td>v18.20.0</td> 5641cb0ef41Sopenharmony_ci<td><p>Switch from Import Assertions to Import Attributes.</p></td></tr> 5651cb0ef41Sopenharmony_ci<tr><td>v17.1.0, v16.14.0</td> 5661cb0ef41Sopenharmony_ci<td><p><span>Added in: v17.1.0, v16.14.0</span></p></td></tr> 5671cb0ef41Sopenharmony_ci</tbody></table> 5681cb0ef41Sopenharmony_ci</details> 5691cb0ef41Sopenharmony_ci</div> 5701cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a>.1 - Active development</div><p></p> 5711cb0ef41Sopenharmony_ci<blockquote> 5721cb0ef41Sopenharmony_ci<p>This feature was previously named "Import assertions", and using the <code>assert</code> 5731cb0ef41Sopenharmony_cikeyword instead of <code>with</code>. Any uses in code of the prior <code>assert</code> keyword 5741cb0ef41Sopenharmony_cishould be updated to use <code>with</code> instead.</p> 5751cb0ef41Sopenharmony_ci</blockquote> 5761cb0ef41Sopenharmony_ci<p>The <a href="https://github.com/tc39/proposal-import-attributes">Import Attributes proposal</a> adds an inline syntax for module import 5771cb0ef41Sopenharmony_cistatements to pass on more information alongside the module specifier.</p> 5781cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">import</span> fooData <span class="hljs-keyword">from</span> <span class="hljs-string">'./foo.json'</span> <span class="hljs-keyword">with</span> { <span class="hljs-attr">type</span>: <span class="hljs-string">'json'</span> }; 5791cb0ef41Sopenharmony_ci 5801cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { <span class="hljs-attr">default</span>: barData } = 5811cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'./bar.json'</span>, { <span class="hljs-attr">with</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'json'</span> } });</code> <button class="copy-button">copy</button></pre> 5821cb0ef41Sopenharmony_ci<p>Node.js supports the following <code>type</code> values, for which the attribute is 5831cb0ef41Sopenharmony_cimandatory:</p> 5841cb0ef41Sopenharmony_ci 5851cb0ef41Sopenharmony_ci 5861cb0ef41Sopenharmony_ci 5871cb0ef41Sopenharmony_ci 5881cb0ef41Sopenharmony_ci 5891cb0ef41Sopenharmony_ci 5901cb0ef41Sopenharmony_ci 5911cb0ef41Sopenharmony_ci 5921cb0ef41Sopenharmony_ci 5931cb0ef41Sopenharmony_ci 5941cb0ef41Sopenharmony_ci 5951cb0ef41Sopenharmony_ci 5961cb0ef41Sopenharmony_ci 5971cb0ef41Sopenharmony_ci<table><thead><tr><th>Attribute <code>type</code></th><th>Needed for</th></tr></thead><tbody><tr><td><code>'json'</code></td><td><a href="#json-modules">JSON modules</a></td></tr></tbody></table> 5981cb0ef41Sopenharmony_ci</section><section><h3>Builtin modules<span><a class="mark" href="#builtin-modules" id="builtin-modules">#</a></span><a aria-hidden="true" class="legacy" id="esm_builtin_modules"></a></h3> 5991cb0ef41Sopenharmony_ci<p><a href="modules.html#core-modules">Core modules</a> provide named exports of their public API. A 6001cb0ef41Sopenharmony_cidefault export is also provided which is the value of the CommonJS exports. 6011cb0ef41Sopenharmony_ciThe default export can be used for, among other things, modifying the named 6021cb0ef41Sopenharmony_ciexports. Named exports of builtin modules are updated only by calling 6031cb0ef41Sopenharmony_ci<a href="module.html#modulesyncbuiltinesmexports"><code>module.syncBuiltinESMExports()</code></a>.</p> 6041cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-title class_">EventEmitter</span> <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 6051cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> e = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();</code> <button class="copy-button">copy</button></pre> 6061cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 6071cb0ef41Sopenharmony_ci<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'./foo.txt'</span>, <span class="hljs-function">(<span class="hljs-params">err, source</span>) =></span> { 6081cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) { 6091cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 6101cb0ef41Sopenharmony_ci } <span class="hljs-keyword">else</span> { 6111cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(source); 6121cb0ef41Sopenharmony_ci } 6131cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 6141cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">import</span> fs, { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 6151cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { syncBuiltinESMExports } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:module'</span>; 6161cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 6171cb0ef41Sopenharmony_ci 6181cb0ef41Sopenharmony_cifs.<span class="hljs-property">readFileSync</span> = <span class="hljs-function">() =></span> <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'Hello, ESM'</span>); 6191cb0ef41Sopenharmony_ci<span class="hljs-title function_">syncBuiltinESMExports</span>(); 6201cb0ef41Sopenharmony_ci 6211cb0ef41Sopenharmony_cifs.<span class="hljs-property">readFileSync</span> === readFileSync;</code> <button class="copy-button">copy</button></pre> 6221cb0ef41Sopenharmony_ci</section><section><h3><code>import()</code> expressions<span><a class="mark" href="#import-expressions" id="import-expressions">#</a></span><a aria-hidden="true" class="legacy" id="esm_import_expressions"></a></h3> 6231cb0ef41Sopenharmony_ci<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import">Dynamic <code>import()</code></a> is supported in both CommonJS and ES modules. In CommonJS 6241cb0ef41Sopenharmony_cimodules it can be used to load ES modules.</p> 6251cb0ef41Sopenharmony_ci</section><section><h3><code>import.meta</code><span><a class="mark" href="#importmeta" id="importmeta">#</a></span><a aria-hidden="true" class="legacy" id="esm_import_meta"></a></h3> 6261cb0ef41Sopenharmony_ci<ul> 6271cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 6281cb0ef41Sopenharmony_ci</ul> 6291cb0ef41Sopenharmony_ci<p>The <code>import.meta</code> meta property is an <code>Object</code> that contains the following 6301cb0ef41Sopenharmony_ciproperties.</p> 6311cb0ef41Sopenharmony_ci<h4><code>import.meta.url</code><span><a class="mark" href="#importmetaurl" id="importmetaurl">#</a></span><a aria-hidden="true" class="legacy" id="esm_import_meta_url"></a></h4> 6321cb0ef41Sopenharmony_ci<ul> 6331cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The absolute <code>file:</code> URL of the module.</li> 6341cb0ef41Sopenharmony_ci</ul> 6351cb0ef41Sopenharmony_ci<p>This is defined exactly the same as it is in browsers providing the URL of the 6361cb0ef41Sopenharmony_cicurrent module file.</p> 6371cb0ef41Sopenharmony_ci<p>This enables useful patterns such as relative file loading:</p> 6381cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 6391cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> buffer = <span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'./data.proto'</span>, <span class="hljs-keyword">import</span>.<span class="hljs-property">meta</span>.<span class="hljs-property">url</span>));</code> <button class="copy-button">copy</button></pre> 6401cb0ef41Sopenharmony_ci<h4><code>import.meta.resolve(specifier)</code><span><a class="mark" href="#importmetaresolvespecifier" id="importmetaresolvespecifier">#</a></span><a aria-hidden="true" class="legacy" id="esm_import_meta_resolve_specifier"></a></h4> 6411cb0ef41Sopenharmony_ci<div class="api_metadata"> 6421cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 6431cb0ef41Sopenharmony_ci<table> 6441cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 6451cb0ef41Sopenharmony_ci<tr><td>v18.19.0</td> 6461cb0ef41Sopenharmony_ci<td><p>Unflag <code>import.meta.resolve``, with </code>parentURL` parameter still flagged.</p></td></tr> 6471cb0ef41Sopenharmony_ci<tr><td>v18.19.0</td> 6481cb0ef41Sopenharmony_ci<td><p>This API no longer throws when targeting <code>file:</code> URLs that do not map to an existing file on the local FS.</p></td></tr> 6491cb0ef41Sopenharmony_ci<tr><td>v18.19.0</td> 6501cb0ef41Sopenharmony_ci<td><p>This API now returns a string synchronously instead of a Promise.</p></td></tr> 6511cb0ef41Sopenharmony_ci<tr><td>v16.2.0, v14.18.0</td> 6521cb0ef41Sopenharmony_ci<td><p>Add support for WHATWG <code>URL</code> object to <code>parentURL</code> parameter.</p></td></tr> 6531cb0ef41Sopenharmony_ci<tr><td>v13.9.0, v12.16.2</td> 6541cb0ef41Sopenharmony_ci<td><p><span>Added in: v13.9.0, v12.16.2</span></p></td></tr> 6551cb0ef41Sopenharmony_ci</tbody></table> 6561cb0ef41Sopenharmony_ci</details> 6571cb0ef41Sopenharmony_ci</div> 6581cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a>.2 - Release candidate</div><p></p> 6591cb0ef41Sopenharmony_ci<ul> 6601cb0ef41Sopenharmony_ci<li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The module specifier to resolve relative to the 6611cb0ef41Sopenharmony_cicurrent module.</li> 6621cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The absolute URL string that the specifier would resolve to.</li> 6631cb0ef41Sopenharmony_ci</ul> 6641cb0ef41Sopenharmony_ci<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve"><code>import.meta.resolve</code></a> is a module-relative resolution function scoped to 6651cb0ef41Sopenharmony_cieach module, returning the URL string.</p> 6661cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> dependencyAsset = <span class="hljs-keyword">import</span>.<span class="hljs-property">meta</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'component-lib/asset.css'</span>); 6671cb0ef41Sopenharmony_ci<span class="hljs-comment">// file:///app/node_modules/component-lib/asset.css</span> 6681cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span>.<span class="hljs-property">meta</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'./dep.js'</span>); 6691cb0ef41Sopenharmony_ci<span class="hljs-comment">// file:///app/dep.js</span></code> <button class="copy-button">copy</button></pre> 6701cb0ef41Sopenharmony_ci<p>All features of the Node.js module resolution are supported. Dependency 6711cb0ef41Sopenharmony_ciresolutions are subject to the permitted exports resolutions within the package.</p> 6721cb0ef41Sopenharmony_ci<p><strong>Caveats</strong>:</p> 6731cb0ef41Sopenharmony_ci<ul> 6741cb0ef41Sopenharmony_ci<li>This can result in synchronous file-system operations, which 6751cb0ef41Sopenharmony_cican impact performance similarly to <code>require.resolve</code>.</li> 6761cb0ef41Sopenharmony_ci<li>This feature is not available within custom loaders (it would 6771cb0ef41Sopenharmony_cicreate a deadlock).</li> 6781cb0ef41Sopenharmony_ci</ul> 6791cb0ef41Sopenharmony_ci<p><strong>Non-standard API</strong>:</p> 6801cb0ef41Sopenharmony_ci<p>When using the <code>--experimental-import-meta-resolve</code> flag, that function accepts 6811cb0ef41Sopenharmony_cia second argument:</p> 6821cb0ef41Sopenharmony_ci<ul> 6831cb0ef41Sopenharmony_ci<li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> An optional absolute parent module URL to resolve from. 6841cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>import.meta.url</code></li> 6851cb0ef41Sopenharmony_ci</ul> 6861cb0ef41Sopenharmony_ci</section><section><h3>Interoperability with CommonJS<span><a class="mark" href="#interoperability-with-commonjs" id="interoperability-with-commonjs">#</a></span><a aria-hidden="true" class="legacy" id="esm_interoperability_with_commonjs"></a></h3> 6871cb0ef41Sopenharmony_ci<h4><code>import</code> statements<span><a class="mark" href="#import-statements" id="import-statements">#</a></span><a aria-hidden="true" class="legacy" id="esm_import_statements"></a></h4> 6881cb0ef41Sopenharmony_ci<p>An <code>import</code> statement can reference an ES module or a CommonJS module. 6891cb0ef41Sopenharmony_ci<code>import</code> statements are permitted only in ES modules, but dynamic <a href="#import-expressions"><code>import()</code></a> 6901cb0ef41Sopenharmony_ciexpressions are supported in CommonJS for loading ES modules.</p> 6911cb0ef41Sopenharmony_ci<p>When importing <a href="#commonjs-namespaces">CommonJS modules</a>, the 6921cb0ef41Sopenharmony_ci<code>module.exports</code> object is provided as the default export. Named exports may be 6931cb0ef41Sopenharmony_ciavailable, provided by static analysis as a convenience for better ecosystem 6941cb0ef41Sopenharmony_cicompatibility.</p> 6951cb0ef41Sopenharmony_ci<h4><code>require</code><span><a class="mark" href="#require" id="require">#</a></span><a aria-hidden="true" class="legacy" id="esm_require"></a></h4> 6961cb0ef41Sopenharmony_ci<p>The CommonJS module <code>require</code> always treats the files it references as CommonJS.</p> 6971cb0ef41Sopenharmony_ci<p>Using <code>require</code> to load an ES module is not supported because ES modules have 6981cb0ef41Sopenharmony_ciasynchronous execution. Instead, use <a href="#import-expressions"><code>import()</code></a> to load an ES module 6991cb0ef41Sopenharmony_cifrom a CommonJS module.</p> 7001cb0ef41Sopenharmony_ci<h4>CommonJS Namespaces<span><a class="mark" href="#commonjs-namespaces" id="commonjs-namespaces">#</a></span><a aria-hidden="true" class="legacy" id="esm_commonjs_namespaces"></a></h4> 7011cb0ef41Sopenharmony_ci<p>CommonJS modules consist of a <code>module.exports</code> object which can be of any type.</p> 7021cb0ef41Sopenharmony_ci<p>When importing a CommonJS module, it can be reliably imported using the ES 7031cb0ef41Sopenharmony_cimodule default import or its corresponding sugar syntax:</p> 7041cb0ef41Sopenharmony_ci<!-- eslint-disable no-duplicate-imports --> 7051cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">import</span> { <span class="hljs-keyword">default</span> <span class="hljs-keyword">as</span> cjs } <span class="hljs-keyword">from</span> <span class="hljs-string">'cjs'</span>; 7061cb0ef41Sopenharmony_ci 7071cb0ef41Sopenharmony_ci<span class="hljs-comment">// The following import statement is "syntax sugar" (equivalent but sweeter)</span> 7081cb0ef41Sopenharmony_ci<span class="hljs-comment">// for `{ default as cjsSugar }` in the above import statement:</span> 7091cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> cjsSugar <span class="hljs-keyword">from</span> <span class="hljs-string">'cjs'</span>; 7101cb0ef41Sopenharmony_ci 7111cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(cjs); 7121cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(cjs === cjsSugar); 7131cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 7141cb0ef41Sopenharmony_ci<span class="hljs-comment">// <module.exports></span> 7151cb0ef41Sopenharmony_ci<span class="hljs-comment">// true</span></code> <button class="copy-button">copy</button></pre> 7161cb0ef41Sopenharmony_ci<p>The ECMAScript Module Namespace representation of a CommonJS module is always 7171cb0ef41Sopenharmony_cia namespace with a <code>default</code> export key pointing to the CommonJS 7181cb0ef41Sopenharmony_ci<code>module.exports</code> value.</p> 7191cb0ef41Sopenharmony_ci<p>This Module Namespace Exotic Object can be directly observed either when using 7201cb0ef41Sopenharmony_ci<code>import * as m from 'cjs'</code> or a dynamic import:</p> 7211cb0ef41Sopenharmony_ci<!-- eslint-skip --> 7221cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> m <span class="hljs-keyword">from</span> <span class="hljs-string">'cjs'</span>; 7231cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(m); 7241cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(m === <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'cjs'</span>)); 7251cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 7261cb0ef41Sopenharmony_ci<span class="hljs-comment">// [Module] { default: <module.exports> }</span> 7271cb0ef41Sopenharmony_ci<span class="hljs-comment">// true</span></code> <button class="copy-button">copy</button></pre> 7281cb0ef41Sopenharmony_ci<p>For better compatibility with existing usage in the JS ecosystem, Node.js 7291cb0ef41Sopenharmony_ciin addition attempts to determine the CommonJS named exports of every imported 7301cb0ef41Sopenharmony_ciCommonJS module to provide them as separate ES module exports using a static 7311cb0ef41Sopenharmony_cianalysis process.</p> 7321cb0ef41Sopenharmony_ci<p>For example, consider a CommonJS module written:</p> 7331cb0ef41Sopenharmony_ci<pre><code class="language-js cjs"><span class="hljs-comment">// cjs.cjs</span> 7341cb0ef41Sopenharmony_ci<span class="hljs-built_in">exports</span>.<span class="hljs-property">name</span> = <span class="hljs-string">'exported'</span>;</code> <button class="copy-button">copy</button></pre> 7351cb0ef41Sopenharmony_ci<p>The preceding module supports named imports in ES modules:</p> 7361cb0ef41Sopenharmony_ci<!-- eslint-disable no-duplicate-imports --> 7371cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">import</span> { name } <span class="hljs-keyword">from</span> <span class="hljs-string">'./cjs.cjs'</span>; 7381cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name); 7391cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 'exported'</span> 7401cb0ef41Sopenharmony_ci 7411cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> cjs <span class="hljs-keyword">from</span> <span class="hljs-string">'./cjs.cjs'</span>; 7421cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(cjs); 7431cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: { name: 'exported' }</span> 7441cb0ef41Sopenharmony_ci 7451cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> m <span class="hljs-keyword">from</span> <span class="hljs-string">'./cjs.cjs'</span>; 7461cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(m); 7471cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: [Module] { default: { name: 'exported' }, name: 'exported' }</span></code> <button class="copy-button">copy</button></pre> 7481cb0ef41Sopenharmony_ci<p>As can be seen from the last example of the Module Namespace Exotic Object being 7491cb0ef41Sopenharmony_cilogged, the <code>name</code> export is copied off of the <code>module.exports</code> object and set 7501cb0ef41Sopenharmony_cidirectly on the ES module namespace when the module is imported.</p> 7511cb0ef41Sopenharmony_ci<p>Live binding updates or new exports added to <code>module.exports</code> are not detected 7521cb0ef41Sopenharmony_cifor these named exports.</p> 7531cb0ef41Sopenharmony_ci<p>The detection of named exports is based on common syntax patterns but does not 7541cb0ef41Sopenharmony_cialways correctly detect named exports. In these cases, using the default 7551cb0ef41Sopenharmony_ciimport form described above can be a better option.</p> 7561cb0ef41Sopenharmony_ci<p>Named exports detection covers many common export patterns, reexport patterns 7571cb0ef41Sopenharmony_ciand build tool and transpiler outputs. See <a href="https://github.com/nodejs/cjs-module-lexer/tree/1.2.2">cjs-module-lexer</a> for the exact 7581cb0ef41Sopenharmony_cisemantics implemented.</p> 7591cb0ef41Sopenharmony_ci<h4>Differences between ES modules and CommonJS<span><a class="mark" href="#differences-between-es-modules-and-commonjs" id="differences-between-es-modules-and-commonjs">#</a></span><a aria-hidden="true" class="legacy" id="esm_differences_between_es_modules_and_commonjs"></a></h4> 7601cb0ef41Sopenharmony_ci<h5>No <code>require</code>, <code>exports</code>, or <code>module.exports</code><span><a class="mark" href="#no-require-exports-or-moduleexports" id="no-require-exports-or-moduleexports">#</a></span><a aria-hidden="true" class="legacy" id="esm_no_require_exports_or_module_exports"></a></h5> 7611cb0ef41Sopenharmony_ci<p>In most cases, the ES module <code>import</code> can be used to load CommonJS modules.</p> 7621cb0ef41Sopenharmony_ci<p>If needed, a <code>require</code> function can be constructed within an ES module using 7631cb0ef41Sopenharmony_ci<a href="module.html#modulecreaterequirefilename"><code>module.createRequire()</code></a>.</p> 7641cb0ef41Sopenharmony_ci<h5>No <code>__filename</code> or <code>__dirname</code><span><a class="mark" href="#no-__filename-or-__dirname" id="no-__filename-or-__dirname">#</a></span><a aria-hidden="true" class="legacy" id="esm_no_filename_or_dirname"></a></h5> 7651cb0ef41Sopenharmony_ci<p>These CommonJS variables are not available in ES modules.</p> 7661cb0ef41Sopenharmony_ci<p><code>__filename</code> and <code>__dirname</code> use cases can be replicated via 7671cb0ef41Sopenharmony_ci<a href="#importmetaurl"><code>import.meta.url</code></a>.</p> 7681cb0ef41Sopenharmony_ci<h5>No Addon Loading<span><a class="mark" href="#no-addon-loading" id="no-addon-loading">#</a></span><a aria-hidden="true" class="legacy" id="esm_no_addon_loading"></a></h5> 7691cb0ef41Sopenharmony_ci<p><a href="addons.html">Addons</a> are not currently supported with ES module imports.</p> 7701cb0ef41Sopenharmony_ci<p>They can instead be loaded with <a href="module.html#modulecreaterequirefilename"><code>module.createRequire()</code></a> or 7711cb0ef41Sopenharmony_ci<a href="process.html#processdlopenmodule-filename-flags"><code>process.dlopen</code></a>.</p> 7721cb0ef41Sopenharmony_ci<h5>No <code>require.resolve</code><span><a class="mark" href="#no-requireresolve" id="no-requireresolve">#</a></span><a aria-hidden="true" class="legacy" id="esm_no_require_resolve"></a></h5> 7731cb0ef41Sopenharmony_ci<p>Relative resolution can be handled via <code>new URL('./local', import.meta.url)</code>.</p> 7741cb0ef41Sopenharmony_ci<p>For a complete <code>require.resolve</code> replacement, there is the 7751cb0ef41Sopenharmony_ci<a href="#importmetaresolvespecifier">import.meta.resolve</a> API.</p> 7761cb0ef41Sopenharmony_ci<p>Alternatively <code>module.createRequire()</code> can be used.</p> 7771cb0ef41Sopenharmony_ci<h5>No <code>NODE_PATH</code><span><a class="mark" href="#no-node_path" id="no-node_path">#</a></span><a aria-hidden="true" class="legacy" id="esm_no_node_path"></a></h5> 7781cb0ef41Sopenharmony_ci<p><code>NODE_PATH</code> is not part of resolving <code>import</code> specifiers. Please use symlinks 7791cb0ef41Sopenharmony_ciif this behavior is desired.</p> 7801cb0ef41Sopenharmony_ci<h5>No <code>require.extensions</code><span><a class="mark" href="#no-requireextensions" id="no-requireextensions">#</a></span><a aria-hidden="true" class="legacy" id="esm_no_require_extensions"></a></h5> 7811cb0ef41Sopenharmony_ci<p><code>require.extensions</code> is not used by <code>import</code>. Module customization hooks can 7821cb0ef41Sopenharmony_ciprovide a replacement.</p> 7831cb0ef41Sopenharmony_ci<h5>No <code>require.cache</code><span><a class="mark" href="#no-requirecache" id="no-requirecache">#</a></span><a aria-hidden="true" class="legacy" id="esm_no_require_cache"></a></h5> 7841cb0ef41Sopenharmony_ci<p><code>require.cache</code> is not used by <code>import</code> as the ES module loader has its own 7851cb0ef41Sopenharmony_ciseparate cache.</p> 7861cb0ef41Sopenharmony_ci<p><i id="esm_experimental_json_modules"></i></p> 7871cb0ef41Sopenharmony_ci</section><section><h3>JSON modules<span><a class="mark" href="#json-modules" id="json-modules">#</a></span><a aria-hidden="true" class="legacy" id="esm_json_modules"></a></h3> 7881cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 7891cb0ef41Sopenharmony_ci<p>JSON files can be referenced by <code>import</code>:</p> 7901cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">import</span> packageConfig <span class="hljs-keyword">from</span> <span class="hljs-string">'./package.json'</span> <span class="hljs-keyword">with</span> { <span class="hljs-attr">type</span>: <span class="hljs-string">'json'</span> };</code> <button class="copy-button">copy</button></pre> 7911cb0ef41Sopenharmony_ci<p>The <code>with { type: 'json' }</code> syntax is mandatory; see <a href="#import-attributes">Import Attributes</a>.</p> 7921cb0ef41Sopenharmony_ci<p>The imported JSON only exposes a <code>default</code> export. There is no support for named 7931cb0ef41Sopenharmony_ciexports. A cache entry is created in the CommonJS cache to avoid duplication. 7941cb0ef41Sopenharmony_ciThe same object is returned in CommonJS if the JSON module has already been 7951cb0ef41Sopenharmony_ciimported from the same path.</p> 7961cb0ef41Sopenharmony_ci<p><i id="esm_experimental_wasm_modules"></i></p> 7971cb0ef41Sopenharmony_ci</section><section><h3>Wasm modules<span><a class="mark" href="#wasm-modules" id="wasm-modules">#</a></span><a aria-hidden="true" class="legacy" id="esm_wasm_modules"></a></h3> 7981cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 7991cb0ef41Sopenharmony_ci<p>Importing WebAssembly modules is supported under the 8001cb0ef41Sopenharmony_ci<code>--experimental-wasm-modules</code> flag, allowing any <code>.wasm</code> files to be 8011cb0ef41Sopenharmony_ciimported as normal modules while also supporting their module imports.</p> 8021cb0ef41Sopenharmony_ci<p>This integration is in line with the 8031cb0ef41Sopenharmony_ci<a href="https://github.com/webassembly/esm-integration">ES Module Integration Proposal for WebAssembly</a>.</p> 8041cb0ef41Sopenharmony_ci<p>For example, an <code>index.mjs</code> containing:</p> 8051cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> M <span class="hljs-keyword">from</span> <span class="hljs-string">'./module.wasm'</span>; 8061cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(M);</code> <button class="copy-button">copy</button></pre> 8071cb0ef41Sopenharmony_ci<p>executed under:</p> 8081cb0ef41Sopenharmony_ci<pre><code class="language-bash">node --experimental-wasm-modules index.mjs</code> <button class="copy-button">copy</button></pre> 8091cb0ef41Sopenharmony_ci<p>would provide the exports interface for the instantiation of <code>module.wasm</code>.</p> 8101cb0ef41Sopenharmony_ci<p><i id="esm_experimental_top_level_await"></i></p> 8111cb0ef41Sopenharmony_ci</section><section><h3>Top-level <code>await</code><span><a class="mark" href="#top-level-await" id="top-level-await">#</a></span><a aria-hidden="true" class="legacy" id="esm_top_level_await"></a></h3> 8121cb0ef41Sopenharmony_ci<div class="api_metadata"> 8131cb0ef41Sopenharmony_ci<span>Added in: v14.8.0</span> 8141cb0ef41Sopenharmony_ci</div> 8151cb0ef41Sopenharmony_ci<p>The <code>await</code> keyword may be used in the top level body of an ECMAScript module.</p> 8161cb0ef41Sopenharmony_ci<p>Assuming an <code>a.mjs</code> with</p> 8171cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> five = <span class="hljs-keyword">await</span> <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-number">5</span>);</code> <button class="copy-button">copy</button></pre> 8181cb0ef41Sopenharmony_ci<p>And a <code>b.mjs</code> with</p> 8191cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">import</span> { five } <span class="hljs-keyword">from</span> <span class="hljs-string">'./a.mjs'</span>; 8201cb0ef41Sopenharmony_ci 8211cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(five); <span class="hljs-comment">// Logs `5`</span></code> <button class="copy-button">copy</button></pre> 8221cb0ef41Sopenharmony_ci<pre><code class="language-bash">node b.mjs <span class="hljs-comment"># works</span></code> <button class="copy-button">copy</button></pre> 8231cb0ef41Sopenharmony_ci<p>If a top level <code>await</code> expression never resolves, the <code>node</code> process will exit 8241cb0ef41Sopenharmony_ciwith a <code>13</code> <a href="process.html#exit-codes">status code</a>.</p> 8251cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">import</span> { spawn } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:child_process'</span>; 8261cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { execPath } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 8271cb0ef41Sopenharmony_ci 8281cb0ef41Sopenharmony_ci<span class="hljs-title function_">spawn</span>(execPath, [ 8291cb0ef41Sopenharmony_ci <span class="hljs-string">'--input-type=module'</span>, 8301cb0ef41Sopenharmony_ci <span class="hljs-string">'--eval'</span>, 8311cb0ef41Sopenharmony_ci <span class="hljs-comment">// Never-resolving Promise:</span> 8321cb0ef41Sopenharmony_ci <span class="hljs-string">'await new Promise(() => {})'</span>, 8331cb0ef41Sopenharmony_ci]).<span class="hljs-title function_">once</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> { 8341cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(code); <span class="hljs-comment">// Logs `13`</span> 8351cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 8361cb0ef41Sopenharmony_ci</section><section><h3>HTTPS and HTTP imports<span><a class="mark" href="#https-and-http-imports" id="https-and-http-imports">#</a></span><a aria-hidden="true" class="legacy" id="esm_https_and_http_imports"></a></h3> 8371cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 8381cb0ef41Sopenharmony_ci<p>Importing network based modules using <code>https:</code> and <code>http:</code> is supported under 8391cb0ef41Sopenharmony_cithe <code>--experimental-network-imports</code> flag. This allows web browser-like imports 8401cb0ef41Sopenharmony_cito work in Node.js with a few differences due to application stability and 8411cb0ef41Sopenharmony_cisecurity concerns that are different when running in a privileged environment 8421cb0ef41Sopenharmony_ciinstead of a browser sandbox.</p> 8431cb0ef41Sopenharmony_ci<h4>Imports are limited to HTTP/1<span><a class="mark" href="#imports-are-limited-to-http1" id="imports-are-limited-to-http1">#</a></span><a aria-hidden="true" class="legacy" id="esm_imports_are_limited_to_http_1"></a></h4> 8441cb0ef41Sopenharmony_ci<p>Automatic protocol negotiation for HTTP/2 and HTTP/3 is not yet supported.</p> 8451cb0ef41Sopenharmony_ci<h4>HTTP is limited to loopback addresses<span><a class="mark" href="#http-is-limited-to-loopback-addresses" id="http-is-limited-to-loopback-addresses">#</a></span><a aria-hidden="true" class="legacy" id="esm_http_is_limited_to_loopback_addresses"></a></h4> 8461cb0ef41Sopenharmony_ci<p><code>http:</code> is vulnerable to man-in-the-middle attacks and is not allowed to be 8471cb0ef41Sopenharmony_ciused for addresses outside of the IPv4 address <code>127.0.0.0/8</code> (<code>127.0.0.1</code> to 8481cb0ef41Sopenharmony_ci<code>127.255.255.255</code>) and the IPv6 address <code>::1</code>. Support for <code>http:</code> is intended 8491cb0ef41Sopenharmony_cito be used for local development.</p> 8501cb0ef41Sopenharmony_ci<h4>Authentication is never sent to the destination server.<span><a class="mark" href="#authentication-is-never-sent-to-the-destination-server" id="authentication-is-never-sent-to-the-destination-server">#</a></span><a aria-hidden="true" class="legacy" id="esm_authentication_is_never_sent_to_the_destination_server"></a></h4> 8511cb0ef41Sopenharmony_ci<p><code>Authorization</code>, <code>Cookie</code>, and <code>Proxy-Authorization</code> headers are not sent to the 8521cb0ef41Sopenharmony_ciserver. Avoid including user info in parts of imported URLs. A security model 8531cb0ef41Sopenharmony_cifor safely using these on the server is being worked on.</p> 8541cb0ef41Sopenharmony_ci<h4>CORS is never checked on the destination server<span><a class="mark" href="#cors-is-never-checked-on-the-destination-server" id="cors-is-never-checked-on-the-destination-server">#</a></span><a aria-hidden="true" class="legacy" id="esm_cors_is_never_checked_on_the_destination_server"></a></h4> 8551cb0ef41Sopenharmony_ci<p>CORS is designed to allow a server to limit the consumers of an API to a 8561cb0ef41Sopenharmony_cispecific set of hosts. This is not supported as it does not make sense for a 8571cb0ef41Sopenharmony_ciserver-based implementation.</p> 8581cb0ef41Sopenharmony_ci<h4>Cannot load non-network dependencies<span><a class="mark" href="#cannot-load-non-network-dependencies" id="cannot-load-non-network-dependencies">#</a></span><a aria-hidden="true" class="legacy" id="esm_cannot_load_non_network_dependencies"></a></h4> 8591cb0ef41Sopenharmony_ci<p>These modules cannot access other modules that are not over <code>http:</code> or <code>https:</code>. 8601cb0ef41Sopenharmony_ciTo still access local modules while avoiding the security concern, pass in 8611cb0ef41Sopenharmony_cireferences to the local dependencies:</p> 8621cb0ef41Sopenharmony_ci<pre><code class="language-js mjs"><span class="hljs-comment">// file.mjs</span> 8631cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> worker_threads <span class="hljs-keyword">from</span> <span class="hljs-string">'node:worker_threads'</span>; 8641cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { configure, resize } <span class="hljs-keyword">from</span> <span class="hljs-string">'https://example.com/imagelib.mjs'</span>; 8651cb0ef41Sopenharmony_ci<span class="hljs-title function_">configure</span>({ worker_threads });</code> <button class="copy-button">copy</button></pre> 8661cb0ef41Sopenharmony_ci<pre><code class="language-js mjs"><span class="hljs-comment">// https://example.com/imagelib.mjs</span> 8671cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> worker_threads; 8681cb0ef41Sopenharmony_ci<span class="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">configure</span>(<span class="hljs-params">opts</span>) { 8691cb0ef41Sopenharmony_ci worker_threads = opts.<span class="hljs-property">worker_threads</span>; 8701cb0ef41Sopenharmony_ci} 8711cb0ef41Sopenharmony_ci<span class="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">resize</span>(<span class="hljs-params">img, size</span>) { 8721cb0ef41Sopenharmony_ci <span class="hljs-comment">// Perform resizing in worker_thread to avoid main thread blocking</span> 8731cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 8741cb0ef41Sopenharmony_ci<h4>Network-based loading is not enabled by default<span><a class="mark" href="#network-based-loading-is-not-enabled-by-default" id="network-based-loading-is-not-enabled-by-default">#</a></span><a aria-hidden="true" class="legacy" id="esm_network_based_loading_is_not_enabled_by_default"></a></h4> 8751cb0ef41Sopenharmony_ci<p>For now, the <code>--experimental-network-imports</code> flag is required to enable loading 8761cb0ef41Sopenharmony_ciresources over <code>http:</code> or <code>https:</code>. In the future, a different mechanism will be 8771cb0ef41Sopenharmony_ciused to enforce this. Opt-in is required to prevent transitive dependencies 8781cb0ef41Sopenharmony_ciinadvertently using potentially mutable state that could affect reliability 8791cb0ef41Sopenharmony_ciof Node.js applications.</p> 8801cb0ef41Sopenharmony_ci<p><i id="esm_experimental_loaders"></i></p> 8811cb0ef41Sopenharmony_ci</section><section><h3>Loaders<span><a class="mark" href="#loaders" id="loaders">#</a></span><a aria-hidden="true" class="legacy" id="esm_loaders"></a></h3> 8821cb0ef41Sopenharmony_ci<p>The former Loaders documentation is now at 8831cb0ef41Sopenharmony_ci<a href="module.html#customization-hooks">Modules: Customization hooks</a>.</p> 8841cb0ef41Sopenharmony_ci</section><section><h3>Resolution and loading algorithm<span><a class="mark" href="#resolution-and-loading-algorithm" id="resolution-and-loading-algorithm">#</a></span><a aria-hidden="true" class="legacy" id="esm_resolution_and_loading_algorithm"></a></h3> 8851cb0ef41Sopenharmony_ci<h4>Features<span><a class="mark" href="#features" id="features">#</a></span><a aria-hidden="true" class="legacy" id="esm_features"></a></h4> 8861cb0ef41Sopenharmony_ci<p>The default resolver has the following properties:</p> 8871cb0ef41Sopenharmony_ci<ul> 8881cb0ef41Sopenharmony_ci<li>FileURL-based resolution as is used by ES modules</li> 8891cb0ef41Sopenharmony_ci<li>Relative and absolute URL resolution</li> 8901cb0ef41Sopenharmony_ci<li>No default extensions</li> 8911cb0ef41Sopenharmony_ci<li>No folder mains</li> 8921cb0ef41Sopenharmony_ci<li>Bare specifier package resolution lookup through node_modules</li> 8931cb0ef41Sopenharmony_ci<li>Does not fail on unknown extensions or protocols</li> 8941cb0ef41Sopenharmony_ci<li>Can optionally provide a hint of the format to the loading phase</li> 8951cb0ef41Sopenharmony_ci</ul> 8961cb0ef41Sopenharmony_ci<p>The default loader has the following properties</p> 8971cb0ef41Sopenharmony_ci<ul> 8981cb0ef41Sopenharmony_ci<li>Support for builtin module loading via <code>node:</code> URLs</li> 8991cb0ef41Sopenharmony_ci<li>Support for "inline" module loading via <code>data:</code> URLs</li> 9001cb0ef41Sopenharmony_ci<li>Support for <code>file:</code> module loading</li> 9011cb0ef41Sopenharmony_ci<li>Fails on any other URL protocol</li> 9021cb0ef41Sopenharmony_ci<li>Fails on unknown extensions for <code>file:</code> loading 9031cb0ef41Sopenharmony_ci(supports only <code>.cjs</code>, <code>.js</code>, and <code>.mjs</code>)</li> 9041cb0ef41Sopenharmony_ci</ul> 9051cb0ef41Sopenharmony_ci<h4>Resolution algorithm<span><a class="mark" href="#resolution-algorithm" id="resolution-algorithm">#</a></span><a aria-hidden="true" class="legacy" id="esm_resolution_algorithm"></a></h4> 9061cb0ef41Sopenharmony_ci<p>The algorithm to load an ES module specifier is given through the 9071cb0ef41Sopenharmony_ci<strong>ESM_RESOLVE</strong> method below. It returns the resolved URL for a 9081cb0ef41Sopenharmony_cimodule specifier relative to a parentURL.</p> 9091cb0ef41Sopenharmony_ci<p>The resolution algorithm determines the full resolved URL for a module 9101cb0ef41Sopenharmony_ciload, along with its suggested module format. The resolution algorithm 9111cb0ef41Sopenharmony_cidoes not determine whether the resolved URL protocol can be loaded, 9121cb0ef41Sopenharmony_cior whether the file extensions are permitted, instead these validations 9131cb0ef41Sopenharmony_ciare applied by Node.js during the load phase 9141cb0ef41Sopenharmony_ci(for example, if it was asked to load a URL that has a protocol that is 9151cb0ef41Sopenharmony_cinot <code>file:</code>, <code>data:</code>, <code>node:</code>, or if <code>--experimental-network-imports</code> 9161cb0ef41Sopenharmony_ciis enabled, <code>https:</code>).</p> 9171cb0ef41Sopenharmony_ci<p>The algorithm also tries to determine the format of the file based 9181cb0ef41Sopenharmony_cion the extension (see <code>ESM_FILE_FORMAT</code> algorithm below). If it does 9191cb0ef41Sopenharmony_cinot recognize the file extension (eg if it is not <code>.mjs</code>, <code>.cjs</code>, or 9201cb0ef41Sopenharmony_ci<code>.json</code>), then a format of <code>undefined</code> is returned, 9211cb0ef41Sopenharmony_ciwhich will throw during the load phase.</p> 9221cb0ef41Sopenharmony_ci<p>The algorithm to determine the module format of a resolved URL is 9231cb0ef41Sopenharmony_ciprovided by <strong>ESM_FILE_FORMAT</strong>, which returns the unique module 9241cb0ef41Sopenharmony_ciformat for any file. The <em>"module"</em> format is returned for an ECMAScript 9251cb0ef41Sopenharmony_ciModule, while the <em>"commonjs"</em> format is used to indicate loading through the 9261cb0ef41Sopenharmony_cilegacy CommonJS loader. Additional formats such as <em>"addon"</em> can be extended in 9271cb0ef41Sopenharmony_cifuture updates.</p> 9281cb0ef41Sopenharmony_ci<p>In the following algorithms, all subroutine errors are propagated as errors 9291cb0ef41Sopenharmony_ciof these top-level routines unless stated otherwise.</p> 9301cb0ef41Sopenharmony_ci<p><em>defaultConditions</em> is the conditional environment name array, 9311cb0ef41Sopenharmony_ci<code>["node", "import"]</code>.</p> 9321cb0ef41Sopenharmony_ci<p>The resolver can throw the following errors:</p> 9331cb0ef41Sopenharmony_ci<ul> 9341cb0ef41Sopenharmony_ci<li><em>Invalid Module Specifier</em>: Module specifier is an invalid URL, package name 9351cb0ef41Sopenharmony_cior package subpath specifier.</li> 9361cb0ef41Sopenharmony_ci<li><em>Invalid Package Configuration</em>: package.json configuration is invalid or 9371cb0ef41Sopenharmony_cicontains an invalid configuration.</li> 9381cb0ef41Sopenharmony_ci<li><em>Invalid Package Target</em>: Package exports or imports define a target module 9391cb0ef41Sopenharmony_cifor the package that is an invalid type or string target.</li> 9401cb0ef41Sopenharmony_ci<li><em>Package Path Not Exported</em>: Package exports do not define or permit a target 9411cb0ef41Sopenharmony_cisubpath in the package for the given module.</li> 9421cb0ef41Sopenharmony_ci<li><em>Package Import Not Defined</em>: Package imports do not define the specifier.</li> 9431cb0ef41Sopenharmony_ci<li><em>Module Not Found</em>: The package or module requested does not exist.</li> 9441cb0ef41Sopenharmony_ci<li><em>Unsupported Directory Import</em>: The resolved path corresponds to a directory, 9451cb0ef41Sopenharmony_ciwhich is not a supported target for module imports.</li> 9461cb0ef41Sopenharmony_ci</ul> 9471cb0ef41Sopenharmony_ci<h4>Resolution Algorithm Specification<span><a class="mark" href="#resolution-algorithm-specification" id="resolution-algorithm-specification">#</a></span><a aria-hidden="true" class="legacy" id="esm_resolution_algorithm_specification"></a></h4> 9481cb0ef41Sopenharmony_ci<p><strong>ESM_RESOLVE</strong>(<em>specifier</em>, <em>parentURL</em>)</p> 9491cb0ef41Sopenharmony_ci<blockquote> 9501cb0ef41Sopenharmony_ci<ol> 9511cb0ef41Sopenharmony_ci<li>Let <em>resolved</em> be <strong>undefined</strong>.</li> 9521cb0ef41Sopenharmony_ci<li>If <em>specifier</em> is a valid URL, then 9531cb0ef41Sopenharmony_ci<ol> 9541cb0ef41Sopenharmony_ci<li>Set <em>resolved</em> to the result of parsing and reserializing 9551cb0ef41Sopenharmony_ci<em>specifier</em> as a URL.</li> 9561cb0ef41Sopenharmony_ci</ol> 9571cb0ef41Sopenharmony_ci</li> 9581cb0ef41Sopenharmony_ci<li>Otherwise, if <em>specifier</em> starts with <em>"/"</em>, <em>"./"</em>, or <em>"../"</em>, then 9591cb0ef41Sopenharmony_ci<ol> 9601cb0ef41Sopenharmony_ci<li>Set <em>resolved</em> to the URL resolution of <em>specifier</em> relative to 9611cb0ef41Sopenharmony_ci<em>parentURL</em>.</li> 9621cb0ef41Sopenharmony_ci</ol> 9631cb0ef41Sopenharmony_ci</li> 9641cb0ef41Sopenharmony_ci<li>Otherwise, if <em>specifier</em> starts with <em>"#"</em>, then 9651cb0ef41Sopenharmony_ci<ol> 9661cb0ef41Sopenharmony_ci<li>Set <em>resolved</em> to the result of 9671cb0ef41Sopenharmony_ci<strong>PACKAGE_IMPORTS_RESOLVE</strong>(<em>specifier</em>, 9681cb0ef41Sopenharmony_ci<em>parentURL</em>, <em>defaultConditions</em>).</li> 9691cb0ef41Sopenharmony_ci</ol> 9701cb0ef41Sopenharmony_ci</li> 9711cb0ef41Sopenharmony_ci<li>Otherwise, 9721cb0ef41Sopenharmony_ci<ol> 9731cb0ef41Sopenharmony_ci<li>Note: <em>specifier</em> is now a bare specifier.</li> 9741cb0ef41Sopenharmony_ci<li>Set <em>resolved</em> the result of 9751cb0ef41Sopenharmony_ci<strong>PACKAGE_RESOLVE</strong>(<em>specifier</em>, <em>parentURL</em>).</li> 9761cb0ef41Sopenharmony_ci</ol> 9771cb0ef41Sopenharmony_ci</li> 9781cb0ef41Sopenharmony_ci<li>Let <em>format</em> be <strong>undefined</strong>.</li> 9791cb0ef41Sopenharmony_ci<li>If <em>resolved</em> is a <em>"file:"</em> URL, then 9801cb0ef41Sopenharmony_ci<ol> 9811cb0ef41Sopenharmony_ci<li>If <em>resolved</em> contains any percent encodings of <em>"/"</em> or <em>"\"</em> (<em>"%2F"</em> 9821cb0ef41Sopenharmony_ciand <em>"%5C"</em> respectively), then 9831cb0ef41Sopenharmony_ci<ol> 9841cb0ef41Sopenharmony_ci<li>Throw an <em>Invalid Module Specifier</em> error.</li> 9851cb0ef41Sopenharmony_ci</ol> 9861cb0ef41Sopenharmony_ci</li> 9871cb0ef41Sopenharmony_ci<li>If the file at <em>resolved</em> is a directory, then 9881cb0ef41Sopenharmony_ci<ol> 9891cb0ef41Sopenharmony_ci<li>Throw an <em>Unsupported Directory Import</em> error.</li> 9901cb0ef41Sopenharmony_ci</ol> 9911cb0ef41Sopenharmony_ci</li> 9921cb0ef41Sopenharmony_ci<li>If the file at <em>resolved</em> does not exist, then 9931cb0ef41Sopenharmony_ci<ol> 9941cb0ef41Sopenharmony_ci<li>Throw a <em>Module Not Found</em> error.</li> 9951cb0ef41Sopenharmony_ci</ol> 9961cb0ef41Sopenharmony_ci</li> 9971cb0ef41Sopenharmony_ci<li>Set <em>resolved</em> to the real path of <em>resolved</em>, maintaining the 9981cb0ef41Sopenharmony_cisame URL querystring and fragment components.</li> 9991cb0ef41Sopenharmony_ci<li>Set <em>format</em> to the result of <strong>ESM_FILE_FORMAT</strong>(<em>resolved</em>).</li> 10001cb0ef41Sopenharmony_ci</ol> 10011cb0ef41Sopenharmony_ci</li> 10021cb0ef41Sopenharmony_ci<li>Otherwise, 10031cb0ef41Sopenharmony_ci<ol> 10041cb0ef41Sopenharmony_ci<li>Set <em>format</em> the module format of the content type associated with the 10051cb0ef41Sopenharmony_ciURL <em>resolved</em>.</li> 10061cb0ef41Sopenharmony_ci</ol> 10071cb0ef41Sopenharmony_ci</li> 10081cb0ef41Sopenharmony_ci<li>Return <em>format</em> and <em>resolved</em> to the loading phase</li> 10091cb0ef41Sopenharmony_ci</ol> 10101cb0ef41Sopenharmony_ci</blockquote> 10111cb0ef41Sopenharmony_ci<p><strong>PACKAGE_RESOLVE</strong>(<em>packageSpecifier</em>, <em>parentURL</em>)</p> 10121cb0ef41Sopenharmony_ci<blockquote> 10131cb0ef41Sopenharmony_ci<ol> 10141cb0ef41Sopenharmony_ci<li>Let <em>packageName</em> be <strong>undefined</strong>.</li> 10151cb0ef41Sopenharmony_ci<li>If <em>packageSpecifier</em> is an empty string, then 10161cb0ef41Sopenharmony_ci<ol> 10171cb0ef41Sopenharmony_ci<li>Throw an <em>Invalid Module Specifier</em> error.</li> 10181cb0ef41Sopenharmony_ci</ol> 10191cb0ef41Sopenharmony_ci</li> 10201cb0ef41Sopenharmony_ci<li>If <em>packageSpecifier</em> is a Node.js builtin module name, then 10211cb0ef41Sopenharmony_ci<ol> 10221cb0ef41Sopenharmony_ci<li>Return the string <em>"node:"</em> concatenated with <em>packageSpecifier</em>.</li> 10231cb0ef41Sopenharmony_ci</ol> 10241cb0ef41Sopenharmony_ci</li> 10251cb0ef41Sopenharmony_ci<li>If <em>packageSpecifier</em> does not start with <em>"@"</em>, then 10261cb0ef41Sopenharmony_ci<ol> 10271cb0ef41Sopenharmony_ci<li>Set <em>packageName</em> to the substring of <em>packageSpecifier</em> until the first 10281cb0ef41Sopenharmony_ci<em>"/"</em> separator or the end of the string.</li> 10291cb0ef41Sopenharmony_ci</ol> 10301cb0ef41Sopenharmony_ci</li> 10311cb0ef41Sopenharmony_ci<li>Otherwise, 10321cb0ef41Sopenharmony_ci<ol> 10331cb0ef41Sopenharmony_ci<li>If <em>packageSpecifier</em> does not contain a <em>"/"</em> separator, then 10341cb0ef41Sopenharmony_ci<ol> 10351cb0ef41Sopenharmony_ci<li>Throw an <em>Invalid Module Specifier</em> error.</li> 10361cb0ef41Sopenharmony_ci</ol> 10371cb0ef41Sopenharmony_ci</li> 10381cb0ef41Sopenharmony_ci<li>Set <em>packageName</em> to the substring of <em>packageSpecifier</em> 10391cb0ef41Sopenharmony_ciuntil the second <em>"/"</em> separator or the end of the string.</li> 10401cb0ef41Sopenharmony_ci</ol> 10411cb0ef41Sopenharmony_ci</li> 10421cb0ef41Sopenharmony_ci<li>If <em>packageName</em> starts with <em>"."</em> or contains <em>"\"</em> or <em>"%"</em>, then 10431cb0ef41Sopenharmony_ci<ol> 10441cb0ef41Sopenharmony_ci<li>Throw an <em>Invalid Module Specifier</em> error.</li> 10451cb0ef41Sopenharmony_ci</ol> 10461cb0ef41Sopenharmony_ci</li> 10471cb0ef41Sopenharmony_ci<li>Let <em>packageSubpath</em> be <em>"."</em> concatenated with the substring of 10481cb0ef41Sopenharmony_ci<em>packageSpecifier</em> from the position at the length of <em>packageName</em>.</li> 10491cb0ef41Sopenharmony_ci<li>If <em>packageSubpath</em> ends in <em>"/"</em>, then 10501cb0ef41Sopenharmony_ci<ol> 10511cb0ef41Sopenharmony_ci<li>Throw an <em>Invalid Module Specifier</em> error.</li> 10521cb0ef41Sopenharmony_ci</ol> 10531cb0ef41Sopenharmony_ci</li> 10541cb0ef41Sopenharmony_ci<li>Let <em>selfUrl</em> be the result of 10551cb0ef41Sopenharmony_ci<strong>PACKAGE_SELF_RESOLVE</strong>(<em>packageName</em>, <em>packageSubpath</em>, <em>parentURL</em>).</li> 10561cb0ef41Sopenharmony_ci<li>If <em>selfUrl</em> is not <strong>undefined</strong>, return <em>selfUrl</em>.</li> 10571cb0ef41Sopenharmony_ci<li>While <em>parentURL</em> is not the file system root, 10581cb0ef41Sopenharmony_ci<ol> 10591cb0ef41Sopenharmony_ci<li>Let <em>packageURL</em> be the URL resolution of <em>"node_modules/"</em> 10601cb0ef41Sopenharmony_ciconcatenated with <em>packageSpecifier</em>, relative to <em>parentURL</em>.</li> 10611cb0ef41Sopenharmony_ci<li>Set <em>parentURL</em> to the parent folder URL of <em>parentURL</em>.</li> 10621cb0ef41Sopenharmony_ci<li>If the folder at <em>packageURL</em> does not exist, then 10631cb0ef41Sopenharmony_ci<ol> 10641cb0ef41Sopenharmony_ci<li>Continue the next loop iteration.</li> 10651cb0ef41Sopenharmony_ci</ol> 10661cb0ef41Sopenharmony_ci</li> 10671cb0ef41Sopenharmony_ci<li>Let <em>pjson</em> be the result of <strong>READ_PACKAGE_JSON</strong>(<em>packageURL</em>).</li> 10681cb0ef41Sopenharmony_ci<li>If <em>pjson</em> is not <strong>null</strong> and <em>pjson</em>.<em>exports</em> is not <strong>null</strong> or 10691cb0ef41Sopenharmony_ci<strong>undefined</strong>, then 10701cb0ef41Sopenharmony_ci<ol> 10711cb0ef41Sopenharmony_ci<li>Return the result of <strong>PACKAGE_EXPORTS_RESOLVE</strong>(<em>packageURL</em>, 10721cb0ef41Sopenharmony_ci<em>packageSubpath</em>, <em>pjson.exports</em>, <em>defaultConditions</em>).</li> 10731cb0ef41Sopenharmony_ci</ol> 10741cb0ef41Sopenharmony_ci</li> 10751cb0ef41Sopenharmony_ci<li>Otherwise, if <em>packageSubpath</em> is equal to <em>"."</em>, then 10761cb0ef41Sopenharmony_ci<ol> 10771cb0ef41Sopenharmony_ci<li>If <em>pjson.main</em> is a string, then 10781cb0ef41Sopenharmony_ci<ol> 10791cb0ef41Sopenharmony_ci<li>Return the URL resolution of <em>main</em> in <em>packageURL</em>.</li> 10801cb0ef41Sopenharmony_ci</ol> 10811cb0ef41Sopenharmony_ci</li> 10821cb0ef41Sopenharmony_ci</ol> 10831cb0ef41Sopenharmony_ci</li> 10841cb0ef41Sopenharmony_ci<li>Otherwise, 10851cb0ef41Sopenharmony_ci<ol> 10861cb0ef41Sopenharmony_ci<li>Return the URL resolution of <em>packageSubpath</em> in <em>packageURL</em>.</li> 10871cb0ef41Sopenharmony_ci</ol> 10881cb0ef41Sopenharmony_ci</li> 10891cb0ef41Sopenharmony_ci</ol> 10901cb0ef41Sopenharmony_ci</li> 10911cb0ef41Sopenharmony_ci<li>Throw a <em>Module Not Found</em> error.</li> 10921cb0ef41Sopenharmony_ci</ol> 10931cb0ef41Sopenharmony_ci</blockquote> 10941cb0ef41Sopenharmony_ci<p><strong>PACKAGE_SELF_RESOLVE</strong>(<em>packageName</em>, <em>packageSubpath</em>, <em>parentURL</em>)</p> 10951cb0ef41Sopenharmony_ci<blockquote> 10961cb0ef41Sopenharmony_ci<ol> 10971cb0ef41Sopenharmony_ci<li>Let <em>packageURL</em> be the result of <strong>LOOKUP_PACKAGE_SCOPE</strong>(<em>parentURL</em>).</li> 10981cb0ef41Sopenharmony_ci<li>If <em>packageURL</em> is <strong>null</strong>, then 10991cb0ef41Sopenharmony_ci<ol> 11001cb0ef41Sopenharmony_ci<li>Return <strong>undefined</strong>.</li> 11011cb0ef41Sopenharmony_ci</ol> 11021cb0ef41Sopenharmony_ci</li> 11031cb0ef41Sopenharmony_ci<li>Let <em>pjson</em> be the result of <strong>READ_PACKAGE_JSON</strong>(<em>packageURL</em>).</li> 11041cb0ef41Sopenharmony_ci<li>If <em>pjson</em> is <strong>null</strong> or if <em>pjson</em>.<em>exports</em> is <strong>null</strong> or 11051cb0ef41Sopenharmony_ci<strong>undefined</strong>, then 11061cb0ef41Sopenharmony_ci<ol> 11071cb0ef41Sopenharmony_ci<li>Return <strong>undefined</strong>.</li> 11081cb0ef41Sopenharmony_ci</ol> 11091cb0ef41Sopenharmony_ci</li> 11101cb0ef41Sopenharmony_ci<li>If <em>pjson.name</em> is equal to <em>packageName</em>, then 11111cb0ef41Sopenharmony_ci<ol> 11121cb0ef41Sopenharmony_ci<li>Return the result of <strong>PACKAGE_EXPORTS_RESOLVE</strong>(<em>packageURL</em>, 11131cb0ef41Sopenharmony_ci<em>packageSubpath</em>, <em>pjson.exports</em>, <em>defaultConditions</em>).</li> 11141cb0ef41Sopenharmony_ci</ol> 11151cb0ef41Sopenharmony_ci</li> 11161cb0ef41Sopenharmony_ci<li>Otherwise, return <strong>undefined</strong>.</li> 11171cb0ef41Sopenharmony_ci</ol> 11181cb0ef41Sopenharmony_ci</blockquote> 11191cb0ef41Sopenharmony_ci<p><strong>PACKAGE_EXPORTS_RESOLVE</strong>(<em>packageURL</em>, <em>subpath</em>, <em>exports</em>, <em>conditions</em>)</p> 11201cb0ef41Sopenharmony_ci<blockquote> 11211cb0ef41Sopenharmony_ci<ol> 11221cb0ef41Sopenharmony_ci<li>If <em>exports</em> is an Object with both a key starting with <em>"."</em> and a key not 11231cb0ef41Sopenharmony_cistarting with <em>"."</em>, throw an <em>Invalid Package Configuration</em> error.</li> 11241cb0ef41Sopenharmony_ci<li>If <em>subpath</em> is equal to <em>"."</em>, then 11251cb0ef41Sopenharmony_ci<ol> 11261cb0ef41Sopenharmony_ci<li>Let <em>mainExport</em> be <strong>undefined</strong>.</li> 11271cb0ef41Sopenharmony_ci<li>If <em>exports</em> is a String or Array, or an Object containing no keys 11281cb0ef41Sopenharmony_cistarting with <em>"."</em>, then 11291cb0ef41Sopenharmony_ci<ol> 11301cb0ef41Sopenharmony_ci<li>Set <em>mainExport</em> to <em>exports</em>.</li> 11311cb0ef41Sopenharmony_ci</ol> 11321cb0ef41Sopenharmony_ci</li> 11331cb0ef41Sopenharmony_ci<li>Otherwise if <em>exports</em> is an Object containing a <em>"."</em> property, then 11341cb0ef41Sopenharmony_ci<ol> 11351cb0ef41Sopenharmony_ci<li>Set <em>mainExport</em> to <em>exports</em>[<em>"."</em>].</li> 11361cb0ef41Sopenharmony_ci</ol> 11371cb0ef41Sopenharmony_ci</li> 11381cb0ef41Sopenharmony_ci<li>If <em>mainExport</em> is not <strong>undefined</strong>, then 11391cb0ef41Sopenharmony_ci<ol> 11401cb0ef41Sopenharmony_ci<li>Let <em>resolved</em> be the result of <strong>PACKAGE_TARGET_RESOLVE</strong>( 11411cb0ef41Sopenharmony_ci<em>packageURL</em>, <em>mainExport</em>, <strong>null</strong>, <strong>false</strong>, <em>conditions</em>).</li> 11421cb0ef41Sopenharmony_ci<li>If <em>resolved</em> is not <strong>null</strong> or <strong>undefined</strong>, return <em>resolved</em>.</li> 11431cb0ef41Sopenharmony_ci</ol> 11441cb0ef41Sopenharmony_ci</li> 11451cb0ef41Sopenharmony_ci</ol> 11461cb0ef41Sopenharmony_ci</li> 11471cb0ef41Sopenharmony_ci<li>Otherwise, if <em>exports</em> is an Object and all keys of <em>exports</em> start with 11481cb0ef41Sopenharmony_ci<em>"."</em>, then 11491cb0ef41Sopenharmony_ci<ol> 11501cb0ef41Sopenharmony_ci<li>Let <em>matchKey</em> be the string <em>"./"</em> concatenated with <em>subpath</em>.</li> 11511cb0ef41Sopenharmony_ci<li>Let <em>resolved</em> be the result of <strong>PACKAGE_IMPORTS_EXPORTS_RESOLVE</strong>( 11521cb0ef41Sopenharmony_ci<em>matchKey</em>, <em>exports</em>, <em>packageURL</em>, <strong>false</strong>, <em>conditions</em>).</li> 11531cb0ef41Sopenharmony_ci<li>If <em>resolved</em> is not <strong>null</strong> or <strong>undefined</strong>, return <em>resolved</em>.</li> 11541cb0ef41Sopenharmony_ci</ol> 11551cb0ef41Sopenharmony_ci</li> 11561cb0ef41Sopenharmony_ci<li>Throw a <em>Package Path Not Exported</em> error.</li> 11571cb0ef41Sopenharmony_ci</ol> 11581cb0ef41Sopenharmony_ci</blockquote> 11591cb0ef41Sopenharmony_ci<p><strong>PACKAGE_IMPORTS_RESOLVE</strong>(<em>specifier</em>, <em>parentURL</em>, <em>conditions</em>)</p> 11601cb0ef41Sopenharmony_ci<blockquote> 11611cb0ef41Sopenharmony_ci<ol> 11621cb0ef41Sopenharmony_ci<li>Assert: <em>specifier</em> begins with <em>"#"</em>.</li> 11631cb0ef41Sopenharmony_ci<li>If <em>specifier</em> is exactly equal to <em>"#"</em> or starts with <em>"#/"</em>, then 11641cb0ef41Sopenharmony_ci<ol> 11651cb0ef41Sopenharmony_ci<li>Throw an <em>Invalid Module Specifier</em> error.</li> 11661cb0ef41Sopenharmony_ci</ol> 11671cb0ef41Sopenharmony_ci</li> 11681cb0ef41Sopenharmony_ci<li>Let <em>packageURL</em> be the result of <strong>LOOKUP_PACKAGE_SCOPE</strong>(<em>parentURL</em>).</li> 11691cb0ef41Sopenharmony_ci<li>If <em>packageURL</em> is not <strong>null</strong>, then 11701cb0ef41Sopenharmony_ci<ol> 11711cb0ef41Sopenharmony_ci<li>Let <em>pjson</em> be the result of <strong>READ_PACKAGE_JSON</strong>(<em>packageURL</em>).</li> 11721cb0ef41Sopenharmony_ci<li>If <em>pjson.imports</em> is a non-null Object, then 11731cb0ef41Sopenharmony_ci<ol> 11741cb0ef41Sopenharmony_ci<li>Let <em>resolved</em> be the result of 11751cb0ef41Sopenharmony_ci<strong>PACKAGE_IMPORTS_EXPORTS_RESOLVE</strong>( 11761cb0ef41Sopenharmony_ci<em>specifier</em>, <em>pjson.imports</em>, <em>packageURL</em>, <strong>true</strong>, <em>conditions</em>).</li> 11771cb0ef41Sopenharmony_ci<li>If <em>resolved</em> is not <strong>null</strong> or <strong>undefined</strong>, return <em>resolved</em>.</li> 11781cb0ef41Sopenharmony_ci</ol> 11791cb0ef41Sopenharmony_ci</li> 11801cb0ef41Sopenharmony_ci</ol> 11811cb0ef41Sopenharmony_ci</li> 11821cb0ef41Sopenharmony_ci<li>Throw a <em>Package Import Not Defined</em> error.</li> 11831cb0ef41Sopenharmony_ci</ol> 11841cb0ef41Sopenharmony_ci</blockquote> 11851cb0ef41Sopenharmony_ci<p><strong>PACKAGE_IMPORTS_EXPORTS_RESOLVE</strong>(<em>matchKey</em>, <em>matchObj</em>, <em>packageURL</em>, 11861cb0ef41Sopenharmony_ci<em>isImports</em>, <em>conditions</em>)</p> 11871cb0ef41Sopenharmony_ci<blockquote> 11881cb0ef41Sopenharmony_ci<ol> 11891cb0ef41Sopenharmony_ci<li>If <em>matchKey</em> is a key of <em>matchObj</em> and does not contain <em>"*"</em>, then 11901cb0ef41Sopenharmony_ci<ol> 11911cb0ef41Sopenharmony_ci<li>Let <em>target</em> be the value of <em>matchObj</em>[<em>matchKey</em>].</li> 11921cb0ef41Sopenharmony_ci<li>Return the result of <strong>PACKAGE_TARGET_RESOLVE</strong>(<em>packageURL</em>, 11931cb0ef41Sopenharmony_ci<em>target</em>, <strong>null</strong>, <em>isImports</em>, <em>conditions</em>).</li> 11941cb0ef41Sopenharmony_ci</ol> 11951cb0ef41Sopenharmony_ci</li> 11961cb0ef41Sopenharmony_ci<li>Let <em>expansionKeys</em> be the list of keys of <em>matchObj</em> containing only a 11971cb0ef41Sopenharmony_cisingle <em>"*"</em>, sorted by the sorting function <strong>PATTERN_KEY_COMPARE</strong> 11981cb0ef41Sopenharmony_ciwhich orders in descending order of specificity.</li> 11991cb0ef41Sopenharmony_ci<li>For each key <em>expansionKey</em> in <em>expansionKeys</em>, do 12001cb0ef41Sopenharmony_ci<ol> 12011cb0ef41Sopenharmony_ci<li>Let <em>patternBase</em> be the substring of <em>expansionKey</em> up to but excluding 12021cb0ef41Sopenharmony_cithe first <em>"*"</em> character.</li> 12031cb0ef41Sopenharmony_ci<li>If <em>matchKey</em> starts with but is not equal to <em>patternBase</em>, then 12041cb0ef41Sopenharmony_ci<ol> 12051cb0ef41Sopenharmony_ci<li>Let <em>patternTrailer</em> be the substring of <em>expansionKey</em> from the 12061cb0ef41Sopenharmony_ciindex after the first <em>"*"</em> character.</li> 12071cb0ef41Sopenharmony_ci<li>If <em>patternTrailer</em> has zero length, or if <em>matchKey</em> ends with 12081cb0ef41Sopenharmony_ci<em>patternTrailer</em> and the length of <em>matchKey</em> is greater than or 12091cb0ef41Sopenharmony_ciequal to the length of <em>expansionKey</em>, then 12101cb0ef41Sopenharmony_ci<ol> 12111cb0ef41Sopenharmony_ci<li>Let <em>target</em> be the value of <em>matchObj</em>[<em>expansionKey</em>].</li> 12121cb0ef41Sopenharmony_ci<li>Let <em>patternMatch</em> be the substring of <em>matchKey</em> starting at the 12131cb0ef41Sopenharmony_ciindex of the length of <em>patternBase</em> up to the length of 12141cb0ef41Sopenharmony_ci<em>matchKey</em> minus the length of <em>patternTrailer</em>.</li> 12151cb0ef41Sopenharmony_ci<li>Return the result of <strong>PACKAGE_TARGET_RESOLVE</strong>(<em>packageURL</em>, 12161cb0ef41Sopenharmony_ci<em>target</em>, <em>patternMatch</em>, <em>isImports</em>, <em>conditions</em>).</li> 12171cb0ef41Sopenharmony_ci</ol> 12181cb0ef41Sopenharmony_ci</li> 12191cb0ef41Sopenharmony_ci</ol> 12201cb0ef41Sopenharmony_ci</li> 12211cb0ef41Sopenharmony_ci</ol> 12221cb0ef41Sopenharmony_ci</li> 12231cb0ef41Sopenharmony_ci<li>Return <strong>null</strong>.</li> 12241cb0ef41Sopenharmony_ci</ol> 12251cb0ef41Sopenharmony_ci</blockquote> 12261cb0ef41Sopenharmony_ci<p><strong>PATTERN_KEY_COMPARE</strong>(<em>keyA</em>, <em>keyB</em>)</p> 12271cb0ef41Sopenharmony_ci<blockquote> 12281cb0ef41Sopenharmony_ci<ol> 12291cb0ef41Sopenharmony_ci<li>Assert: <em>keyA</em> ends with <em>"/"</em> or contains only a single <em>"*"</em>.</li> 12301cb0ef41Sopenharmony_ci<li>Assert: <em>keyB</em> ends with <em>"/"</em> or contains only a single <em>"*"</em>.</li> 12311cb0ef41Sopenharmony_ci<li>Let <em>baseLengthA</em> be the index of <em>"*"</em> in <em>keyA</em> plus one, if <em>keyA</em> 12321cb0ef41Sopenharmony_cicontains <em>"*"</em>, or the length of <em>keyA</em> otherwise.</li> 12331cb0ef41Sopenharmony_ci<li>Let <em>baseLengthB</em> be the index of <em>"*"</em> in <em>keyB</em> plus one, if <em>keyB</em> 12341cb0ef41Sopenharmony_cicontains <em>"*"</em>, or the length of <em>keyB</em> otherwise.</li> 12351cb0ef41Sopenharmony_ci<li>If <em>baseLengthA</em> is greater than <em>baseLengthB</em>, return -1.</li> 12361cb0ef41Sopenharmony_ci<li>If <em>baseLengthB</em> is greater than <em>baseLengthA</em>, return 1.</li> 12371cb0ef41Sopenharmony_ci<li>If <em>keyA</em> does not contain <em>"*"</em>, return 1.</li> 12381cb0ef41Sopenharmony_ci<li>If <em>keyB</em> does not contain <em>"*"</em>, return -1.</li> 12391cb0ef41Sopenharmony_ci<li>If the length of <em>keyA</em> is greater than the length of <em>keyB</em>, return -1.</li> 12401cb0ef41Sopenharmony_ci<li>If the length of <em>keyB</em> is greater than the length of <em>keyA</em>, return 1.</li> 12411cb0ef41Sopenharmony_ci<li>Return 0.</li> 12421cb0ef41Sopenharmony_ci</ol> 12431cb0ef41Sopenharmony_ci</blockquote> 12441cb0ef41Sopenharmony_ci<p><strong>PACKAGE_TARGET_RESOLVE</strong>(<em>packageURL</em>, <em>target</em>, <em>patternMatch</em>, 12451cb0ef41Sopenharmony_ci<em>isImports</em>, <em>conditions</em>)</p> 12461cb0ef41Sopenharmony_ci<blockquote> 12471cb0ef41Sopenharmony_ci<ol> 12481cb0ef41Sopenharmony_ci<li>If <em>target</em> is a String, then 12491cb0ef41Sopenharmony_ci<ol> 12501cb0ef41Sopenharmony_ci<li>If <em>target</em> does not start with <em>"./"</em>, then 12511cb0ef41Sopenharmony_ci<ol> 12521cb0ef41Sopenharmony_ci<li>If <em>isImports</em> is <strong>false</strong>, or if <em>target</em> starts with <em>"../"</em> or 12531cb0ef41Sopenharmony_ci<em>"/"</em>, or if <em>target</em> is a valid URL, then 12541cb0ef41Sopenharmony_ci<ol> 12551cb0ef41Sopenharmony_ci<li>Throw an <em>Invalid Package Target</em> error.</li> 12561cb0ef41Sopenharmony_ci</ol> 12571cb0ef41Sopenharmony_ci</li> 12581cb0ef41Sopenharmony_ci<li>If <em>patternMatch</em> is a String, then 12591cb0ef41Sopenharmony_ci<ol> 12601cb0ef41Sopenharmony_ci<li>Return <strong>PACKAGE_RESOLVE</strong>(<em>target</em> with every instance of <em>"*"</em> 12611cb0ef41Sopenharmony_cireplaced by <em>patternMatch</em>, <em>packageURL</em> + <em>"/"</em>).</li> 12621cb0ef41Sopenharmony_ci</ol> 12631cb0ef41Sopenharmony_ci</li> 12641cb0ef41Sopenharmony_ci<li>Return <strong>PACKAGE_RESOLVE</strong>(<em>target</em>, <em>packageURL</em> + <em>"/"</em>).</li> 12651cb0ef41Sopenharmony_ci</ol> 12661cb0ef41Sopenharmony_ci</li> 12671cb0ef41Sopenharmony_ci<li>If <em>target</em> split on <em>"/"</em> or <em>"\"</em> contains any <em>""</em>, <em>"."</em>, <em>".."</em>, 12681cb0ef41Sopenharmony_cior <em>"node_modules"</em> segments after the first <em>"."</em> segment, case 12691cb0ef41Sopenharmony_ciinsensitive and including percent encoded variants, throw an <em>Invalid 12701cb0ef41Sopenharmony_ciPackage Target</em> error.</li> 12711cb0ef41Sopenharmony_ci<li>Let <em>resolvedTarget</em> be the URL resolution of the concatenation of 12721cb0ef41Sopenharmony_ci<em>packageURL</em> and <em>target</em>.</li> 12731cb0ef41Sopenharmony_ci<li>Assert: <em>resolvedTarget</em> is contained in <em>packageURL</em>.</li> 12741cb0ef41Sopenharmony_ci<li>If <em>patternMatch</em> is <strong>null</strong>, then 12751cb0ef41Sopenharmony_ci<ol> 12761cb0ef41Sopenharmony_ci<li>Return <em>resolvedTarget</em>.</li> 12771cb0ef41Sopenharmony_ci</ol> 12781cb0ef41Sopenharmony_ci</li> 12791cb0ef41Sopenharmony_ci<li>If <em>patternMatch</em> split on <em>"/"</em> or <em>"\"</em> contains any <em>""</em>, <em>"."</em>, 12801cb0ef41Sopenharmony_ci<em>".."</em>, or <em>"node_modules"</em> segments, case insensitive and including 12811cb0ef41Sopenharmony_cipercent encoded variants, throw an <em>Invalid Module Specifier</em> error.</li> 12821cb0ef41Sopenharmony_ci<li>Return the URL resolution of <em>resolvedTarget</em> with every instance of 12831cb0ef41Sopenharmony_ci<em>"*"</em> replaced with <em>patternMatch</em>.</li> 12841cb0ef41Sopenharmony_ci</ol> 12851cb0ef41Sopenharmony_ci</li> 12861cb0ef41Sopenharmony_ci<li>Otherwise, if <em>target</em> is a non-null Object, then 12871cb0ef41Sopenharmony_ci<ol> 12881cb0ef41Sopenharmony_ci<li>If <em>exports</em> contains any index property keys, as defined in ECMA-262 12891cb0ef41Sopenharmony_ci<a href="https://tc39.es/ecma262/#integer-index">6.1.7 Array Index</a>, throw an <em>Invalid Package Configuration</em> error.</li> 12901cb0ef41Sopenharmony_ci<li>For each property <em>p</em> of <em>target</em>, in object insertion order as, 12911cb0ef41Sopenharmony_ci<ol> 12921cb0ef41Sopenharmony_ci<li>If <em>p</em> equals <em>"default"</em> or <em>conditions</em> contains an entry for <em>p</em>, 12931cb0ef41Sopenharmony_cithen 12941cb0ef41Sopenharmony_ci<ol> 12951cb0ef41Sopenharmony_ci<li>Let <em>targetValue</em> be the value of the <em>p</em> property in <em>target</em>.</li> 12961cb0ef41Sopenharmony_ci<li>Let <em>resolved</em> be the result of <strong>PACKAGE_TARGET_RESOLVE</strong>( 12971cb0ef41Sopenharmony_ci<em>packageURL</em>, <em>targetValue</em>, <em>patternMatch</em>, <em>isImports</em>, 12981cb0ef41Sopenharmony_ci<em>conditions</em>).</li> 12991cb0ef41Sopenharmony_ci<li>If <em>resolved</em> is equal to <strong>undefined</strong>, continue the loop.</li> 13001cb0ef41Sopenharmony_ci<li>Return <em>resolved</em>.</li> 13011cb0ef41Sopenharmony_ci</ol> 13021cb0ef41Sopenharmony_ci</li> 13031cb0ef41Sopenharmony_ci</ol> 13041cb0ef41Sopenharmony_ci</li> 13051cb0ef41Sopenharmony_ci<li>Return <strong>undefined</strong>.</li> 13061cb0ef41Sopenharmony_ci</ol> 13071cb0ef41Sopenharmony_ci</li> 13081cb0ef41Sopenharmony_ci<li>Otherwise, if <em>target</em> is an Array, then 13091cb0ef41Sopenharmony_ci<ol> 13101cb0ef41Sopenharmony_ci<li>If _target.length is zero, return <strong>null</strong>.</li> 13111cb0ef41Sopenharmony_ci<li>For each item <em>targetValue</em> in <em>target</em>, do 13121cb0ef41Sopenharmony_ci<ol> 13131cb0ef41Sopenharmony_ci<li>Let <em>resolved</em> be the result of <strong>PACKAGE_TARGET_RESOLVE</strong>( 13141cb0ef41Sopenharmony_ci<em>packageURL</em>, <em>targetValue</em>, <em>patternMatch</em>, <em>isImports</em>, 13151cb0ef41Sopenharmony_ci<em>conditions</em>), continuing the loop on any <em>Invalid Package Target</em> 13161cb0ef41Sopenharmony_cierror.</li> 13171cb0ef41Sopenharmony_ci<li>If <em>resolved</em> is <strong>undefined</strong>, continue the loop.</li> 13181cb0ef41Sopenharmony_ci<li>Return <em>resolved</em>.</li> 13191cb0ef41Sopenharmony_ci</ol> 13201cb0ef41Sopenharmony_ci</li> 13211cb0ef41Sopenharmony_ci<li>Return or throw the last fallback resolution <strong>null</strong> return or error.</li> 13221cb0ef41Sopenharmony_ci</ol> 13231cb0ef41Sopenharmony_ci</li> 13241cb0ef41Sopenharmony_ci<li>Otherwise, if <em>target</em> is <em>null</em>, return <strong>null</strong>.</li> 13251cb0ef41Sopenharmony_ci<li>Otherwise throw an <em>Invalid Package Target</em> error.</li> 13261cb0ef41Sopenharmony_ci</ol> 13271cb0ef41Sopenharmony_ci</blockquote> 13281cb0ef41Sopenharmony_ci<p><strong>ESM_FILE_FORMAT</strong>(<em>url</em>)</p> 13291cb0ef41Sopenharmony_ci<blockquote> 13301cb0ef41Sopenharmony_ci<ol> 13311cb0ef41Sopenharmony_ci<li>Assert: <em>url</em> corresponds to an existing file.</li> 13321cb0ef41Sopenharmony_ci<li>If <em>url</em> ends in <em>".mjs"</em>, then 13331cb0ef41Sopenharmony_ci<ol> 13341cb0ef41Sopenharmony_ci<li>Return <em>"module"</em>.</li> 13351cb0ef41Sopenharmony_ci</ol> 13361cb0ef41Sopenharmony_ci</li> 13371cb0ef41Sopenharmony_ci<li>If <em>url</em> ends in <em>".cjs"</em>, then 13381cb0ef41Sopenharmony_ci<ol> 13391cb0ef41Sopenharmony_ci<li>Return <em>"commonjs"</em>.</li> 13401cb0ef41Sopenharmony_ci</ol> 13411cb0ef41Sopenharmony_ci</li> 13421cb0ef41Sopenharmony_ci<li>If <em>url</em> ends in <em>".json"</em>, then 13431cb0ef41Sopenharmony_ci<ol> 13441cb0ef41Sopenharmony_ci<li>Return <em>"json"</em>.</li> 13451cb0ef41Sopenharmony_ci</ol> 13461cb0ef41Sopenharmony_ci</li> 13471cb0ef41Sopenharmony_ci<li>Let <em>packageURL</em> be the result of <strong>LOOKUP_PACKAGE_SCOPE</strong>(<em>url</em>).</li> 13481cb0ef41Sopenharmony_ci<li>Let <em>pjson</em> be the result of <strong>READ_PACKAGE_JSON</strong>(<em>packageURL</em>).</li> 13491cb0ef41Sopenharmony_ci<li>If <em>pjson?.type</em> exists and is <em>"module"</em>, then 13501cb0ef41Sopenharmony_ci<ol> 13511cb0ef41Sopenharmony_ci<li>If <em>url</em> ends in <em>".js"</em> or has no file extension, then 13521cb0ef41Sopenharmony_ci<ol> 13531cb0ef41Sopenharmony_ci<li>If <code>--experimental-wasm-modules</code> is enabled and the file at <em>url</em> 13541cb0ef41Sopenharmony_cicontains the header for a WebAssembly module, then 13551cb0ef41Sopenharmony_ci<ol> 13561cb0ef41Sopenharmony_ci<li>Return <em>"wasm"</em>.</li> 13571cb0ef41Sopenharmony_ci</ol> 13581cb0ef41Sopenharmony_ci</li> 13591cb0ef41Sopenharmony_ci<li>Otherwise, 13601cb0ef41Sopenharmony_ci<ol> 13611cb0ef41Sopenharmony_ci<li>Return <em>"module"</em>.</li> 13621cb0ef41Sopenharmony_ci</ol> 13631cb0ef41Sopenharmony_ci</li> 13641cb0ef41Sopenharmony_ci</ol> 13651cb0ef41Sopenharmony_ci</li> 13661cb0ef41Sopenharmony_ci<li>Return <strong>undefined</strong>.</li> 13671cb0ef41Sopenharmony_ci</ol> 13681cb0ef41Sopenharmony_ci</li> 13691cb0ef41Sopenharmony_ci<li>Otherwise, 13701cb0ef41Sopenharmony_ci<ol> 13711cb0ef41Sopenharmony_ci<li>Return <strong>undefined</strong>.</li> 13721cb0ef41Sopenharmony_ci</ol> 13731cb0ef41Sopenharmony_ci</li> 13741cb0ef41Sopenharmony_ci</ol> 13751cb0ef41Sopenharmony_ci</blockquote> 13761cb0ef41Sopenharmony_ci<p><strong>LOOKUP_PACKAGE_SCOPE</strong>(<em>url</em>)</p> 13771cb0ef41Sopenharmony_ci<blockquote> 13781cb0ef41Sopenharmony_ci<ol> 13791cb0ef41Sopenharmony_ci<li>Let <em>scopeURL</em> be <em>url</em>.</li> 13801cb0ef41Sopenharmony_ci<li>While <em>scopeURL</em> is not the file system root, 13811cb0ef41Sopenharmony_ci<ol> 13821cb0ef41Sopenharmony_ci<li>Set <em>scopeURL</em> to the parent URL of <em>scopeURL</em>.</li> 13831cb0ef41Sopenharmony_ci<li>If <em>scopeURL</em> ends in a <em>"node_modules"</em> path segment, return <strong>null</strong>.</li> 13841cb0ef41Sopenharmony_ci<li>Let <em>pjsonURL</em> be the resolution of <em>"package.json"</em> within 13851cb0ef41Sopenharmony_ci<em>scopeURL</em>.</li> 13861cb0ef41Sopenharmony_ci<li>if the file at <em>pjsonURL</em> exists, then 13871cb0ef41Sopenharmony_ci<ol> 13881cb0ef41Sopenharmony_ci<li>Return <em>scopeURL</em>.</li> 13891cb0ef41Sopenharmony_ci</ol> 13901cb0ef41Sopenharmony_ci</li> 13911cb0ef41Sopenharmony_ci</ol> 13921cb0ef41Sopenharmony_ci</li> 13931cb0ef41Sopenharmony_ci<li>Return <strong>null</strong>.</li> 13941cb0ef41Sopenharmony_ci</ol> 13951cb0ef41Sopenharmony_ci</blockquote> 13961cb0ef41Sopenharmony_ci<p><strong>READ_PACKAGE_JSON</strong>(<em>packageURL</em>)</p> 13971cb0ef41Sopenharmony_ci<blockquote> 13981cb0ef41Sopenharmony_ci<ol> 13991cb0ef41Sopenharmony_ci<li>Let <em>pjsonURL</em> be the resolution of <em>"package.json"</em> within <em>packageURL</em>.</li> 14001cb0ef41Sopenharmony_ci<li>If the file at <em>pjsonURL</em> does not exist, then 14011cb0ef41Sopenharmony_ci<ol> 14021cb0ef41Sopenharmony_ci<li>Return <strong>null</strong>.</li> 14031cb0ef41Sopenharmony_ci</ol> 14041cb0ef41Sopenharmony_ci</li> 14051cb0ef41Sopenharmony_ci<li>If the file at <em>packageURL</em> does not parse as valid JSON, then 14061cb0ef41Sopenharmony_ci<ol> 14071cb0ef41Sopenharmony_ci<li>Throw an <em>Invalid Package Configuration</em> error.</li> 14081cb0ef41Sopenharmony_ci</ol> 14091cb0ef41Sopenharmony_ci</li> 14101cb0ef41Sopenharmony_ci<li>Return the parsed JSON source of the file at <em>pjsonURL</em>.</li> 14111cb0ef41Sopenharmony_ci</ol> 14121cb0ef41Sopenharmony_ci</blockquote> 14131cb0ef41Sopenharmony_ci<h4>Customizing ESM specifier resolution algorithm<span><a class="mark" href="#customizing-esm-specifier-resolution-algorithm" id="customizing-esm-specifier-resolution-algorithm">#</a></span><a aria-hidden="true" class="legacy" id="esm_customizing_esm_specifier_resolution_algorithm"></a></h4> 14141cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 14151cb0ef41Sopenharmony_ci<blockquote> 14161cb0ef41Sopenharmony_ci<p>Do not rely on this flag. We plan to remove it once the 14171cb0ef41Sopenharmony_ci<a href="module.html#customization-hooks">Module customization hooks</a> have advanced to the point that equivalent 14181cb0ef41Sopenharmony_cifunctionality can be achieved via custom hooks.</p> 14191cb0ef41Sopenharmony_ci</blockquote> 14201cb0ef41Sopenharmony_ci<p>The current specifier resolution does not support all default behavior of 14211cb0ef41Sopenharmony_cithe CommonJS loader. One of the behavior differences is automatic resolution 14221cb0ef41Sopenharmony_ciof file extensions and the ability to import directories that have an index 14231cb0ef41Sopenharmony_cifile.</p> 14241cb0ef41Sopenharmony_ci<p>The <code>--experimental-specifier-resolution=[mode]</code> flag can be used to customize 14251cb0ef41Sopenharmony_cithe extension resolution algorithm. The default mode is <code>explicit</code>, which 14261cb0ef41Sopenharmony_cirequires the full path to a module be provided to the loader. To enable the 14271cb0ef41Sopenharmony_ciautomatic extension resolution and importing from directories that include an 14281cb0ef41Sopenharmony_ciindex file use the <code>node</code> mode.</p> 14291cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node index.mjs</span> 14301cb0ef41Sopenharmony_cisuccess! 14311cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">$ </span><span class="language-bash">node index <span class="hljs-comment"># Failure!</span></span> 14321cb0ef41Sopenharmony_ciError: Cannot find module 14331cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">$ </span><span class="language-bash">node --experimental-specifier-resolution=node index</span> 14341cb0ef41Sopenharmony_cisuccess!</code> <button class="copy-button">copy</button></pre> 14351cb0ef41Sopenharmony_ci<!-- Note: The cjs-module-lexer link should be kept in-sync with the deps version --></section> 14361cb0ef41Sopenharmony_ci <!-- API END --> 14371cb0ef41Sopenharmony_ci </div> 14381cb0ef41Sopenharmony_ci </div> 14391cb0ef41Sopenharmony_ci </div> 14401cb0ef41Sopenharmony_ci</body> 14411cb0ef41Sopenharmony_ci</html> 1442