• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v18.20.1">
7  <title>Modules: ECMAScript modules | Node.js v18.20.1 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/esm.html">
12  <script async defer src="assets/api.js" type="text/javascript"></script>
13
14</head>
15<body class="alt apidoc" id="api-section-esm">
16  <div id="content" class="clearfix">
17    <div id="column2" class="interior">
18      <div id="intro" class="interior">
19        <a href="/" title="Go back to the home page">
20          Node.js
21        </a>
22      </div>
23      <ul>
24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
26</ul>
27<hr class="line">
28<ul>
29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
33<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
38<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
39<li><a href="console.html" class="nav-console">Console</a></li>
40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
45<li><a href="dns.html" class="nav-dns">DNS</a></li>
46<li><a href="domain.html" class="nav-domain">Domain</a></li>
47<li><a href="errors.html" class="nav-errors">Errors</a></li>
48<li><a href="events.html" class="nav-events">Events</a></li>
49<li><a href="fs.html" class="nav-fs">File system</a></li>
50<li><a href="globals.html" class="nav-globals">Globals</a></li>
51<li><a href="http.html" class="nav-http">HTTP</a></li>
52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
53<li><a href="https.html" class="nav-https">HTTPS</a></li>
54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
55<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
57<li><a href="esm.html" class="nav-esm active">Modules: ECMAScript modules</a></li>
58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
60<li><a href="net.html" class="nav-net">Net</a></li>
61<li><a href="os.html" class="nav-os">OS</a></li>
62<li><a href="path.html" class="nav-path">Path</a></li>
63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
65<li><a href="process.html" class="nav-process">Process</a></li>
66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
68<li><a href="readline.html" class="nav-readline">Readline</a></li>
69<li><a href="repl.html" class="nav-repl">REPL</a></li>
70<li><a href="report.html" class="nav-report">Report</a></li>
71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
72<li><a href="stream.html" class="nav-stream">Stream</a></li>
73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
74<li><a href="test.html" class="nav-test">Test runner</a></li>
75<li><a href="timers.html" class="nav-timers">Timers</a></li>
76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
78<li><a href="tty.html" class="nav-tty">TTY</a></li>
79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
80<li><a href="url.html" class="nav-url">URL</a></li>
81<li><a href="util.html" class="nav-util">Utilities</a></li>
82<li><a href="v8.html" class="nav-v8">V8</a></li>
83<li><a href="vm.html" class="nav-vm">VM</a></li>
84<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
89</ul>
90<hr class="line">
91<ul>
92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
93</ul>
94    </div>
95
96    <div id="column1" data-id="esm" class="interior">
97      <header class="header">
98        <div class="header-container">
99          <h1>Node.js v18.20.1 documentation</h1>
100          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
101            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
102              <path fill="none" d="M0 0h24v24H0z" />
103              <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"/>
104              <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"/>
105            </svg>
106            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
107              <path d="M0 0h24v24H0z" fill="none" />
108              <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"/>
109            </svg>
110          </button>
111        </div>
112        <div id="gtoc">
113          <ul>
114            <li class="pinned-header">Node.js v18.20.1</li>
115
116    <li class="picker-header">
117      <a href="#">
118        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
119        Table of contents
120      </a>
121
122      <div class="picker"><div class="toc"><ul>
123<li><a href="#modules-ecmascript-modules">Modules: ECMAScript modules</a>
124<ul>
125<li><a href="#introduction">Introduction</a></li>
126<li><a href="#enabling">Enabling</a></li>
127<li><a href="#packages">Packages</a></li>
128<li><a href="#import-specifiers"><code>import</code> Specifiers</a>
129<ul>
130<li><a href="#terminology">Terminology</a></li>
131<li><a href="#mandatory-file-extensions">Mandatory file extensions</a></li>
132<li><a href="#urls">URLs</a>
133<ul>
134<li><a href="#file-urls"><code>file:</code> URLs</a></li>
135<li><a href="#data-imports"><code>data:</code> imports</a></li>
136<li><a href="#node-imports"><code>node:</code> imports</a></li>
137</ul>
138</li>
139</ul>
140</li>
141<li><span class="stability_1"><a href="#import-attributes">Import attributes</a></span></li>
142<li><a href="#builtin-modules">Builtin modules</a></li>
143<li><a href="#import-expressions"><code>import()</code> expressions</a></li>
144<li><a href="#importmeta"><code>import.meta</code></a>
145<ul>
146<li><a href="#importmetaurl"><code>import.meta.url</code></a></li>
147<li><span class="stability_1"><a href="#importmetaresolvespecifier"><code>import.meta.resolve(specifier)</code></a></span></li>
148</ul>
149</li>
150<li><a href="#interoperability-with-commonjs">Interoperability with CommonJS</a>
151<ul>
152<li><a href="#import-statements"><code>import</code> statements</a></li>
153<li><a href="#require"><code>require</code></a></li>
154<li><a href="#commonjs-namespaces">CommonJS Namespaces</a></li>
155<li><a href="#differences-between-es-modules-and-commonjs">Differences between ES modules and CommonJS</a>
156<ul>
157<li><a href="#no-require-exports-or-moduleexports">No <code>require</code>, <code>exports</code>, or <code>module.exports</code></a></li>
158<li><a href="#no-__filename-or-__dirname">No <code>__filename</code> or <code>__dirname</code></a></li>
159<li><a href="#no-addon-loading">No Addon Loading</a></li>
160<li><a href="#no-requireresolve">No <code>require.resolve</code></a></li>
161<li><a href="#no-node_path">No <code>NODE_PATH</code></a></li>
162<li><a href="#no-requireextensions">No <code>require.extensions</code></a></li>
163<li><a href="#no-requirecache">No <code>require.cache</code></a></li>
164</ul>
165</li>
166</ul>
167</li>
168<li><span class="stability_1"><a href="#json-modules">JSON modules</a></span></li>
169<li><span class="stability_1"><a href="#wasm-modules">Wasm modules</a></span></li>
170<li><a href="#top-level-await">Top-level <code>await</code></a></li>
171<li><span class="stability_1"><a href="#https-and-http-imports">HTTPS and HTTP imports</a></span>
172<ul>
173<li><a href="#imports-are-limited-to-http1">Imports are limited to HTTP/1</a></li>
174<li><a href="#http-is-limited-to-loopback-addresses">HTTP is limited to loopback addresses</a></li>
175<li><a href="#authentication-is-never-sent-to-the-destination-server">Authentication is never sent to the destination server.</a></li>
176<li><a href="#cors-is-never-checked-on-the-destination-server">CORS is never checked on the destination server</a></li>
177<li><a href="#cannot-load-non-network-dependencies">Cannot load non-network dependencies</a></li>
178<li><a href="#network-based-loading-is-not-enabled-by-default">Network-based loading is not enabled by default</a></li>
179</ul>
180</li>
181<li><a href="#loaders">Loaders</a></li>
182<li><a href="#resolution-and-loading-algorithm">Resolution and loading algorithm</a>
183<ul>
184<li><a href="#features">Features</a></li>
185<li><a href="#resolution-algorithm">Resolution algorithm</a></li>
186<li><a href="#resolution-algorithm-specification">Resolution Algorithm Specification</a></li>
187<li><span class="stability_1"><a href="#customizing-esm-specifier-resolution-algorithm">Customizing ESM specifier resolution algorithm</a></span></li>
188</ul>
189</li>
190</ul>
191</li>
192</ul></div></div>
193    </li>
194
195
196    <li class="picker-header">
197      <a href="#">
198        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
199        Index
200      </a>
201
202      <div class="picker"><ul>
203<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
204<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
205
206      <li>
207        <a href="index.html">Index</a>
208      </li>
209    </ul>
210
211<hr class="line">
212<ul>
213<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
214<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
215<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
216<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
217<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
218<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
219<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
220<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
221<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
222<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
223<li><a href="console.html" class="nav-console">Console</a></li>
224<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
225<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
226<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
227<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
228<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
229<li><a href="dns.html" class="nav-dns">DNS</a></li>
230<li><a href="domain.html" class="nav-domain">Domain</a></li>
231<li><a href="errors.html" class="nav-errors">Errors</a></li>
232<li><a href="events.html" class="nav-events">Events</a></li>
233<li><a href="fs.html" class="nav-fs">File system</a></li>
234<li><a href="globals.html" class="nav-globals">Globals</a></li>
235<li><a href="http.html" class="nav-http">HTTP</a></li>
236<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
237<li><a href="https.html" class="nav-https">HTTPS</a></li>
238<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
239<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
240<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
241<li><a href="esm.html" class="nav-esm active">Modules: ECMAScript modules</a></li>
242<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
243<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
244<li><a href="net.html" class="nav-net">Net</a></li>
245<li><a href="os.html" class="nav-os">OS</a></li>
246<li><a href="path.html" class="nav-path">Path</a></li>
247<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
248<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
249<li><a href="process.html" class="nav-process">Process</a></li>
250<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
251<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
252<li><a href="readline.html" class="nav-readline">Readline</a></li>
253<li><a href="repl.html" class="nav-repl">REPL</a></li>
254<li><a href="report.html" class="nav-report">Report</a></li>
255<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
256<li><a href="stream.html" class="nav-stream">Stream</a></li>
257<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
258<li><a href="test.html" class="nav-test">Test runner</a></li>
259<li><a href="timers.html" class="nav-timers">Timers</a></li>
260<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
261<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
262<li><a href="tty.html" class="nav-tty">TTY</a></li>
263<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
264<li><a href="url.html" class="nav-url">URL</a></li>
265<li><a href="util.html" class="nav-util">Utilities</a></li>
266<li><a href="v8.html" class="nav-v8">V8</a></li>
267<li><a href="vm.html" class="nav-vm">VM</a></li>
268<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
269<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
270<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
271<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
272<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
273</ul>
274<hr class="line">
275<ul>
276<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
277</ul></div>
278    </li>
279
280
281    <li class="picker-header">
282      <a href="#">
283        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
284        Other versions
285      </a>
286      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/esm.html">21.x</a></li>
287<li><a href="https://nodejs.org/docs/latest-v20.x/api/esm.html">20.x <b>LTS</b></a></li>
288<li><a href="https://nodejs.org/docs/latest-v19.x/api/esm.html">19.x</a></li>
289<li><a href="https://nodejs.org/docs/latest-v18.x/api/esm.html">18.x <b>LTS</b></a></li>
290<li><a href="https://nodejs.org/docs/latest-v17.x/api/esm.html">17.x</a></li>
291<li><a href="https://nodejs.org/docs/latest-v16.x/api/esm.html">16.x</a></li>
292<li><a href="https://nodejs.org/docs/latest-v15.x/api/esm.html">15.x</a></li>
293<li><a href="https://nodejs.org/docs/latest-v14.x/api/esm.html">14.x</a></li>
294<li><a href="https://nodejs.org/docs/latest-v13.x/api/esm.html">13.x</a></li>
295<li><a href="https://nodejs.org/docs/latest-v12.x/api/esm.html">12.x</a></li>
296<li><a href="https://nodejs.org/docs/latest-v11.x/api/esm.html">11.x</a></li>
297<li><a href="https://nodejs.org/docs/latest-v10.x/api/esm.html">10.x</a></li>
298<li><a href="https://nodejs.org/docs/latest-v9.x/api/esm.html">9.x</a></li>
299<li><a href="https://nodejs.org/docs/latest-v8.x/api/esm.html">8.x</a></li></ol></div>
300    </li>
301
302            <li class="picker-header">
303              <a href="#">
304                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
305                Options
306              </a>
307
308              <div class="picker">
309                <ul>
310                  <li>
311                    <a href="all.html">View on single page</a>
312                  </li>
313                  <li>
314                    <a href="esm.json">View as JSON</a>
315                  </li>
316                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/esm.md">Edit on GitHub</a></li>
317                </ul>
318              </div>
319            </li>
320          </ul>
321        </div>
322        <hr>
323      </header>
324
325      <details id="toc" open><summary>Table of contents</summary><ul>
326<li><a href="#modules-ecmascript-modules">Modules: ECMAScript modules</a>
327<ul>
328<li><a href="#introduction">Introduction</a></li>
329<li><a href="#enabling">Enabling</a></li>
330<li><a href="#packages">Packages</a></li>
331<li><a href="#import-specifiers"><code>import</code> Specifiers</a>
332<ul>
333<li><a href="#terminology">Terminology</a></li>
334<li><a href="#mandatory-file-extensions">Mandatory file extensions</a></li>
335<li><a href="#urls">URLs</a>
336<ul>
337<li><a href="#file-urls"><code>file:</code> URLs</a></li>
338<li><a href="#data-imports"><code>data:</code> imports</a></li>
339<li><a href="#node-imports"><code>node:</code> imports</a></li>
340</ul>
341</li>
342</ul>
343</li>
344<li><span class="stability_1"><a href="#import-attributes">Import attributes</a></span></li>
345<li><a href="#builtin-modules">Builtin modules</a></li>
346<li><a href="#import-expressions"><code>import()</code> expressions</a></li>
347<li><a href="#importmeta"><code>import.meta</code></a>
348<ul>
349<li><a href="#importmetaurl"><code>import.meta.url</code></a></li>
350<li><span class="stability_1"><a href="#importmetaresolvespecifier"><code>import.meta.resolve(specifier)</code></a></span></li>
351</ul>
352</li>
353<li><a href="#interoperability-with-commonjs">Interoperability with CommonJS</a>
354<ul>
355<li><a href="#import-statements"><code>import</code> statements</a></li>
356<li><a href="#require"><code>require</code></a></li>
357<li><a href="#commonjs-namespaces">CommonJS Namespaces</a></li>
358<li><a href="#differences-between-es-modules-and-commonjs">Differences between ES modules and CommonJS</a>
359<ul>
360<li><a href="#no-require-exports-or-moduleexports">No <code>require</code>, <code>exports</code>, or <code>module.exports</code></a></li>
361<li><a href="#no-__filename-or-__dirname">No <code>__filename</code> or <code>__dirname</code></a></li>
362<li><a href="#no-addon-loading">No Addon Loading</a></li>
363<li><a href="#no-requireresolve">No <code>require.resolve</code></a></li>
364<li><a href="#no-node_path">No <code>NODE_PATH</code></a></li>
365<li><a href="#no-requireextensions">No <code>require.extensions</code></a></li>
366<li><a href="#no-requirecache">No <code>require.cache</code></a></li>
367</ul>
368</li>
369</ul>
370</li>
371<li><span class="stability_1"><a href="#json-modules">JSON modules</a></span></li>
372<li><span class="stability_1"><a href="#wasm-modules">Wasm modules</a></span></li>
373<li><a href="#top-level-await">Top-level <code>await</code></a></li>
374<li><span class="stability_1"><a href="#https-and-http-imports">HTTPS and HTTP imports</a></span>
375<ul>
376<li><a href="#imports-are-limited-to-http1">Imports are limited to HTTP/1</a></li>
377<li><a href="#http-is-limited-to-loopback-addresses">HTTP is limited to loopback addresses</a></li>
378<li><a href="#authentication-is-never-sent-to-the-destination-server">Authentication is never sent to the destination server.</a></li>
379<li><a href="#cors-is-never-checked-on-the-destination-server">CORS is never checked on the destination server</a></li>
380<li><a href="#cannot-load-non-network-dependencies">Cannot load non-network dependencies</a></li>
381<li><a href="#network-based-loading-is-not-enabled-by-default">Network-based loading is not enabled by default</a></li>
382</ul>
383</li>
384<li><a href="#loaders">Loaders</a></li>
385<li><a href="#resolution-and-loading-algorithm">Resolution and loading algorithm</a>
386<ul>
387<li><a href="#features">Features</a></li>
388<li><a href="#resolution-algorithm">Resolution algorithm</a></li>
389<li><a href="#resolution-algorithm-specification">Resolution Algorithm Specification</a></li>
390<li><span class="stability_1"><a href="#customizing-esm-specifier-resolution-algorithm">Customizing ESM specifier resolution algorithm</a></span></li>
391</ul>
392</li>
393</ul>
394</li>
395</ul></details>
396
397      <div id="apicontent">
398        <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>
399
400
401<div class="api_metadata">
402<details class="changelog"><summary>History</summary>
403<table>
404<tbody><tr><th>Version</th><th>Changes</th></tr>
405<tr><td>v18.20.0</td>
406<td><p>Add experimental support for import attributes.</p></td></tr>
407<tr><td>v18.19.0</td>
408<td><p>Module customization hooks are executed off the main thread.</p></td></tr>
409<tr><td>v18.6.0</td>
410<td><p>Add support for chaining module customization hooks.</p></td></tr>
411<tr><td>v17.1.0, v16.14.0</td>
412<td><p>Add experimental support for import assertions.</p></td></tr>
413<tr><td>v17.0.0, v16.12.0</td>
414<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>
415<tr><td>v14.8.0</td>
416<td><p>Unflag Top-Level Await.</p></td></tr>
417<tr><td>v15.3.0, v14.17.0, v12.22.0</td>
418<td><p>Stabilize modules implementation.</p></td></tr>
419<tr><td>v14.13.0, v12.20.0</td>
420<td><p>Support for detection of CommonJS named exports.</p></td></tr>
421<tr><td>v14.0.0, v13.14.0, v12.20.0</td>
422<td><p>Remove experimental modules warning.</p></td></tr>
423<tr><td>v13.2.0, v12.17.0</td>
424<td><p>Loading ECMAScript modules no longer requires a command-line flag.</p></td></tr>
425<tr><td>v12.0.0</td>
426<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>
427<tr><td>v8.5.0</td>
428<td><p><span>Added in: v8.5.0</span></p></td></tr>
429</tbody></table>
430</details>
431</div>
432<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
433<section><h3>Introduction<span><a class="mark" href="#introduction" id="introduction">#</a></span><a aria-hidden="true" class="legacy" id="esm_introduction"></a></h3>
434
435<p>ECMAScript modules are <a href="https://tc39.github.io/ecma262/#sec-modules">the official standard format</a> to package JavaScript
436code 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
437<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export"><code>export</code></a> statements.</p>
438<p>The following example of an ES module exports a function:</p>
439<pre><code class="language-js"><span class="hljs-comment">// addTwo.mjs</span>
440<span class="hljs-keyword">function</span> <span class="hljs-title function_">addTwo</span>(<span class="hljs-params">num</span>) {
441  <span class="hljs-keyword">return</span> num + <span class="hljs-number">2</span>;
442}
443
444<span class="hljs-keyword">export</span> { addTwo };</code> <button class="copy-button">copy</button></pre>
445<p>The following example of an ES module imports the function from <code>addTwo.mjs</code>:</p>
446<pre><code class="language-js"><span class="hljs-comment">// app.mjs</span>
447<span class="hljs-keyword">import</span> { addTwo } <span class="hljs-keyword">from</span> <span class="hljs-string">'./addTwo.mjs'</span>;
448
449<span class="hljs-comment">// Prints: 6</span>
450<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>
451<p>Node.js fully supports ECMAScript modules as they are currently specified and
452provides interoperability between them and its original module format,
453<a href="modules.html">CommonJS</a>.</p>
454<!-- Anchors to make sure old links find a target -->
455<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>
456</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>
457
458<p>Node.js has two module systems: <a href="modules.html">CommonJS</a> modules and ECMAScript modules.</p>
459<p>Authors can tell Node.js to use the ECMAScript modules loader via the <code>.mjs</code>
460file 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>
461flag, or the <a href="cli.html#--experimental-default-typetype"><code>--experimental-default-type</code></a> flag. Outside of those cases,
462Node.js will use the CommonJS module loader. See <a href="packages.html#determining-module-system">Determining module system</a>
463for more details.</p>
464<!-- Anchors to make sure old links find a target -->
465<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>
466</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>
467<p>This section was moved to <a href="packages.html">Modules: Packages</a>.</p>
468</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>
469<h4>Terminology<span><a class="mark" href="#terminology" id="terminology">#</a></span><a aria-hidden="true" class="legacy" id="esm_terminology"></a></h4>
470<p>The <em>specifier</em> of an <code>import</code> statement is the string after the <code>from</code> keyword,
471e.g. <code>'node:path'</code> in <code>import { sep } from 'node:path'</code>. Specifiers are also
472used in <code>export from</code> statements, and as the argument to an <code>import()</code>
473expression.</p>
474<p>There are three types of specifiers:</p>
475<ul>
476<li>
477<p><em>Relative specifiers</em> like <code>'./startup.js'</code> or <code>'../config.mjs'</code>. They refer
478to a path relative to the location of the importing file. <em>The file extension
479is always necessary for these.</em></p>
480</li>
481<li>
482<p><em>Bare specifiers</em> like <code>'some-package'</code> or <code>'some-package/shuffle'</code>. They can
483refer to the main entry point of a package by the package name, or a
484specific feature module within a package prefixed by the package name as per
485the examples respectively. <em>Including the file extension is only necessary
486for packages without an <a href="packages.html#exports"><code>"exports"</code></a> field.</em></p>
487</li>
488<li>
489<p><em>Absolute specifiers</em> like <code>'file:///opt/nodejs/config.js'</code>. They refer
490directly and explicitly to a full path.</p>
491</li>
492</ul>
493<p>Bare specifier resolutions are handled by the <a href="#resolution-algorithm-specification">Node.js module
494resolution and loading algorithm</a>.
495All other specifier resolutions are always only resolved with
496the standard relative <a href="https://url.spec.whatwg.org/">URL</a> resolution semantics.</p>
497<p>Like in CommonJS, module files within packages can be accessed by appending a
498path to the package name unless the package's <a href="packages.html#nodejs-packagejson-field-definitions"><code>package.json</code></a> contains an
499<a href="packages.html#exports"><code>"exports"</code></a> field, in which case files within packages can only be accessed
500via the paths defined in <a href="packages.html#exports"><code>"exports"</code></a>.</p>
501<p>For details on these package resolution rules that apply to bare specifiers in
502the Node.js module resolution, see the <a href="packages.html">packages documentation</a>.</p>
503<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>
504<p>A file extension must be provided when using the <code>import</code> keyword to resolve
505relative or absolute specifiers. Directory indexes (e.g. <code>'./startup/index.js'</code>)
506must also be fully specified.</p>
507<p>This behavior matches how <code>import</code> behaves in browser environments, assuming a
508typically configured server.</p>
509<h4>URLs<span><a class="mark" href="#urls" id="urls">#</a></span><a aria-hidden="true" class="legacy" id="esm_urls"></a></h4>
510<p>ES modules are resolved and cached as URLs. This means that special characters
511must 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>
512<p><code>file:</code>, <code>node:</code>, and <code>data:</code> URL schemes are supported. A specifier like
513<code>'https://example.com/app.js'</code> is not supported natively in Node.js unless using
514a <a href="module.html#import-from-https">custom HTTPS loader</a>.</p>
515<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>
516<p>Modules are loaded multiple times if the <code>import</code> specifier used to resolve
517them has a different query or fragment.</p>
518<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>
519<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>
520<p>The volume root may be referenced via <code>/</code>, <code>//</code>, or <code>file:///</code>. Given the
521differences between <a href="https://url.spec.whatwg.org/">URL</a> and path resolution (such as percent encoding
522details), it is recommended to use <a href="url.html#urlpathtofileurlpath">url.pathToFileURL</a> when importing a path.</p>
523<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>
524<div class="api_metadata">
525<span>Added in: v12.10.0</span>
526</div>
527<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>
528<ul>
529<li><code>text/javascript</code> for ES modules</li>
530<li><code>application/json</code> for JSON</li>
531<li><code>application/wasm</code> for Wasm</li>
532</ul>
533<pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'data:text/javascript,console.log("hello!");'</span>;
534<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>
535<p><code>data:</code> URLs only resolve <a href="#terminology">bare specifiers</a> for builtin modules
536and <a href="#terminology">absolute specifiers</a>. Resolving
537<a href="#terminology">relative specifiers</a> does not work because <code>data:</code> is not a
538<a href="https://url.spec.whatwg.org/#special-scheme">special scheme</a>. For example, attempting to load <code>./foo</code>
539from <code>data:text/javascript,import "./foo";</code> fails to resolve because there
540is no concept of relative resolution for <code>data:</code> URLs.</p>
541<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>
542<div class="api_metadata">
543<details class="changelog"><summary>History</summary>
544<table>
545<tbody><tr><th>Version</th><th>Changes</th></tr>
546<tr><td>v16.0.0, v14.18.0</td>
547<td><p>Added <code>node:</code> import support to <code>require(...)</code>.</p></td></tr>
548<tr><td>v14.13.1, v12.20.0</td>
549<td><p><span>Added in: v14.13.1, v12.20.0</span></p></td></tr>
550</tbody></table>
551</details>
552</div>
553<p><code>node:</code> URLs are supported as an alternative means to load Node.js builtin
554modules. This URL scheme allows for builtin modules to be referenced by valid
555absolute URL strings.</p>
556<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>
557<p><a id="import-assertions"></a></p>
558</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>
559<div class="api_metadata">
560<details class="changelog"><summary>History</summary>
561<table>
562<tbody><tr><th>Version</th><th>Changes</th></tr>
563<tr><td>v18.20.0</td>
564<td><p>Switch from Import Assertions to Import Attributes.</p></td></tr>
565<tr><td>v17.1.0, v16.14.0</td>
566<td><p><span>Added in: v17.1.0, v16.14.0</span></p></td></tr>
567</tbody></table>
568</details>
569</div>
570<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a>.1 - Active development</div><p></p>
571<blockquote>
572<p>This feature was previously named "Import assertions", and using the <code>assert</code>
573keyword instead of <code>with</code>. Any uses in code of the prior <code>assert</code> keyword
574should be updated to use <code>with</code> instead.</p>
575</blockquote>
576<p>The <a href="https://github.com/tc39/proposal-import-attributes">Import Attributes proposal</a> adds an inline syntax for module import
577statements to pass on more information alongside the module specifier.</p>
578<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> };
579
580<span class="hljs-keyword">const</span> { <span class="hljs-attr">default</span>: barData } =
581  <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>
582<p>Node.js supports the following <code>type</code> values, for which the attribute is
583mandatory:</p>
584
585
586
587
588
589
590
591
592
593
594
595
596
597<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>
598</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>
599<p><a href="modules.html#core-modules">Core modules</a> provide named exports of their public API. A
600default export is also provided which is the value of the CommonJS exports.
601The default export can be used for, among other things, modifying the named
602exports. Named exports of builtin modules are updated only by calling
603<a href="module.html#modulesyncbuiltinesmexports"><code>module.syncBuiltinESMExports()</code></a>.</p>
604<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>;
605<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>
606<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>;
607<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> {
608  <span class="hljs-keyword">if</span> (err) {
609    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
610  } <span class="hljs-keyword">else</span> {
611    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(source);
612  }
613});</code> <button class="copy-button">copy</button></pre>
614<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>;
615<span class="hljs-keyword">import</span> { syncBuiltinESMExports } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:module'</span>;
616<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>;
617
618fs.<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>);
619<span class="hljs-title function_">syncBuiltinESMExports</span>();
620
621fs.<span class="hljs-property">readFileSync</span> === readFileSync;</code> <button class="copy-button">copy</button></pre>
622</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>
623<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
624modules it can be used to load ES modules.</p>
625</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>
626<ul>
627<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
628</ul>
629<p>The <code>import.meta</code> meta property is an <code>Object</code> that contains the following
630properties.</p>
631<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>
632<ul>
633<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The absolute <code>file:</code> URL of the module.</li>
634</ul>
635<p>This is defined exactly the same as it is in browsers providing the URL of the
636current module file.</p>
637<p>This enables useful patterns such as relative file loading:</p>
638<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>;
639<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>
640<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>
641<div class="api_metadata">
642<details class="changelog"><summary>History</summary>
643<table>
644<tbody><tr><th>Version</th><th>Changes</th></tr>
645<tr><td>v18.19.0</td>
646<td><p>Unflag <code>import.meta.resolve``, with </code>parentURL` parameter still flagged.</p></td></tr>
647<tr><td>v18.19.0</td>
648<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>
649<tr><td>v18.19.0</td>
650<td><p>This API now returns a string synchronously instead of a Promise.</p></td></tr>
651<tr><td>v16.2.0, v14.18.0</td>
652<td><p>Add support for WHATWG <code>URL</code> object to <code>parentURL</code> parameter.</p></td></tr>
653<tr><td>v13.9.0, v12.16.2</td>
654<td><p><span>Added in: v13.9.0, v12.16.2</span></p></td></tr>
655</tbody></table>
656</details>
657</div>
658<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a>.2 - Release candidate</div><p></p>
659<ul>
660<li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The module specifier to resolve relative to the
661current module.</li>
662<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The absolute URL string that the specifier would resolve to.</li>
663</ul>
664<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
665each module, returning the URL string.</p>
666<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>);
667<span class="hljs-comment">// file:///app/node_modules/component-lib/asset.css</span>
668<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>);
669<span class="hljs-comment">// file:///app/dep.js</span></code> <button class="copy-button">copy</button></pre>
670<p>All features of the Node.js module resolution are supported. Dependency
671resolutions are subject to the permitted exports resolutions within the package.</p>
672<p><strong>Caveats</strong>:</p>
673<ul>
674<li>This can result in synchronous file-system operations, which
675can impact performance similarly to <code>require.resolve</code>.</li>
676<li>This feature is not available within custom loaders (it would
677create a deadlock).</li>
678</ul>
679<p><strong>Non-standard API</strong>:</p>
680<p>When using the <code>--experimental-import-meta-resolve</code> flag, that function accepts
681a second argument:</p>
682<ul>
683<li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> An optional absolute parent module URL to resolve from.
684<strong>Default:</strong> <code>import.meta.url</code></li>
685</ul>
686</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>
687<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>
688<p>An <code>import</code> statement can reference an ES module or a CommonJS module.
689<code>import</code> statements are permitted only in ES modules, but dynamic <a href="#import-expressions"><code>import()</code></a>
690expressions are supported in CommonJS for loading ES modules.</p>
691<p>When importing <a href="#commonjs-namespaces">CommonJS modules</a>, the
692<code>module.exports</code> object is provided as the default export. Named exports may be
693available, provided by static analysis as a convenience for better ecosystem
694compatibility.</p>
695<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>
696<p>The CommonJS module <code>require</code> always treats the files it references as CommonJS.</p>
697<p>Using <code>require</code> to load an ES module is not supported because ES modules have
698asynchronous execution. Instead, use <a href="#import-expressions"><code>import()</code></a> to load an ES module
699from a CommonJS module.</p>
700<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>
701<p>CommonJS modules consist of a <code>module.exports</code> object which can be of any type.</p>
702<p>When importing a CommonJS module, it can be reliably imported using the ES
703module default import or its corresponding sugar syntax:</p>
704<!-- eslint-disable no-duplicate-imports -->
705<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>;
706
707<span class="hljs-comment">// The following import statement is "syntax sugar" (equivalent but sweeter)</span>
708<span class="hljs-comment">// for `{ default as cjsSugar }` in the above import statement:</span>
709<span class="hljs-keyword">import</span> cjsSugar <span class="hljs-keyword">from</span> <span class="hljs-string">'cjs'</span>;
710
711<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(cjs);
712<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(cjs === cjsSugar);
713<span class="hljs-comment">// Prints:</span>
714<span class="hljs-comment">//   &#x3C;module.exports></span>
715<span class="hljs-comment">//   true</span></code> <button class="copy-button">copy</button></pre>
716<p>The ECMAScript Module Namespace representation of a CommonJS module is always
717a namespace with a <code>default</code> export key pointing to the CommonJS
718<code>module.exports</code> value.</p>
719<p>This Module Namespace Exotic Object can be directly observed either when using
720<code>import * as m from 'cjs'</code> or a dynamic import:</p>
721<!-- eslint-skip -->
722<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>;
723<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(m);
724<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>));
725<span class="hljs-comment">// Prints:</span>
726<span class="hljs-comment">//   [Module] { default: &#x3C;module.exports> }</span>
727<span class="hljs-comment">//   true</span></code> <button class="copy-button">copy</button></pre>
728<p>For better compatibility with existing usage in the JS ecosystem, Node.js
729in addition attempts to determine the CommonJS named exports of every imported
730CommonJS module to provide them as separate ES module exports using a static
731analysis process.</p>
732<p>For example, consider a CommonJS module written:</p>
733<pre><code class="language-js cjs"><span class="hljs-comment">// cjs.cjs</span>
734<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>
735<p>The preceding module supports named imports in ES modules:</p>
736<!-- eslint-disable no-duplicate-imports -->
737<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>;
738<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name);
739<span class="hljs-comment">// Prints: 'exported'</span>
740
741<span class="hljs-keyword">import</span> cjs <span class="hljs-keyword">from</span> <span class="hljs-string">'./cjs.cjs'</span>;
742<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(cjs);
743<span class="hljs-comment">// Prints: { name: 'exported' }</span>
744
745<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>;
746<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(m);
747<span class="hljs-comment">// Prints: [Module] { default: { name: 'exported' }, name: 'exported' }</span></code> <button class="copy-button">copy</button></pre>
748<p>As can be seen from the last example of the Module Namespace Exotic Object being
749logged, the <code>name</code> export is copied off of the <code>module.exports</code> object and set
750directly on the ES module namespace when the module is imported.</p>
751<p>Live binding updates or new exports added to <code>module.exports</code> are not detected
752for these named exports.</p>
753<p>The detection of named exports is based on common syntax patterns but does not
754always correctly detect named exports. In these cases, using the default
755import form described above can be a better option.</p>
756<p>Named exports detection covers many common export patterns, reexport patterns
757and 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
758semantics implemented.</p>
759<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>
760<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>
761<p>In most cases, the ES module <code>import</code> can be used to load CommonJS modules.</p>
762<p>If needed, a <code>require</code> function can be constructed within an ES module using
763<a href="module.html#modulecreaterequirefilename"><code>module.createRequire()</code></a>.</p>
764<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>
765<p>These CommonJS variables are not available in ES modules.</p>
766<p><code>__filename</code> and <code>__dirname</code> use cases can be replicated via
767<a href="#importmetaurl"><code>import.meta.url</code></a>.</p>
768<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>
769<p><a href="addons.html">Addons</a> are not currently supported with ES module imports.</p>
770<p>They can instead be loaded with <a href="module.html#modulecreaterequirefilename"><code>module.createRequire()</code></a> or
771<a href="process.html#processdlopenmodule-filename-flags"><code>process.dlopen</code></a>.</p>
772<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>
773<p>Relative resolution can be handled via <code>new URL('./local', import.meta.url)</code>.</p>
774<p>For a complete <code>require.resolve</code> replacement, there is the
775<a href="#importmetaresolvespecifier">import.meta.resolve</a> API.</p>
776<p>Alternatively <code>module.createRequire()</code> can be used.</p>
777<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>
778<p><code>NODE_PATH</code> is not part of resolving <code>import</code> specifiers. Please use symlinks
779if this behavior is desired.</p>
780<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>
781<p><code>require.extensions</code> is not used by <code>import</code>. Module customization hooks can
782provide a replacement.</p>
783<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>
784<p><code>require.cache</code> is not used by <code>import</code> as the ES module loader has its own
785separate cache.</p>
786<p><i id="esm_experimental_json_modules"></i></p>
787</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>
788<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
789<p>JSON files can be referenced by <code>import</code>:</p>
790<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>
791<p>The <code>with { type: 'json' }</code> syntax is mandatory; see <a href="#import-attributes">Import Attributes</a>.</p>
792<p>The imported JSON only exposes a <code>default</code> export. There is no support for named
793exports. A cache entry is created in the CommonJS cache to avoid duplication.
794The same object is returned in CommonJS if the JSON module has already been
795imported from the same path.</p>
796<p><i id="esm_experimental_wasm_modules"></i></p>
797</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>
798<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
799<p>Importing WebAssembly modules is supported under the
800<code>--experimental-wasm-modules</code> flag, allowing any <code>.wasm</code> files to be
801imported as normal modules while also supporting their module imports.</p>
802<p>This integration is in line with the
803<a href="https://github.com/webassembly/esm-integration">ES Module Integration Proposal for WebAssembly</a>.</p>
804<p>For example, an <code>index.mjs</code> containing:</p>
805<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>;
806<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(M);</code> <button class="copy-button">copy</button></pre>
807<p>executed under:</p>
808<pre><code class="language-bash">node --experimental-wasm-modules index.mjs</code> <button class="copy-button">copy</button></pre>
809<p>would provide the exports interface for the instantiation of <code>module.wasm</code>.</p>
810<p><i id="esm_experimental_top_level_await"></i></p>
811</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>
812<div class="api_metadata">
813<span>Added in: v14.8.0</span>
814</div>
815<p>The <code>await</code> keyword may be used in the top level body of an ECMAScript module.</p>
816<p>Assuming an <code>a.mjs</code> with</p>
817<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>
818<p>And a <code>b.mjs</code> with</p>
819<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>;
820
821<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>
822<pre><code class="language-bash">node b.mjs <span class="hljs-comment"># works</span></code> <button class="copy-button">copy</button></pre>
823<p>If a top level <code>await</code> expression never resolves, the <code>node</code> process will exit
824with a <code>13</code> <a href="process.html#exit-codes">status code</a>.</p>
825<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>;
826<span class="hljs-keyword">import</span> { execPath } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>;
827
828<span class="hljs-title function_">spawn</span>(execPath, [
829  <span class="hljs-string">'--input-type=module'</span>,
830  <span class="hljs-string">'--eval'</span>,
831  <span class="hljs-comment">// Never-resolving Promise:</span>
832  <span class="hljs-string">'await new Promise(() => {})'</span>,
833]).<span class="hljs-title function_">once</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">code</span>) =></span> {
834  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(code); <span class="hljs-comment">// Logs `13`</span>
835});</code> <button class="copy-button">copy</button></pre>
836</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>
837<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
838<p>Importing network based modules using <code>https:</code> and <code>http:</code> is supported under
839the <code>--experimental-network-imports</code> flag. This allows web browser-like imports
840to work in Node.js with a few differences due to application stability and
841security concerns that are different when running in a privileged environment
842instead of a browser sandbox.</p>
843<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>
844<p>Automatic protocol negotiation for HTTP/2 and HTTP/3 is not yet supported.</p>
845<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>
846<p><code>http:</code> is vulnerable to man-in-the-middle attacks and is not allowed to be
847used for addresses outside of the IPv4 address <code>127.0.0.0/8</code> (<code>127.0.0.1</code> to
848<code>127.255.255.255</code>) and the IPv6 address <code>::1</code>. Support for <code>http:</code> is intended
849to be used for local development.</p>
850<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>
851<p><code>Authorization</code>, <code>Cookie</code>, and <code>Proxy-Authorization</code> headers are not sent to the
852server. Avoid including user info in parts of imported URLs. A security model
853for safely using these on the server is being worked on.</p>
854<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>
855<p>CORS is designed to allow a server to limit the consumers of an API to a
856specific set of hosts. This is not supported as it does not make sense for a
857server-based implementation.</p>
858<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>
859<p>These modules cannot access other modules that are not over <code>http:</code> or <code>https:</code>.
860To still access local modules while avoiding the security concern, pass in
861references to the local dependencies:</p>
862<pre><code class="language-js mjs"><span class="hljs-comment">// file.mjs</span>
863<span class="hljs-keyword">import</span> worker_threads <span class="hljs-keyword">from</span> <span class="hljs-string">'node:worker_threads'</span>;
864<span class="hljs-keyword">import</span> { configure, resize } <span class="hljs-keyword">from</span> <span class="hljs-string">'https://example.com/imagelib.mjs'</span>;
865<span class="hljs-title function_">configure</span>({ worker_threads });</code> <button class="copy-button">copy</button></pre>
866<pre><code class="language-js mjs"><span class="hljs-comment">// https://example.com/imagelib.mjs</span>
867<span class="hljs-keyword">let</span> worker_threads;
868<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>) {
869  worker_threads = opts.<span class="hljs-property">worker_threads</span>;
870}
871<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>) {
872  <span class="hljs-comment">// Perform resizing in worker_thread to avoid main thread blocking</span>
873}</code> <button class="copy-button">copy</button></pre>
874<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>
875<p>For now, the <code>--experimental-network-imports</code> flag is required to enable loading
876resources over <code>http:</code> or <code>https:</code>. In the future, a different mechanism will be
877used to enforce this. Opt-in is required to prevent transitive dependencies
878inadvertently using potentially mutable state that could affect reliability
879of Node.js applications.</p>
880<p><i id="esm_experimental_loaders"></i></p>
881</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>
882<p>The former Loaders documentation is now at
883<a href="module.html#customization-hooks">Modules: Customization hooks</a>.</p>
884</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>
885<h4>Features<span><a class="mark" href="#features" id="features">#</a></span><a aria-hidden="true" class="legacy" id="esm_features"></a></h4>
886<p>The default resolver has the following properties:</p>
887<ul>
888<li>FileURL-based resolution as is used by ES modules</li>
889<li>Relative and absolute URL resolution</li>
890<li>No default extensions</li>
891<li>No folder mains</li>
892<li>Bare specifier package resolution lookup through node_modules</li>
893<li>Does not fail on unknown extensions or protocols</li>
894<li>Can optionally provide a hint of the format to the loading phase</li>
895</ul>
896<p>The default loader has the following properties</p>
897<ul>
898<li>Support for builtin module loading via <code>node:</code> URLs</li>
899<li>Support for "inline" module loading via <code>data:</code> URLs</li>
900<li>Support for <code>file:</code> module loading</li>
901<li>Fails on any other URL protocol</li>
902<li>Fails on unknown extensions for <code>file:</code> loading
903(supports only <code>.cjs</code>, <code>.js</code>, and <code>.mjs</code>)</li>
904</ul>
905<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>
906<p>The algorithm to load an ES module specifier is given through the
907<strong>ESM_RESOLVE</strong> method below. It returns the resolved URL for a
908module specifier relative to a parentURL.</p>
909<p>The resolution algorithm determines the full resolved URL for a module
910load, along with its suggested module format. The resolution algorithm
911does not determine whether the resolved URL protocol can be loaded,
912or whether the file extensions are permitted, instead these validations
913are applied by Node.js during the load phase
914(for example, if it was asked to load a URL that has a protocol that is
915not <code>file:</code>, <code>data:</code>, <code>node:</code>, or if <code>--experimental-network-imports</code>
916is enabled, <code>https:</code>).</p>
917<p>The algorithm also tries to determine the format of the file based
918on the extension (see <code>ESM_FILE_FORMAT</code> algorithm below). If it does
919not recognize the file extension (eg if it is not <code>.mjs</code>, <code>.cjs</code>, or
920<code>.json</code>), then a format of <code>undefined</code> is returned,
921which will throw during the load phase.</p>
922<p>The algorithm to determine the module format of a resolved URL is
923provided by <strong>ESM_FILE_FORMAT</strong>, which returns the unique module
924format for any file. The <em>"module"</em> format is returned for an ECMAScript
925Module, while the <em>"commonjs"</em> format is used to indicate loading through the
926legacy CommonJS loader. Additional formats such as <em>"addon"</em> can be extended in
927future updates.</p>
928<p>In the following algorithms, all subroutine errors are propagated as errors
929of these top-level routines unless stated otherwise.</p>
930<p><em>defaultConditions</em> is the conditional environment name array,
931<code>["node", "import"]</code>.</p>
932<p>The resolver can throw the following errors:</p>
933<ul>
934<li><em>Invalid Module Specifier</em>: Module specifier is an invalid URL, package name
935or package subpath specifier.</li>
936<li><em>Invalid Package Configuration</em>: package.json configuration is invalid or
937contains an invalid configuration.</li>
938<li><em>Invalid Package Target</em>: Package exports or imports define a target module
939for the package that is an invalid type or string target.</li>
940<li><em>Package Path Not Exported</em>: Package exports do not define or permit a target
941subpath in the package for the given module.</li>
942<li><em>Package Import Not Defined</em>: Package imports do not define the specifier.</li>
943<li><em>Module Not Found</em>: The package or module requested does not exist.</li>
944<li><em>Unsupported Directory Import</em>: The resolved path corresponds to a directory,
945which is not a supported target for module imports.</li>
946</ul>
947<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>
948<p><strong>ESM_RESOLVE</strong>(<em>specifier</em>, <em>parentURL</em>)</p>
949<blockquote>
950<ol>
951<li>Let <em>resolved</em> be <strong>undefined</strong>.</li>
952<li>If <em>specifier</em> is a valid URL, then
953<ol>
954<li>Set <em>resolved</em> to the result of parsing and reserializing
955<em>specifier</em> as a URL.</li>
956</ol>
957</li>
958<li>Otherwise, if <em>specifier</em> starts with <em>"/"</em>, <em>"./"</em>, or <em>"../"</em>, then
959<ol>
960<li>Set <em>resolved</em> to the URL resolution of <em>specifier</em> relative to
961<em>parentURL</em>.</li>
962</ol>
963</li>
964<li>Otherwise, if <em>specifier</em> starts with <em>"#"</em>, then
965<ol>
966<li>Set <em>resolved</em> to the result of
967<strong>PACKAGE_IMPORTS_RESOLVE</strong>(<em>specifier</em>,
968<em>parentURL</em>, <em>defaultConditions</em>).</li>
969</ol>
970</li>
971<li>Otherwise,
972<ol>
973<li>Note: <em>specifier</em> is now a bare specifier.</li>
974<li>Set <em>resolved</em> the result of
975<strong>PACKAGE_RESOLVE</strong>(<em>specifier</em>, <em>parentURL</em>).</li>
976</ol>
977</li>
978<li>Let <em>format</em> be <strong>undefined</strong>.</li>
979<li>If <em>resolved</em> is a <em>"file:"</em> URL, then
980<ol>
981<li>If <em>resolved</em> contains any percent encodings of <em>"/"</em> or <em>"\"</em> (<em>"%2F"</em>
982and <em>"%5C"</em> respectively), then
983<ol>
984<li>Throw an <em>Invalid Module Specifier</em> error.</li>
985</ol>
986</li>
987<li>If the file at <em>resolved</em> is a directory, then
988<ol>
989<li>Throw an <em>Unsupported Directory Import</em> error.</li>
990</ol>
991</li>
992<li>If the file at <em>resolved</em> does not exist, then
993<ol>
994<li>Throw a <em>Module Not Found</em> error.</li>
995</ol>
996</li>
997<li>Set <em>resolved</em> to the real path of <em>resolved</em>, maintaining the
998same URL querystring and fragment components.</li>
999<li>Set <em>format</em> to the result of <strong>ESM_FILE_FORMAT</strong>(<em>resolved</em>).</li>
1000</ol>
1001</li>
1002<li>Otherwise,
1003<ol>
1004<li>Set <em>format</em> the module format of the content type associated with the
1005URL <em>resolved</em>.</li>
1006</ol>
1007</li>
1008<li>Return <em>format</em> and <em>resolved</em> to the loading phase</li>
1009</ol>
1010</blockquote>
1011<p><strong>PACKAGE_RESOLVE</strong>(<em>packageSpecifier</em>, <em>parentURL</em>)</p>
1012<blockquote>
1013<ol>
1014<li>Let <em>packageName</em> be <strong>undefined</strong>.</li>
1015<li>If <em>packageSpecifier</em> is an empty string, then
1016<ol>
1017<li>Throw an <em>Invalid Module Specifier</em> error.</li>
1018</ol>
1019</li>
1020<li>If <em>packageSpecifier</em> is a Node.js builtin module name, then
1021<ol>
1022<li>Return the string <em>"node:"</em> concatenated with <em>packageSpecifier</em>.</li>
1023</ol>
1024</li>
1025<li>If <em>packageSpecifier</em> does not start with <em>"@"</em>, then
1026<ol>
1027<li>Set <em>packageName</em> to the substring of <em>packageSpecifier</em> until the first
1028<em>"/"</em> separator or the end of the string.</li>
1029</ol>
1030</li>
1031<li>Otherwise,
1032<ol>
1033<li>If <em>packageSpecifier</em> does not contain a <em>"/"</em> separator, then
1034<ol>
1035<li>Throw an <em>Invalid Module Specifier</em> error.</li>
1036</ol>
1037</li>
1038<li>Set <em>packageName</em> to the substring of <em>packageSpecifier</em>
1039until the second <em>"/"</em> separator or the end of the string.</li>
1040</ol>
1041</li>
1042<li>If <em>packageName</em> starts with <em>"."</em> or contains <em>"\"</em> or <em>"%"</em>, then
1043<ol>
1044<li>Throw an <em>Invalid Module Specifier</em> error.</li>
1045</ol>
1046</li>
1047<li>Let <em>packageSubpath</em> be <em>"."</em> concatenated with the substring of
1048<em>packageSpecifier</em> from the position at the length of <em>packageName</em>.</li>
1049<li>If <em>packageSubpath</em> ends in <em>"/"</em>, then
1050<ol>
1051<li>Throw an <em>Invalid Module Specifier</em> error.</li>
1052</ol>
1053</li>
1054<li>Let <em>selfUrl</em> be the result of
1055<strong>PACKAGE_SELF_RESOLVE</strong>(<em>packageName</em>, <em>packageSubpath</em>, <em>parentURL</em>).</li>
1056<li>If <em>selfUrl</em> is not <strong>undefined</strong>, return <em>selfUrl</em>.</li>
1057<li>While <em>parentURL</em> is not the file system root,
1058<ol>
1059<li>Let <em>packageURL</em> be the URL resolution of <em>"node_modules/"</em>
1060concatenated with <em>packageSpecifier</em>, relative to <em>parentURL</em>.</li>
1061<li>Set <em>parentURL</em> to the parent folder URL of <em>parentURL</em>.</li>
1062<li>If the folder at <em>packageURL</em> does not exist, then
1063<ol>
1064<li>Continue the next loop iteration.</li>
1065</ol>
1066</li>
1067<li>Let <em>pjson</em> be the result of <strong>READ_PACKAGE_JSON</strong>(<em>packageURL</em>).</li>
1068<li>If <em>pjson</em> is not <strong>null</strong> and <em>pjson</em>.<em>exports</em> is not <strong>null</strong> or
1069<strong>undefined</strong>, then
1070<ol>
1071<li>Return the result of <strong>PACKAGE_EXPORTS_RESOLVE</strong>(<em>packageURL</em>,
1072<em>packageSubpath</em>, <em>pjson.exports</em>, <em>defaultConditions</em>).</li>
1073</ol>
1074</li>
1075<li>Otherwise, if <em>packageSubpath</em> is equal to <em>"."</em>, then
1076<ol>
1077<li>If <em>pjson.main</em> is a string, then
1078<ol>
1079<li>Return the URL resolution of <em>main</em> in <em>packageURL</em>.</li>
1080</ol>
1081</li>
1082</ol>
1083</li>
1084<li>Otherwise,
1085<ol>
1086<li>Return the URL resolution of <em>packageSubpath</em> in <em>packageURL</em>.</li>
1087</ol>
1088</li>
1089</ol>
1090</li>
1091<li>Throw a <em>Module Not Found</em> error.</li>
1092</ol>
1093</blockquote>
1094<p><strong>PACKAGE_SELF_RESOLVE</strong>(<em>packageName</em>, <em>packageSubpath</em>, <em>parentURL</em>)</p>
1095<blockquote>
1096<ol>
1097<li>Let <em>packageURL</em> be the result of <strong>LOOKUP_PACKAGE_SCOPE</strong>(<em>parentURL</em>).</li>
1098<li>If <em>packageURL</em> is <strong>null</strong>, then
1099<ol>
1100<li>Return <strong>undefined</strong>.</li>
1101</ol>
1102</li>
1103<li>Let <em>pjson</em> be the result of <strong>READ_PACKAGE_JSON</strong>(<em>packageURL</em>).</li>
1104<li>If <em>pjson</em> is <strong>null</strong> or if <em>pjson</em>.<em>exports</em> is <strong>null</strong> or
1105<strong>undefined</strong>, then
1106<ol>
1107<li>Return <strong>undefined</strong>.</li>
1108</ol>
1109</li>
1110<li>If <em>pjson.name</em> is equal to <em>packageName</em>, then
1111<ol>
1112<li>Return the result of <strong>PACKAGE_EXPORTS_RESOLVE</strong>(<em>packageURL</em>,
1113<em>packageSubpath</em>, <em>pjson.exports</em>, <em>defaultConditions</em>).</li>
1114</ol>
1115</li>
1116<li>Otherwise, return <strong>undefined</strong>.</li>
1117</ol>
1118</blockquote>
1119<p><strong>PACKAGE_EXPORTS_RESOLVE</strong>(<em>packageURL</em>, <em>subpath</em>, <em>exports</em>, <em>conditions</em>)</p>
1120<blockquote>
1121<ol>
1122<li>If <em>exports</em> is an Object with both a key starting with <em>"."</em> and a key not
1123starting with <em>"."</em>, throw an <em>Invalid Package Configuration</em> error.</li>
1124<li>If <em>subpath</em> is equal to <em>"."</em>, then
1125<ol>
1126<li>Let <em>mainExport</em> be <strong>undefined</strong>.</li>
1127<li>If <em>exports</em> is a String or Array, or an Object containing no keys
1128starting with <em>"."</em>, then
1129<ol>
1130<li>Set <em>mainExport</em> to <em>exports</em>.</li>
1131</ol>
1132</li>
1133<li>Otherwise if <em>exports</em> is an Object containing a <em>"."</em> property, then
1134<ol>
1135<li>Set <em>mainExport</em> to <em>exports</em>[<em>"."</em>].</li>
1136</ol>
1137</li>
1138<li>If <em>mainExport</em> is not <strong>undefined</strong>, then
1139<ol>
1140<li>Let <em>resolved</em> be the result of <strong>PACKAGE_TARGET_RESOLVE</strong>(
1141<em>packageURL</em>, <em>mainExport</em>, <strong>null</strong>, <strong>false</strong>, <em>conditions</em>).</li>
1142<li>If <em>resolved</em> is not <strong>null</strong> or <strong>undefined</strong>, return <em>resolved</em>.</li>
1143</ol>
1144</li>
1145</ol>
1146</li>
1147<li>Otherwise, if <em>exports</em> is an Object and all keys of <em>exports</em> start with
1148<em>"."</em>, then
1149<ol>
1150<li>Let <em>matchKey</em> be the string <em>"./"</em> concatenated with <em>subpath</em>.</li>
1151<li>Let <em>resolved</em> be the result of <strong>PACKAGE_IMPORTS_EXPORTS_RESOLVE</strong>(
1152<em>matchKey</em>, <em>exports</em>, <em>packageURL</em>, <strong>false</strong>, <em>conditions</em>).</li>
1153<li>If <em>resolved</em> is not <strong>null</strong> or <strong>undefined</strong>, return <em>resolved</em>.</li>
1154</ol>
1155</li>
1156<li>Throw a <em>Package Path Not Exported</em> error.</li>
1157</ol>
1158</blockquote>
1159<p><strong>PACKAGE_IMPORTS_RESOLVE</strong>(<em>specifier</em>, <em>parentURL</em>, <em>conditions</em>)</p>
1160<blockquote>
1161<ol>
1162<li>Assert: <em>specifier</em> begins with <em>"#"</em>.</li>
1163<li>If <em>specifier</em> is exactly equal to <em>"#"</em> or starts with <em>"#/"</em>, then
1164<ol>
1165<li>Throw an <em>Invalid Module Specifier</em> error.</li>
1166</ol>
1167</li>
1168<li>Let <em>packageURL</em> be the result of <strong>LOOKUP_PACKAGE_SCOPE</strong>(<em>parentURL</em>).</li>
1169<li>If <em>packageURL</em> is not <strong>null</strong>, then
1170<ol>
1171<li>Let <em>pjson</em> be the result of <strong>READ_PACKAGE_JSON</strong>(<em>packageURL</em>).</li>
1172<li>If <em>pjson.imports</em> is a non-null Object, then
1173<ol>
1174<li>Let <em>resolved</em> be the result of
1175<strong>PACKAGE_IMPORTS_EXPORTS_RESOLVE</strong>(
1176<em>specifier</em>, <em>pjson.imports</em>, <em>packageURL</em>, <strong>true</strong>, <em>conditions</em>).</li>
1177<li>If <em>resolved</em> is not <strong>null</strong> or <strong>undefined</strong>, return <em>resolved</em>.</li>
1178</ol>
1179</li>
1180</ol>
1181</li>
1182<li>Throw a <em>Package Import Not Defined</em> error.</li>
1183</ol>
1184</blockquote>
1185<p><strong>PACKAGE_IMPORTS_EXPORTS_RESOLVE</strong>(<em>matchKey</em>, <em>matchObj</em>, <em>packageURL</em>,
1186<em>isImports</em>, <em>conditions</em>)</p>
1187<blockquote>
1188<ol>
1189<li>If <em>matchKey</em> is a key of <em>matchObj</em> and does not contain <em>"*"</em>, then
1190<ol>
1191<li>Let <em>target</em> be the value of <em>matchObj</em>[<em>matchKey</em>].</li>
1192<li>Return the result of <strong>PACKAGE_TARGET_RESOLVE</strong>(<em>packageURL</em>,
1193<em>target</em>, <strong>null</strong>, <em>isImports</em>, <em>conditions</em>).</li>
1194</ol>
1195</li>
1196<li>Let <em>expansionKeys</em> be the list of keys of <em>matchObj</em> containing only a
1197single <em>"*"</em>, sorted by the sorting function <strong>PATTERN_KEY_COMPARE</strong>
1198which orders in descending order of specificity.</li>
1199<li>For each key <em>expansionKey</em> in <em>expansionKeys</em>, do
1200<ol>
1201<li>Let <em>patternBase</em> be the substring of <em>expansionKey</em> up to but excluding
1202the first <em>"*"</em> character.</li>
1203<li>If <em>matchKey</em> starts with but is not equal to <em>patternBase</em>, then
1204<ol>
1205<li>Let <em>patternTrailer</em> be the substring of <em>expansionKey</em> from the
1206index after the first <em>"*"</em> character.</li>
1207<li>If <em>patternTrailer</em> has zero length, or if <em>matchKey</em> ends with
1208<em>patternTrailer</em> and the length of <em>matchKey</em> is greater than or
1209equal to the length of <em>expansionKey</em>, then
1210<ol>
1211<li>Let <em>target</em> be the value of <em>matchObj</em>[<em>expansionKey</em>].</li>
1212<li>Let <em>patternMatch</em> be the substring of <em>matchKey</em> starting at the
1213index of the length of <em>patternBase</em> up to the length of
1214<em>matchKey</em> minus the length of <em>patternTrailer</em>.</li>
1215<li>Return the result of <strong>PACKAGE_TARGET_RESOLVE</strong>(<em>packageURL</em>,
1216<em>target</em>, <em>patternMatch</em>, <em>isImports</em>, <em>conditions</em>).</li>
1217</ol>
1218</li>
1219</ol>
1220</li>
1221</ol>
1222</li>
1223<li>Return <strong>null</strong>.</li>
1224</ol>
1225</blockquote>
1226<p><strong>PATTERN_KEY_COMPARE</strong>(<em>keyA</em>, <em>keyB</em>)</p>
1227<blockquote>
1228<ol>
1229<li>Assert: <em>keyA</em> ends with <em>"/"</em> or contains only a single <em>"*"</em>.</li>
1230<li>Assert: <em>keyB</em> ends with <em>"/"</em> or contains only a single <em>"*"</em>.</li>
1231<li>Let <em>baseLengthA</em> be the index of <em>"*"</em> in <em>keyA</em> plus one, if <em>keyA</em>
1232contains <em>"*"</em>, or the length of <em>keyA</em> otherwise.</li>
1233<li>Let <em>baseLengthB</em> be the index of <em>"*"</em> in <em>keyB</em> plus one, if <em>keyB</em>
1234contains <em>"*"</em>, or the length of <em>keyB</em> otherwise.</li>
1235<li>If <em>baseLengthA</em> is greater than <em>baseLengthB</em>, return -1.</li>
1236<li>If <em>baseLengthB</em> is greater than <em>baseLengthA</em>, return 1.</li>
1237<li>If <em>keyA</em> does not contain <em>"*"</em>, return 1.</li>
1238<li>If <em>keyB</em> does not contain <em>"*"</em>, return -1.</li>
1239<li>If the length of <em>keyA</em> is greater than the length of <em>keyB</em>, return -1.</li>
1240<li>If the length of <em>keyB</em> is greater than the length of <em>keyA</em>, return 1.</li>
1241<li>Return 0.</li>
1242</ol>
1243</blockquote>
1244<p><strong>PACKAGE_TARGET_RESOLVE</strong>(<em>packageURL</em>, <em>target</em>, <em>patternMatch</em>,
1245<em>isImports</em>, <em>conditions</em>)</p>
1246<blockquote>
1247<ol>
1248<li>If <em>target</em> is a String, then
1249<ol>
1250<li>If <em>target</em> does not start with <em>"./"</em>, then
1251<ol>
1252<li>If <em>isImports</em> is <strong>false</strong>, or if <em>target</em> starts with <em>"../"</em> or
1253<em>"/"</em>, or if <em>target</em> is a valid URL, then
1254<ol>
1255<li>Throw an <em>Invalid Package Target</em> error.</li>
1256</ol>
1257</li>
1258<li>If <em>patternMatch</em> is a String, then
1259<ol>
1260<li>Return <strong>PACKAGE_RESOLVE</strong>(<em>target</em> with every instance of <em>"*"</em>
1261replaced by <em>patternMatch</em>, <em>packageURL</em> + <em>"/"</em>).</li>
1262</ol>
1263</li>
1264<li>Return <strong>PACKAGE_RESOLVE</strong>(<em>target</em>, <em>packageURL</em> + <em>"/"</em>).</li>
1265</ol>
1266</li>
1267<li>If <em>target</em> split on <em>"/"</em> or <em>"\"</em> contains any <em>""</em>, <em>"."</em>, <em>".."</em>,
1268or <em>"node_modules"</em> segments after the first <em>"."</em> segment, case
1269insensitive and including percent encoded variants, throw an <em>Invalid
1270Package Target</em> error.</li>
1271<li>Let <em>resolvedTarget</em> be the URL resolution of the concatenation of
1272<em>packageURL</em> and <em>target</em>.</li>
1273<li>Assert: <em>resolvedTarget</em> is contained in <em>packageURL</em>.</li>
1274<li>If <em>patternMatch</em> is <strong>null</strong>, then
1275<ol>
1276<li>Return <em>resolvedTarget</em>.</li>
1277</ol>
1278</li>
1279<li>If <em>patternMatch</em> split on <em>"/"</em> or <em>"\"</em> contains any <em>""</em>, <em>"."</em>,
1280<em>".."</em>, or <em>"node_modules"</em> segments, case insensitive and including
1281percent encoded variants, throw an <em>Invalid Module Specifier</em> error.</li>
1282<li>Return the URL resolution of <em>resolvedTarget</em> with every instance of
1283<em>"*"</em> replaced with <em>patternMatch</em>.</li>
1284</ol>
1285</li>
1286<li>Otherwise, if <em>target</em> is a non-null Object, then
1287<ol>
1288<li>If <em>exports</em> contains any index property keys, as defined in ECMA-262
1289<a href="https://tc39.es/ecma262/#integer-index">6.1.7 Array Index</a>, throw an <em>Invalid Package Configuration</em> error.</li>
1290<li>For each property <em>p</em> of <em>target</em>, in object insertion order as,
1291<ol>
1292<li>If <em>p</em> equals <em>"default"</em> or <em>conditions</em> contains an entry for <em>p</em>,
1293then
1294<ol>
1295<li>Let <em>targetValue</em> be the value of the <em>p</em> property in <em>target</em>.</li>
1296<li>Let <em>resolved</em> be the result of <strong>PACKAGE_TARGET_RESOLVE</strong>(
1297<em>packageURL</em>, <em>targetValue</em>, <em>patternMatch</em>, <em>isImports</em>,
1298<em>conditions</em>).</li>
1299<li>If <em>resolved</em> is equal to <strong>undefined</strong>, continue the loop.</li>
1300<li>Return <em>resolved</em>.</li>
1301</ol>
1302</li>
1303</ol>
1304</li>
1305<li>Return <strong>undefined</strong>.</li>
1306</ol>
1307</li>
1308<li>Otherwise, if <em>target</em> is an Array, then
1309<ol>
1310<li>If _target.length is zero, return <strong>null</strong>.</li>
1311<li>For each item <em>targetValue</em> in <em>target</em>, do
1312<ol>
1313<li>Let <em>resolved</em> be the result of <strong>PACKAGE_TARGET_RESOLVE</strong>(
1314<em>packageURL</em>, <em>targetValue</em>, <em>patternMatch</em>, <em>isImports</em>,
1315<em>conditions</em>), continuing the loop on any <em>Invalid Package Target</em>
1316error.</li>
1317<li>If <em>resolved</em> is <strong>undefined</strong>, continue the loop.</li>
1318<li>Return <em>resolved</em>.</li>
1319</ol>
1320</li>
1321<li>Return or throw the last fallback resolution <strong>null</strong> return or error.</li>
1322</ol>
1323</li>
1324<li>Otherwise, if <em>target</em> is <em>null</em>, return <strong>null</strong>.</li>
1325<li>Otherwise throw an <em>Invalid Package Target</em> error.</li>
1326</ol>
1327</blockquote>
1328<p><strong>ESM_FILE_FORMAT</strong>(<em>url</em>)</p>
1329<blockquote>
1330<ol>
1331<li>Assert: <em>url</em> corresponds to an existing file.</li>
1332<li>If <em>url</em> ends in <em>".mjs"</em>, then
1333<ol>
1334<li>Return <em>"module"</em>.</li>
1335</ol>
1336</li>
1337<li>If <em>url</em> ends in <em>".cjs"</em>, then
1338<ol>
1339<li>Return <em>"commonjs"</em>.</li>
1340</ol>
1341</li>
1342<li>If <em>url</em> ends in <em>".json"</em>, then
1343<ol>
1344<li>Return <em>"json"</em>.</li>
1345</ol>
1346</li>
1347<li>Let <em>packageURL</em> be the result of <strong>LOOKUP_PACKAGE_SCOPE</strong>(<em>url</em>).</li>
1348<li>Let <em>pjson</em> be the result of <strong>READ_PACKAGE_JSON</strong>(<em>packageURL</em>).</li>
1349<li>If <em>pjson?.type</em> exists and is <em>"module"</em>, then
1350<ol>
1351<li>If <em>url</em> ends in <em>".js"</em> or has no file extension, then
1352<ol>
1353<li>If <code>--experimental-wasm-modules</code> is enabled and the file at <em>url</em>
1354contains the header for a WebAssembly module, then
1355<ol>
1356<li>Return <em>"wasm"</em>.</li>
1357</ol>
1358</li>
1359<li>Otherwise,
1360<ol>
1361<li>Return <em>"module"</em>.</li>
1362</ol>
1363</li>
1364</ol>
1365</li>
1366<li>Return <strong>undefined</strong>.</li>
1367</ol>
1368</li>
1369<li>Otherwise,
1370<ol>
1371<li>Return <strong>undefined</strong>.</li>
1372</ol>
1373</li>
1374</ol>
1375</blockquote>
1376<p><strong>LOOKUP_PACKAGE_SCOPE</strong>(<em>url</em>)</p>
1377<blockquote>
1378<ol>
1379<li>Let <em>scopeURL</em> be <em>url</em>.</li>
1380<li>While <em>scopeURL</em> is not the file system root,
1381<ol>
1382<li>Set <em>scopeURL</em> to the parent URL of <em>scopeURL</em>.</li>
1383<li>If <em>scopeURL</em> ends in a <em>"node_modules"</em> path segment, return <strong>null</strong>.</li>
1384<li>Let <em>pjsonURL</em> be the resolution of <em>"package.json"</em> within
1385<em>scopeURL</em>.</li>
1386<li>if the file at <em>pjsonURL</em> exists, then
1387<ol>
1388<li>Return <em>scopeURL</em>.</li>
1389</ol>
1390</li>
1391</ol>
1392</li>
1393<li>Return <strong>null</strong>.</li>
1394</ol>
1395</blockquote>
1396<p><strong>READ_PACKAGE_JSON</strong>(<em>packageURL</em>)</p>
1397<blockquote>
1398<ol>
1399<li>Let <em>pjsonURL</em> be the resolution of <em>"package.json"</em> within <em>packageURL</em>.</li>
1400<li>If the file at <em>pjsonURL</em> does not exist, then
1401<ol>
1402<li>Return <strong>null</strong>.</li>
1403</ol>
1404</li>
1405<li>If the file at <em>packageURL</em> does not parse as valid JSON, then
1406<ol>
1407<li>Throw an <em>Invalid Package Configuration</em> error.</li>
1408</ol>
1409</li>
1410<li>Return the parsed JSON source of the file at <em>pjsonURL</em>.</li>
1411</ol>
1412</blockquote>
1413<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>
1414<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
1415<blockquote>
1416<p>Do not rely on this flag. We plan to remove it once the
1417<a href="module.html#customization-hooks">Module customization hooks</a> have advanced to the point that equivalent
1418functionality can be achieved via custom hooks.</p>
1419</blockquote>
1420<p>The current specifier resolution does not support all default behavior of
1421the CommonJS loader. One of the behavior differences is automatic resolution
1422of file extensions and the ability to import directories that have an index
1423file.</p>
1424<p>The <code>--experimental-specifier-resolution=[mode]</code> flag can be used to customize
1425the extension resolution algorithm. The default mode is <code>explicit</code>, which
1426requires the full path to a module be provided to the loader. To enable the
1427automatic extension resolution and importing from directories that include an
1428index file use the <code>node</code> mode.</p>
1429<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node index.mjs</span>
1430success!
1431<span class="hljs-meta prompt_">$ </span><span class="language-bash">node index <span class="hljs-comment"># Failure!</span></span>
1432Error: Cannot find module
1433<span class="hljs-meta prompt_">$ </span><span class="language-bash">node --experimental-specifier-resolution=node index</span>
1434success!</code> <button class="copy-button">copy</button></pre>
1435<!-- Note: The cjs-module-lexer link should be kept in-sync with the deps version --></section>
1436        <!-- API END -->
1437      </div>
1438    </div>
1439  </div>
1440</body>
1441</html>
1442