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: Packages | 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/packages.html">
12  <script async defer src="assets/api.js" type="text/javascript"></script>
13  
14</head>
15<body class="alt apidoc" id="api-section-packages">
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">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 active">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="packages" 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-packages">Modules: Packages</a>
124<ul>
125<li><a href="#introduction">Introduction</a></li>
126<li><a href="#determining-module-system">Determining module system</a>
127<ul>
128<li><a href="#introduction_1">Introduction</a></li>
129<li><a href="#modules-loaders">Modules loaders</a></li>
130<li><a href="#packagejson-and-file-extensions"><code>package.json</code> and file extensions</a></li>
131<li><a href="#--input-type-flag"><code>--input-type</code> flag</a></li>
132</ul>
133</li>
134<li><span class="stability_1"><a href="#determining-package-manager">Determining package manager</a></span></li>
135<li><a href="#package-entry-points">Package entry points</a>
136<ul>
137<li><a href="#main-entry-point-export">Main entry point export</a></li>
138<li><a href="#subpath-exports">Subpath exports</a>
139<ul>
140<li><a href="#extensions-in-subpaths">Extensions in subpaths</a></li>
141</ul>
142</li>
143<li><a href="#exports-sugar">Exports sugar</a></li>
144<li><a href="#subpath-imports">Subpath imports</a></li>
145<li><a href="#subpath-patterns">Subpath patterns</a></li>
146<li><a href="#conditional-exports">Conditional exports</a></li>
147<li><a href="#nested-conditions">Nested conditions</a></li>
148<li><a href="#resolving-user-conditions">Resolving user conditions</a></li>
149<li><a href="#community-conditions-definitions">Community Conditions Definitions</a></li>
150<li><a href="#self-referencing-a-package-using-its-name">Self-referencing a package using its name</a></li>
151</ul>
152</li>
153<li><a href="#dual-commonjses-module-packages">Dual CommonJS/ES module packages</a>
154<ul>
155<li><a href="#dual-package-hazard">Dual package hazard</a></li>
156<li><a href="#writing-dual-packages-while-avoiding-or-minimizing-hazards">Writing dual packages while avoiding or minimizing hazards</a>
157<ul>
158<li><a href="#approach-1-use-an-es-module-wrapper">Approach #1: Use an ES module wrapper</a></li>
159<li><a href="#approach-2-isolate-state">Approach #2: Isolate state</a></li>
160</ul>
161</li>
162</ul>
163</li>
164<li><a href="#nodejs-packagejson-field-definitions">Node.js <code>package.json</code> field definitions</a>
165<ul>
166<li><a href="#name"><code>"name"</code></a></li>
167<li><a href="#main"><code>"main"</code></a></li>
168<li><span class="stability_1"><a href="#packagemanager"><code>"packageManager"</code></a></span></li>
169<li><a href="#type"><code>"type"</code></a></li>
170<li><a href="#exports"><code>"exports"</code></a></li>
171<li><a href="#imports"><code>"imports"</code></a></li>
172</ul>
173</li>
174</ul>
175</li>
176</ul></div></div>
177    </li>
178  
179            
180    <li class="picker-header">
181      <a href="#">
182        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
183        Index
184      </a>
185
186      <div class="picker"><ul>
187<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
188<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
189
190      <li>
191        <a href="index.html">Index</a>
192      </li>
193    </ul>
194  
195<hr class="line">
196<ul>
197<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
198<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
199<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
200<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
201<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
202<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
203<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
204<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
205<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
206<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
207<li><a href="console.html" class="nav-console">Console</a></li>
208<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
209<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
210<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
211<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
212<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
213<li><a href="dns.html" class="nav-dns">DNS</a></li>
214<li><a href="domain.html" class="nav-domain">Domain</a></li>
215<li><a href="errors.html" class="nav-errors">Errors</a></li>
216<li><a href="events.html" class="nav-events">Events</a></li>
217<li><a href="fs.html" class="nav-fs">File system</a></li>
218<li><a href="globals.html" class="nav-globals">Globals</a></li>
219<li><a href="http.html" class="nav-http">HTTP</a></li>
220<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
221<li><a href="https.html" class="nav-https">HTTPS</a></li>
222<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
223<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
224<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
225<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
226<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
227<li><a href="packages.html" class="nav-packages active">Modules: Packages</a></li>
228<li><a href="net.html" class="nav-net">Net</a></li>
229<li><a href="os.html" class="nav-os">OS</a></li>
230<li><a href="path.html" class="nav-path">Path</a></li>
231<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
232<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
233<li><a href="process.html" class="nav-process">Process</a></li>
234<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
235<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
236<li><a href="readline.html" class="nav-readline">Readline</a></li>
237<li><a href="repl.html" class="nav-repl">REPL</a></li>
238<li><a href="report.html" class="nav-report">Report</a></li>
239<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
240<li><a href="stream.html" class="nav-stream">Stream</a></li>
241<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
242<li><a href="test.html" class="nav-test">Test runner</a></li>
243<li><a href="timers.html" class="nav-timers">Timers</a></li>
244<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
245<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
246<li><a href="tty.html" class="nav-tty">TTY</a></li>
247<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
248<li><a href="url.html" class="nav-url">URL</a></li>
249<li><a href="util.html" class="nav-util">Utilities</a></li>
250<li><a href="v8.html" class="nav-v8">V8</a></li>
251<li><a href="vm.html" class="nav-vm">VM</a></li>
252<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
253<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
254<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
255<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
256<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
257</ul>
258<hr class="line">
259<ul>
260<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
261</ul></div>
262    </li>
263  
264            
265    <li class="picker-header">
266      <a href="#">
267        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
268        Other versions
269      </a>
270      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/packages.html">21.x</a></li>
271<li><a href="https://nodejs.org/docs/latest-v20.x/api/packages.html">20.x <b>LTS</b></a></li>
272<li><a href="https://nodejs.org/docs/latest-v19.x/api/packages.html">19.x</a></li>
273<li><a href="https://nodejs.org/docs/latest-v18.x/api/packages.html">18.x <b>LTS</b></a></li>
274<li><a href="https://nodejs.org/docs/latest-v17.x/api/packages.html">17.x</a></li>
275<li><a href="https://nodejs.org/docs/latest-v16.x/api/packages.html">16.x</a></li>
276<li><a href="https://nodejs.org/docs/latest-v15.x/api/packages.html">15.x</a></li>
277<li><a href="https://nodejs.org/docs/latest-v14.x/api/packages.html">14.x</a></li>
278<li><a href="https://nodejs.org/docs/latest-v13.x/api/packages.html">13.x</a></li>
279<li><a href="https://nodejs.org/docs/latest-v12.x/api/packages.html">12.x</a></li></ol></div>
280    </li>
281  
282            <li class="picker-header">
283              <a href="#">
284                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
285                Options
286              </a>
287        
288              <div class="picker">
289                <ul>
290                  <li>
291                    <a href="all.html">View on single page</a>
292                  </li>
293                  <li>
294                    <a href="packages.json">View as JSON</a>
295                  </li>
296                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/packages.md">Edit on GitHub</a></li>    
297                </ul>
298              </div>
299            </li>
300          </ul>
301        </div>
302        <hr>
303      </header>
304
305      <details id="toc" open><summary>Table of contents</summary><ul>
306<li><a href="#modules-packages">Modules: Packages</a>
307<ul>
308<li><a href="#introduction">Introduction</a></li>
309<li><a href="#determining-module-system">Determining module system</a>
310<ul>
311<li><a href="#introduction_1">Introduction</a></li>
312<li><a href="#modules-loaders">Modules loaders</a></li>
313<li><a href="#packagejson-and-file-extensions"><code>package.json</code> and file extensions</a></li>
314<li><a href="#--input-type-flag"><code>--input-type</code> flag</a></li>
315</ul>
316</li>
317<li><span class="stability_1"><a href="#determining-package-manager">Determining package manager</a></span></li>
318<li><a href="#package-entry-points">Package entry points</a>
319<ul>
320<li><a href="#main-entry-point-export">Main entry point export</a></li>
321<li><a href="#subpath-exports">Subpath exports</a>
322<ul>
323<li><a href="#extensions-in-subpaths">Extensions in subpaths</a></li>
324</ul>
325</li>
326<li><a href="#exports-sugar">Exports sugar</a></li>
327<li><a href="#subpath-imports">Subpath imports</a></li>
328<li><a href="#subpath-patterns">Subpath patterns</a></li>
329<li><a href="#conditional-exports">Conditional exports</a></li>
330<li><a href="#nested-conditions">Nested conditions</a></li>
331<li><a href="#resolving-user-conditions">Resolving user conditions</a></li>
332<li><a href="#community-conditions-definitions">Community Conditions Definitions</a></li>
333<li><a href="#self-referencing-a-package-using-its-name">Self-referencing a package using its name</a></li>
334</ul>
335</li>
336<li><a href="#dual-commonjses-module-packages">Dual CommonJS/ES module packages</a>
337<ul>
338<li><a href="#dual-package-hazard">Dual package hazard</a></li>
339<li><a href="#writing-dual-packages-while-avoiding-or-minimizing-hazards">Writing dual packages while avoiding or minimizing hazards</a>
340<ul>
341<li><a href="#approach-1-use-an-es-module-wrapper">Approach #1: Use an ES module wrapper</a></li>
342<li><a href="#approach-2-isolate-state">Approach #2: Isolate state</a></li>
343</ul>
344</li>
345</ul>
346</li>
347<li><a href="#nodejs-packagejson-field-definitions">Node.js <code>package.json</code> field definitions</a>
348<ul>
349<li><a href="#name"><code>"name"</code></a></li>
350<li><a href="#main"><code>"main"</code></a></li>
351<li><span class="stability_1"><a href="#packagemanager"><code>"packageManager"</code></a></span></li>
352<li><a href="#type"><code>"type"</code></a></li>
353<li><a href="#exports"><code>"exports"</code></a></li>
354<li><a href="#imports"><code>"imports"</code></a></li>
355</ul>
356</li>
357</ul>
358</li>
359</ul></details>
360
361      <div id="apicontent">
362        <h2>Modules: Packages<span><a class="mark" href="#modules-packages" id="modules-packages">#</a></span><a aria-hidden="true" class="legacy" id="packages_modules_packages"></a></h2>
363
364
365<div class="api_metadata">
366<details class="changelog"><summary>History</summary>
367<table>
368<tbody><tr><th>Version</th><th>Changes</th></tr>
369<tr><td>v14.13.0, v12.20.0</td>
370<td><p>Add support for <code>"exports"</code> patterns.</p></td></tr>
371<tr><td>v14.6.0, v12.19.0</td>
372<td><p>Add package <code>"imports"</code> field.</p></td></tr>
373<tr><td>v13.7.0, v12.17.0</td>
374<td><p>Unflag conditional exports.</p></td></tr>
375<tr><td>v13.7.0, v12.16.0</td>
376<td><p>Remove the <code>--experimental-conditional-exports</code> option. In 12.16.0, conditional exports are still behind <code>--experimental-modules</code>.</p></td></tr>
377<tr><td>v13.6.0, v12.16.0</td>
378<td><p>Unflag self-referencing a package using its name.</p></td></tr>
379<tr><td>v12.7.0</td>
380<td><p>Introduce <code>"exports"</code> <code>package.json</code> field as a more powerful alternative to the classic <code>"main"</code> field.</p></td></tr>
381<tr><td>v12.0.0</td>
382<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>
383</tbody></table>
384</details>
385</div>
386<section><h3>Introduction<span><a class="mark" href="#introduction" id="introduction">#</a></span><a aria-hidden="true" class="legacy" id="packages_introduction"></a></h3>
387<p>A package is a folder tree described by a <code>package.json</code> file. The package
388consists of the folder containing the <code>package.json</code> file and all subfolders
389until the next folder containing another <code>package.json</code> file, or a folder
390named <code>node_modules</code>.</p>
391<p>This page provides guidance for package authors writing <code>package.json</code> files
392along with a reference for the <a href="#nodejs-packagejson-field-definitions"><code>package.json</code></a> fields defined by Node.js.</p>
393</section><section><h3>Determining module system<span><a class="mark" href="#determining-module-system" id="determining-module-system">#</a></span><a aria-hidden="true" class="legacy" id="packages_determining_module_system"></a></h3>
394<h4>Introduction<span><a class="mark" href="#introduction_1" id="introduction_1">#</a></span><a aria-hidden="true" class="legacy" id="packages_introduction_1"></a></h4>
395<p>Node.js will treat the following as <a href="esm.html">ES modules</a> when passed to <code>node</code> as the
396initial input, or when referenced by <code>import</code> statements or <code>import()</code>
397expressions:</p>
398<ul>
399<li>
400<p>Files with an <code>.mjs</code> extension.</p>
401</li>
402<li>
403<p>Files with a <code>.js</code> extension when the nearest parent <code>package.json</code> file
404contains a top-level <a href="#type"><code>"type"</code></a> field with a value of <code>"module"</code>.</p>
405</li>
406<li>
407<p>Strings passed in as an argument to <code>--eval</code>, or piped to <code>node</code> via <code>STDIN</code>,
408with the flag <code>--input-type=module</code>.</p>
409</li>
410</ul>
411<p>Node.js will treat the following as <a href="modules.html">CommonJS</a> when passed to <code>node</code> as the
412initial input, or when referenced by <code>import</code> statements or <code>import()</code>
413expressions:</p>
414<ul>
415<li>
416<p>Files with a <code>.cjs</code> extension.</p>
417</li>
418<li>
419<p>Files with a <code>.js</code> extension when the nearest parent <code>package.json</code> file
420contains a top-level field <a href="#type"><code>"type"</code></a> with a value of <code>"commonjs"</code>.</p>
421</li>
422<li>
423<p>Strings passed in as an argument to <code>--eval</code> or <code>--print</code>, or piped to <code>node</code>
424via <code>STDIN</code>, with the flag <code>--input-type=commonjs</code>.</p>
425</li>
426</ul>
427<p>Aside from these explicit cases, there are other cases where Node.js defaults to
428one module system or the other based on the value of the
429<a href="cli.html#--experimental-default-typetype"><code>--experimental-default-type</code></a> flag:</p>
430<ul>
431<li>
432<p>Files ending in <code>.js</code> or with no extension, if there is no <code>package.json</code> file
433present in the same folder or any parent folder.</p>
434</li>
435<li>
436<p>Files ending in <code>.js</code> or with no extension, if the nearest parent
437<code>package.json</code> field lacks a <code>"type"</code> field; unless the folder is inside a
438<code>node_modules</code> folder. (Package scopes under <code>node_modules</code> are always treated
439as CommonJS when the <code>package.json</code> file lacks a <code>"type"</code> field, regardless
440of <code>--experimental-default-type</code>, for backward compatibility.)</p>
441</li>
442<li>
443<p>Strings passed in as an argument to <code>--eval</code> or piped to <code>node</code> via <code>STDIN</code>,
444when <code>--input-type</code> is unspecified.</p>
445</li>
446</ul>
447<p>This flag currently defaults to <code>"commonjs"</code>, but it may change in the future to
448default to <code>"module"</code>. For this reason it is best to be explicit wherever
449possible; in particular, package authors should always include the <a href="#type"><code>"type"</code></a>
450field in their <code>package.json</code> files, even in packages where all sources are
451CommonJS. Being explicit about the <code>type</code> of the package will future-proof the
452package in case the default type of Node.js ever changes, and it will also make
453things easier for build tools and loaders to determine how the files in the
454package should be interpreted.</p>
455<h4>Modules loaders<span><a class="mark" href="#modules-loaders" id="modules-loaders">#</a></span><a aria-hidden="true" class="legacy" id="packages_modules_loaders"></a></h4>
456<p>Node.js has two systems for resolving a specifier and loading modules.</p>
457<p>There is the CommonJS module loader:</p>
458<ul>
459<li>It is fully synchronous.</li>
460<li>It is responsible for handling <code>require()</code> calls.</li>
461<li>It is monkey patchable.</li>
462<li>It supports <a href="modules.html#folders-as-modules">folders as modules</a>.</li>
463<li>When resolving a specifier, if no exact match is found, it will try to add
464extensions (<code>.js</code>, <code>.json</code>, and finally <code>.node</code>) and then attempt to resolve
465<a href="modules.html#folders-as-modules">folders as modules</a>.</li>
466<li>It treats <code>.json</code> as JSON text files.</li>
467<li><code>.node</code> files are interpreted as compiled addon modules loaded with
468<code>process.dlopen()</code>.</li>
469<li>It treats all files that lack <code>.json</code> or <code>.node</code> extensions as JavaScript
470text files.</li>
471<li>It cannot be used to load ECMAScript modules (although it is possible to
472<a href="modules.html#the-mjs-extension">load ECMASCript modules from CommonJS modules</a>). When used to load a
473JavaScript text file that is not an ECMAScript module, it loads it as a
474CommonJS module.</li>
475</ul>
476<p>There is the ECMAScript module loader:</p>
477<ul>
478<li>It is asynchronous.</li>
479<li>It is responsible for handling <code>import</code> statements and <code>import()</code> expressions.</li>
480<li>It is not monkey patchable, can be customized using <a href="esm.html#loaders">loader hooks</a>.</li>
481<li>It does not support folders as modules, directory indexes (e.g.
482<code>'./startup/index.js'</code>) must be fully specified.</li>
483<li>It does no extension searching. A file extension must be provided
484when the specifier is a relative or absolute file URL.</li>
485<li>It can load JSON modules, but an import assertion is required.</li>
486<li>It accepts only <code>.js</code>, <code>.mjs</code>, and <code>.cjs</code> extensions for JavaScript text
487files.</li>
488<li>It can be used to load JavaScript CommonJS modules. Such modules
489are passed through the <code>cjs-module-lexer</code> to try to identify named exports,
490which are available if they can be determined through static analysis.
491Imported CommonJS modules have their URLs converted to absolute
492paths and are then loaded via the CommonJS module loader.</li>
493</ul>
494<h4><code>package.json</code> and file extensions<span><a class="mark" href="#packagejson-and-file-extensions" id="packagejson-and-file-extensions">#</a></span><a aria-hidden="true" class="legacy" id="packages_package_json_and_file_extensions"></a></h4>
495<p>Within a package, the <a href="#nodejs-packagejson-field-definitions"><code>package.json</code></a> <a href="#type"><code>"type"</code></a> field defines how
496Node.js should interpret <code>.js</code> files. If a <code>package.json</code> file does not have a
497<code>"type"</code> field, <code>.js</code> files are treated as <a href="modules.html">CommonJS</a>.</p>
498<p>A <code>package.json</code> <code>"type"</code> value of <code>"module"</code> tells Node.js to interpret <code>.js</code>
499files within that package as using <a href="esm.html">ES module</a> syntax.</p>
500<p>The <code>"type"</code> field applies not only to initial entry points (<code>node my-app.js</code>)
501but also to files referenced by <code>import</code> statements and <code>import()</code> expressions.</p>
502<pre><code class="language-js"><span class="hljs-comment">// my-app.js, treated as an ES module because there is a package.json</span>
503<span class="hljs-comment">// file in the same folder with "type": "module".</span>
504
505<span class="hljs-keyword">import</span> <span class="hljs-string">'./startup/init.js'</span>;
506<span class="hljs-comment">// Loaded as ES module since ./startup contains no package.json file,</span>
507<span class="hljs-comment">// and therefore inherits the "type" value from one level up.</span>
508
509<span class="hljs-keyword">import</span> <span class="hljs-string">'commonjs-package'</span>;
510<span class="hljs-comment">// Loaded as CommonJS since ./node_modules/commonjs-package/package.json</span>
511<span class="hljs-comment">// lacks a "type" field or contains "type": "commonjs".</span>
512
513<span class="hljs-keyword">import</span> <span class="hljs-string">'./node_modules/commonjs-package/index.js'</span>;
514<span class="hljs-comment">// Loaded as CommonJS since ./node_modules/commonjs-package/package.json</span>
515<span class="hljs-comment">// lacks a "type" field or contains "type": "commonjs".</span></code> <button class="copy-button">copy</button></pre>
516<p>Files ending with <code>.mjs</code> are always loaded as <a href="esm.html">ES modules</a> regardless of
517the nearest parent <code>package.json</code>.</p>
518<p>Files ending with <code>.cjs</code> are always loaded as <a href="modules.html">CommonJS</a> regardless of the
519nearest parent <code>package.json</code>.</p>
520<pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'./legacy-file.cjs'</span>;
521<span class="hljs-comment">// Loaded as CommonJS since .cjs is always loaded as CommonJS.</span>
522
523<span class="hljs-keyword">import</span> <span class="hljs-string">'commonjs-package/src/index.mjs'</span>;
524<span class="hljs-comment">// Loaded as ES module since .mjs is always loaded as ES module.</span></code> <button class="copy-button">copy</button></pre>
525<p>The <code>.mjs</code> and <code>.cjs</code> extensions can be used to mix types within the same
526package:</p>
527<ul>
528<li>
529<p>Within a <code>"type": "module"</code> package, Node.js can be instructed to
530interpret a particular file as <a href="modules.html">CommonJS</a> by naming it with a <code>.cjs</code>
531extension (since both <code>.js</code> and <code>.mjs</code> files are treated as ES modules within
532a <code>"module"</code> package).</p>
533</li>
534<li>
535<p>Within a <code>"type": "commonjs"</code> package, Node.js can be instructed to
536interpret a particular file as an <a href="esm.html">ES module</a> by naming it with an <code>.mjs</code>
537extension (since both <code>.js</code> and <code>.cjs</code> files are treated as CommonJS within a
538<code>"commonjs"</code> package).</p>
539</li>
540</ul>
541<h4><code>--input-type</code> flag<span><a class="mark" href="#--input-type-flag" id="--input-type-flag">#</a></span><a aria-hidden="true" class="legacy" id="packages_input_type_flag"></a></h4>
542<div class="api_metadata">
543<span>Added in: v12.0.0</span>
544</div>
545<p>Strings passed in as an argument to <code>--eval</code> (or <code>-e</code>), or piped to <code>node</code> via
546<code>STDIN</code>, are treated as <a href="esm.html">ES modules</a> when the <code>--input-type=module</code> flag
547is set.</p>
548<pre><code class="language-bash">node --input-type=module --<span class="hljs-built_in">eval</span> <span class="hljs-string">"import { sep } from 'node:path'; console.log(sep);"</span>
549
550<span class="hljs-built_in">echo</span> <span class="hljs-string">"import { sep } from 'node:path'; console.log(sep);"</span> | node --input-type=module</code> <button class="copy-button">copy</button></pre>
551<p>For completeness there is also <code>--input-type=commonjs</code>, for explicitly running
552string input as CommonJS. This is the default behavior if <code>--input-type</code> is
553unspecified.</p>
554</section><section><h3>Determining package manager<span><a class="mark" href="#determining-package-manager" id="determining-package-manager">#</a></span><a aria-hidden="true" class="legacy" id="packages_determining_package_manager"></a></h3>
555<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
556<p>While all Node.js projects are expected to be installable by all package
557managers once published, their development teams are often required to use one
558specific package manager. To make this process easier, Node.js ships with a
559tool called <a href="corepack.html">Corepack</a> that aims to make all package managers transparently
560available in your environment - provided you have Node.js installed.</p>
561<p>By default Corepack won't enforce any specific package manager and will use
562the generic "Last Known Good" versions associated with each Node.js release,
563but you can improve this experience by setting the <a href="#packagemanager"><code>"packageManager"</code></a> field
564in your project's <code>package.json</code>.</p>
565</section><section><h3>Package entry points<span><a class="mark" href="#package-entry-points" id="package-entry-points">#</a></span><a aria-hidden="true" class="legacy" id="packages_package_entry_points"></a></h3>
566<p>In a package's <code>package.json</code> file, two fields can define entry points for a
567package: <a href="#main"><code>"main"</code></a> and <a href="#exports"><code>"exports"</code></a>. Both fields apply to both ES module
568and CommonJS module entry points.</p>
569<p>The <a href="#main"><code>"main"</code></a> field is supported in all versions of Node.js, but its
570capabilities are limited: it only defines the main entry point of the package.</p>
571<p>The <a href="#exports"><code>"exports"</code></a> provides a modern alternative to <a href="#main"><code>"main"</code></a> allowing
572multiple entry points to be defined, conditional entry resolution support
573between environments, and <strong>preventing any other entry points besides those
574defined in <a href="#exports"><code>"exports"</code></a></strong>. This encapsulation allows module authors to
575clearly define the public interface for their package.</p>
576<p>For new packages targeting the currently supported versions of Node.js, the
577<a href="#exports"><code>"exports"</code></a> field is recommended. For packages supporting Node.js 10 and
578below, the <a href="#main"><code>"main"</code></a> field is required. If both <a href="#exports"><code>"exports"</code></a> and
579<a href="#main"><code>"main"</code></a> are defined, the <a href="#exports"><code>"exports"</code></a> field takes precedence over
580<a href="#main"><code>"main"</code></a> in supported versions of Node.js.</p>
581<p><a href="#conditional-exports">Conditional exports</a> can be used within <a href="#exports"><code>"exports"</code></a> to define different
582package entry points per environment, including whether the package is
583referenced via <code>require</code> or via <code>import</code>. For more information about supporting
584both CommonJS and ES modules in a single package please consult
585<a href="#dual-commonjses-module-packages">the dual CommonJS/ES module packages section</a>.</p>
586<p>Existing packages introducing the <a href="#exports"><code>"exports"</code></a> field will prevent consumers
587of the package from using any entry points that are not defined, including the
588<a href="#nodejs-packagejson-field-definitions"><code>package.json</code></a> (e.g. <code>require('your-package/package.json')</code>. <strong>This will
589likely be a breaking change.</strong></p>
590<p>To make the introduction of <a href="#exports"><code>"exports"</code></a> non-breaking, ensure that every
591previously supported entry point is exported. It is best to explicitly specify
592entry points so that the package's public API is well-defined. For example,
593a project that previously exported <code>main</code>, <code>lib</code>,
594<code>feature</code>, and the <code>package.json</code> could use the following <code>package.exports</code>:</p>
595<pre><code class="language-json"><span class="hljs-punctuation">{</span>
596  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"my-package"</span><span class="hljs-punctuation">,</span>
597  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
598    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/index.js"</span><span class="hljs-punctuation">,</span>
599    <span class="hljs-attr">"./lib"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/index.js"</span><span class="hljs-punctuation">,</span>
600    <span class="hljs-attr">"./lib/index"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/index.js"</span><span class="hljs-punctuation">,</span>
601    <span class="hljs-attr">"./lib/index.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/index.js"</span><span class="hljs-punctuation">,</span>
602    <span class="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature/index.js"</span><span class="hljs-punctuation">,</span>
603    <span class="hljs-attr">"./feature/index"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature/index.js"</span><span class="hljs-punctuation">,</span>
604    <span class="hljs-attr">"./feature/index.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature/index.js"</span><span class="hljs-punctuation">,</span>
605    <span class="hljs-attr">"./package.json"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./package.json"</span>
606  <span class="hljs-punctuation">}</span>
607<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
608<p>Alternatively a project could choose to export entire folders both with and
609without extensioned subpaths using export patterns:</p>
610<pre><code class="language-json"><span class="hljs-punctuation">{</span>
611  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"my-package"</span><span class="hljs-punctuation">,</span>
612  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
613    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/index.js"</span><span class="hljs-punctuation">,</span>
614    <span class="hljs-attr">"./lib"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/index.js"</span><span class="hljs-punctuation">,</span>
615    <span class="hljs-attr">"./lib/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/*.js"</span><span class="hljs-punctuation">,</span>
616    <span class="hljs-attr">"./lib/*.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/*.js"</span><span class="hljs-punctuation">,</span>
617    <span class="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature/index.js"</span><span class="hljs-punctuation">,</span>
618    <span class="hljs-attr">"./feature/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature/*.js"</span><span class="hljs-punctuation">,</span>
619    <span class="hljs-attr">"./feature/*.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature/*.js"</span><span class="hljs-punctuation">,</span>
620    <span class="hljs-attr">"./package.json"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./package.json"</span>
621  <span class="hljs-punctuation">}</span>
622<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
623<p>With the above providing backwards-compatibility for any minor package versions,
624a future major change for the package can then properly restrict the exports
625to only the specific feature exports exposed:</p>
626<pre><code class="language-json"><span class="hljs-punctuation">{</span>
627  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"my-package"</span><span class="hljs-punctuation">,</span>
628  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
629    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/index.js"</span><span class="hljs-punctuation">,</span>
630    <span class="hljs-attr">"./feature/*.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature/*.js"</span><span class="hljs-punctuation">,</span>
631    <span class="hljs-attr">"./feature/internal/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">null</span></span>
632  <span class="hljs-punctuation">}</span>
633<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
634<h4>Main entry point export<span><a class="mark" href="#main-entry-point-export" id="main-entry-point-export">#</a></span><a aria-hidden="true" class="legacy" id="packages_main_entry_point_export"></a></h4>
635<p>When writing a new package, it is recommended to use the <a href="#exports"><code>"exports"</code></a> field:</p>
636<pre><code class="language-json"><span class="hljs-punctuation">{</span>
637  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.js"</span>
638<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
639<p>When the <a href="#exports"><code>"exports"</code></a> field is defined, all subpaths of the package are
640encapsulated and no longer available to importers. For example,
641<code>require('pkg/subpath.js')</code> throws an <a href="errors.html#err_package_path_not_exported"><code>ERR_PACKAGE_PATH_NOT_EXPORTED</code></a>
642error.</p>
643<p>This encapsulation of exports provides more reliable guarantees
644about package interfaces for tools and when handling semver upgrades for a
645package. It is not a strong encapsulation since a direct require of any
646absolute subpath of the package such as
647<code>require('/path/to/node_modules/pkg/subpath.js')</code> will still load <code>subpath.js</code>.</p>
648<p>All currently supported versions of Node.js and modern build tools support the
649<code>"exports"</code> field. For projects using an older version of Node.js or a related
650build tool, compatibility can be achieved by including the <code>"main"</code> field
651alongside <code>"exports"</code> pointing to the same module:</p>
652<pre><code class="language-json"><span class="hljs-punctuation">{</span>
653  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.js"</span><span class="hljs-punctuation">,</span>
654  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.js"</span>
655<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
656<h4>Subpath exports<span><a class="mark" href="#subpath-exports" id="subpath-exports">#</a></span><a aria-hidden="true" class="legacy" id="packages_subpath_exports"></a></h4>
657<div class="api_metadata">
658<span>Added in: v12.7.0</span>
659</div>
660<p>When using the <a href="#exports"><code>"exports"</code></a> field, custom subpaths can be defined along
661with the main entry point by treating the main entry point as the
662<code>"."</code> subpath:</p>
663<pre><code class="language-json"><span class="hljs-punctuation">{</span>
664  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
665    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.js"</span><span class="hljs-punctuation">,</span>
666    <span class="hljs-attr">"./submodule.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./src/submodule.js"</span>
667  <span class="hljs-punctuation">}</span>
668<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
669<p>Now only the defined subpath in <a href="#exports"><code>"exports"</code></a> can be imported by a consumer:</p>
670<pre><code class="language-js"><span class="hljs-keyword">import</span> submodule <span class="hljs-keyword">from</span> <span class="hljs-string">'es-module-package/submodule.js'</span>;
671<span class="hljs-comment">// Loads ./node_modules/es-module-package/src/submodule.js</span></code> <button class="copy-button">copy</button></pre>
672<p>While other subpaths will error:</p>
673<pre><code class="language-js"><span class="hljs-keyword">import</span> submodule <span class="hljs-keyword">from</span> <span class="hljs-string">'es-module-package/private-module.js'</span>;
674<span class="hljs-comment">// Throws ERR_PACKAGE_PATH_NOT_EXPORTED</span></code> <button class="copy-button">copy</button></pre>
675<h5>Extensions in subpaths<span><a class="mark" href="#extensions-in-subpaths" id="extensions-in-subpaths">#</a></span><a aria-hidden="true" class="legacy" id="packages_extensions_in_subpaths"></a></h5>
676<p>Package authors should provide either extensioned (<code>import 'pkg/subpath.js'</code>) or
677extensionless (<code>import 'pkg/subpath'</code>) subpaths in their exports. This ensures
678that there is only one subpath for each exported module so that all dependents
679import the same consistent specifier, keeping the package contract clear for
680consumers and simplifying package subpath completions.</p>
681<p>Traditionally, packages tended to use the extensionless style, which has the
682benefits of readability and of masking the true path of the file within the
683package.</p>
684<p>With <a href="https://github.com/WICG/import-maps">import maps</a> now providing a standard for package resolution in browsers
685and other JavaScript runtimes, using the extensionless style can result in
686bloated import map definitions. Explicit file extensions can avoid this issue by
687enabling the import map to utilize a <a href="https://github.com/WICG/import-maps#packages-via-trailing-slashes">packages folder mapping</a> to map multiple
688subpaths where possible instead of a separate map entry per package subpath
689export. This also mirrors the requirement of using <a href="esm.html#mandatory-file-extensions">the full specifier path</a>
690in relative and absolute import specifiers.</p>
691<h4>Exports sugar<span><a class="mark" href="#exports-sugar" id="exports-sugar">#</a></span><a aria-hidden="true" class="legacy" id="packages_exports_sugar"></a></h4>
692<div class="api_metadata">
693<span>Added in: v12.11.0</span>
694</div>
695<p>If the <code>"."</code> export is the only export, the <a href="#exports"><code>"exports"</code></a> field provides sugar
696for this case being the direct <a href="#exports"><code>"exports"</code></a> field value.</p>
697<pre><code class="language-json"><span class="hljs-punctuation">{</span>
698  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
699    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.js"</span>
700  <span class="hljs-punctuation">}</span>
701<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
702<p>can be written:</p>
703<pre><code class="language-json"><span class="hljs-punctuation">{</span>
704  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.js"</span>
705<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
706<h4>Subpath imports<span><a class="mark" href="#subpath-imports" id="subpath-imports">#</a></span><a aria-hidden="true" class="legacy" id="packages_subpath_imports"></a></h4>
707<div class="api_metadata">
708<span>Added in: v14.6.0, v12.19.0</span>
709</div>
710<p>In addition to the <a href="#exports"><code>"exports"</code></a> field, there is a package <code>"imports"</code> field
711to create private mappings that only apply to import specifiers from within the
712package itself.</p>
713<p>Entries in the <code>"imports"</code> field must always start with <code>#</code> to ensure they are
714disambiguated from external package specifiers.</p>
715<p>For example, the imports field can be used to gain the benefits of conditional
716exports for internal modules:</p>
717<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
718<span class="hljs-punctuation">{</span>
719  <span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
720    <span class="hljs-attr">"#dep"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
721      <span class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"dep-node-native"</span><span class="hljs-punctuation">,</span>
722      <span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./dep-polyfill.js"</span>
723    <span class="hljs-punctuation">}</span>
724  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
725  <span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
726    <span class="hljs-attr">"dep-node-native"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"^1.0.0"</span>
727  <span class="hljs-punctuation">}</span>
728<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
729<p>where <code>import '#dep'</code> does not get the resolution of the external package
730<code>dep-node-native</code> (including its exports in turn), and instead gets the local
731file <code>./dep-polyfill.js</code> relative to the package in other environments.</p>
732<p>Unlike the <code>"exports"</code> field, the <code>"imports"</code> field permits mapping to external
733packages.</p>
734<p>The resolution rules for the imports field are otherwise analogous to the
735exports field.</p>
736<h4>Subpath patterns<span><a class="mark" href="#subpath-patterns" id="subpath-patterns">#</a></span><a aria-hidden="true" class="legacy" id="packages_subpath_patterns"></a></h4>
737<div class="api_metadata">
738<details class="changelog"><summary>History</summary>
739<table>
740<tbody><tr><th>Version</th><th>Changes</th></tr>
741<tr><td>v16.10.0, v14.19.0</td>
742<td><p>Support pattern trailers in "imports" field.</p></td></tr>
743<tr><td>v16.9.0, v14.19.0</td>
744<td><p>Support pattern trailers.</p></td></tr>
745<tr><td>v14.13.0, v12.20.0</td>
746<td><p><span>Added in: v14.13.0, v12.20.0</span></p></td></tr>
747</tbody></table>
748</details>
749</div>
750<p>For packages with a small number of exports or imports, we recommend
751explicitly listing each exports subpath entry. But for packages that have
752large numbers of subpaths, this might cause <code>package.json</code> bloat and
753maintenance issues.</p>
754<p>For these use cases, subpath export patterns can be used instead:</p>
755<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/es-module-package/package.json</span>
756<span class="hljs-punctuation">{</span>
757  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
758    <span class="hljs-attr">"./features/*.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./src/features/*.js"</span>
759  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
760  <span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
761    <span class="hljs-attr">"#internal/*.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./src/internal/*.js"</span>
762  <span class="hljs-punctuation">}</span>
763<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
764<p><strong><code>*</code> maps expose nested subpaths as it is a string replacement syntax
765only.</strong></p>
766<p>All instances of <code>*</code> on the right hand side will then be replaced with this
767value, including if it contains any <code>/</code> separators.</p>
768<pre><code class="language-js"><span class="hljs-keyword">import</span> featureX <span class="hljs-keyword">from</span> <span class="hljs-string">'es-module-package/features/x.js'</span>;
769<span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/x.js</span>
770
771<span class="hljs-keyword">import</span> featureY <span class="hljs-keyword">from</span> <span class="hljs-string">'es-module-package/features/y/y.js'</span>;
772<span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/y/y.js</span>
773
774<span class="hljs-keyword">import</span> internalZ <span class="hljs-keyword">from</span> <span class="hljs-string">'#internal/z.js'</span>;
775<span class="hljs-comment">// Loads ./node_modules/es-module-package/src/internal/z.js</span></code> <button class="copy-button">copy</button></pre>
776<p>This is a direct static matching and replacement without any special handling
777for file extensions. Including the <code>"*.js"</code> on both sides of the mapping
778restricts the exposed package exports to only JS files.</p>
779<p>The property of exports being statically enumerable is maintained with exports
780patterns since the individual exports for a package can be determined by
781treating the right hand side target pattern as a <code>**</code> glob against the list of
782files within the package. Because <code>node_modules</code> paths are forbidden in exports
783targets, this expansion is dependent on only the files of the package itself.</p>
784<p>To exclude private subfolders from patterns, <code>null</code> targets can be used:</p>
785<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/es-module-package/package.json</span>
786<span class="hljs-punctuation">{</span>
787  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
788    <span class="hljs-attr">"./features/*.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./src/features/*.js"</span><span class="hljs-punctuation">,</span>
789    <span class="hljs-attr">"./features/private-internal/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">null</span></span>
790  <span class="hljs-punctuation">}</span>
791<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
792<pre><code class="language-js"><span class="hljs-keyword">import</span> featureInternal <span class="hljs-keyword">from</span> <span class="hljs-string">'es-module-package/features/private-internal/m.js'</span>;
793<span class="hljs-comment">// Throws: ERR_PACKAGE_PATH_NOT_EXPORTED</span>
794
795<span class="hljs-keyword">import</span> featureX <span class="hljs-keyword">from</span> <span class="hljs-string">'es-module-package/features/x.js'</span>;
796<span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/x.js</span></code> <button class="copy-button">copy</button></pre>
797<h4>Conditional exports<span><a class="mark" href="#conditional-exports" id="conditional-exports">#</a></span><a aria-hidden="true" class="legacy" id="packages_conditional_exports"></a></h4>
798<div class="api_metadata">
799<details class="changelog"><summary>History</summary>
800<table>
801<tbody><tr><th>Version</th><th>Changes</th></tr>
802<tr><td>v13.7.0, v12.16.0</td>
803<td><p>Unflag conditional exports.</p></td></tr>
804<tr><td>v13.2.0, v12.16.0</td>
805<td><p><span>Added in: v13.2.0, v12.16.0</span></p></td></tr>
806</tbody></table>
807</details>
808</div>
809<p>Conditional exports provide a way to map to different paths depending on
810certain conditions. They are supported for both CommonJS and ES module imports.</p>
811<p>For example, a package that wants to provide different ES module exports for
812<code>require()</code> and <code>import</code> can be written:</p>
813<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
814<span class="hljs-punctuation">{</span>
815  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
816    <span class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index-module.js"</span><span class="hljs-punctuation">,</span>
817    <span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index-require.cjs"</span>
818  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
819  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span>
820<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
821<p>Node.js implements the following conditions, listed in order from most
822specific to least specific as conditions should be defined:</p>
823<ul>
824<li><code>"node-addons"</code> - similar to <code>"node"</code> and matches for any Node.js environment.
825This condition can be used to provide an entry point which uses native C++
826addons as opposed to an entry point which is more universal and doesn't rely
827on native addons. This condition can be disabled via the
828<a href="cli.html#--no-addons"><code>--no-addons</code> flag</a>.</li>
829<li><code>"node"</code> - matches for any Node.js environment. Can be a CommonJS or ES
830module file. <em>In most cases explicitly calling out the Node.js platform is
831not necessary.</em></li>
832<li><code>"import"</code> - matches when the package is loaded via <code>import</code> or
833<code>import()</code>, or via any top-level import or resolve operation by the
834ECMAScript module loader. Applies regardless of the module format of the
835target file. <em>Always mutually exclusive with <code>"require"</code>.</em></li>
836<li><code>"require"</code> - matches when the package is loaded via <code>require()</code>. The
837referenced file should be loadable with <code>require()</code> although the condition
838matches regardless of the module format of the target file. Expected
839formats include CommonJS, JSON, and native addons but not ES modules as
840<code>require()</code> doesn't support them. <em>Always mutually exclusive with
841<code>"import"</code>.</em></li>
842<li><code>"default"</code> - the generic fallback that always matches. Can be a CommonJS
843or ES module file. <em>This condition should always come last.</em></li>
844</ul>
845<p>Within the <a href="#exports"><code>"exports"</code></a> object, key order is significant. During condition
846matching, earlier entries have higher priority and take precedence over later
847entries. <em>The general rule is that conditions should be from most specific to
848least specific in object order</em>.</p>
849<p>Using the <code>"import"</code> and <code>"require"</code> conditions can lead to some hazards,
850which are further explained in <a href="#dual-commonjses-module-packages">the dual CommonJS/ES module packages section</a>.</p>
851<p>The <code>"node-addons"</code> condition can be used to provide an entry point which
852uses native C++ addons. However, this condition can be disabled via the
853<a href="cli.html#--no-addons"><code>--no-addons</code> flag</a>. When using <code>"node-addons"</code>, it's recommended to treat
854<code>"default"</code> as an enhancement that provides a more universal entry point, e.g.
855using WebAssembly instead of a native addon.</p>
856<p>Conditional exports can also be extended to exports subpaths, for example:</p>
857<pre><code class="language-json"><span class="hljs-punctuation">{</span>
858  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
859    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.js"</span><span class="hljs-punctuation">,</span>
860    <span class="hljs-attr">"./feature.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
861      <span class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature-node.js"</span><span class="hljs-punctuation">,</span>
862      <span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature.js"</span>
863    <span class="hljs-punctuation">}</span>
864  <span class="hljs-punctuation">}</span>
865<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
866<p>Defines a package where <code>require('pkg/feature.js')</code> and
867<code>import 'pkg/feature.js'</code> could provide different implementations between
868Node.js and other JS environments.</p>
869<p>When using environment branches, always include a <code>"default"</code> condition where
870possible. Providing a <code>"default"</code> condition ensures that any unknown JS
871environments are able to use this universal implementation, which helps avoid
872these JS environments from having to pretend to be existing environments in
873order to support packages with conditional exports. For this reason, using
874<code>"node"</code> and <code>"default"</code> condition branches is usually preferable to using
875<code>"node"</code> and <code>"browser"</code> condition branches.</p>
876<h4>Nested conditions<span><a class="mark" href="#nested-conditions" id="nested-conditions">#</a></span><a aria-hidden="true" class="legacy" id="packages_nested_conditions"></a></h4>
877<p>In addition to direct mappings, Node.js also supports nested condition objects.</p>
878<p>For example, to define a package that only has dual mode entry points for
879use in Node.js but not the browser:</p>
880<pre><code class="language-json"><span class="hljs-punctuation">{</span>
881  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
882    <span class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
883      <span class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature-node.mjs"</span><span class="hljs-punctuation">,</span>
884      <span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature-node.cjs"</span>
885    <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
886    <span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature.mjs"</span>
887  <span class="hljs-punctuation">}</span>
888<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
889<p>Conditions continue to be matched in order as with flat conditions. If
890a nested condition does not have any mapping it will continue checking
891the remaining conditions of the parent condition. In this way nested
892conditions behave analogously to nested JavaScript <code>if</code> statements.</p>
893<h4>Resolving user conditions<span><a class="mark" href="#resolving-user-conditions" id="resolving-user-conditions">#</a></span><a aria-hidden="true" class="legacy" id="packages_resolving_user_conditions"></a></h4>
894<div class="api_metadata">
895<span>Added in: v14.9.0, v12.19.0</span>
896</div>
897<p>When running Node.js, custom user conditions can be added with the
898<code>--conditions</code> flag:</p>
899<pre><code class="language-bash">node --conditions=development index.js</code> <button class="copy-button">copy</button></pre>
900<p>which would then resolve the <code>"development"</code> condition in package imports and
901exports, while resolving the existing <code>"node"</code>, <code>"node-addons"</code>, <code>"default"</code>,
902<code>"import"</code>, and <code>"require"</code> conditions as appropriate.</p>
903<p>Any number of custom conditions can be set with repeat flags.</p>
904<h4>Community Conditions Definitions<span><a class="mark" href="#community-conditions-definitions" id="community-conditions-definitions">#</a></span><a aria-hidden="true" class="legacy" id="packages_community_conditions_definitions"></a></h4>
905<p>Condition strings other than the <code>"import"</code>, <code>"require"</code>, <code>"node"</code>,
906<code>"node-addons"</code> and <code>"default"</code> conditions
907<a href="#conditional-exports">implemented in Node.js core</a> are ignored by default.</p>
908<p>Other platforms may implement other conditions and user conditions can be
909enabled in Node.js via the <a href="#resolving-user-conditions"><code>--conditions</code> / <code>-C</code> flag</a>.</p>
910<p>Since custom package conditions require clear definitions to ensure correct
911usage, a list of common known package conditions and their strict definitions
912is provided below to assist with ecosystem coordination.</p>
913<ul>
914<li><code>"types"</code> - can be used by typing systems to resolve the typing file for
915the given export. <em>This condition should always be included first.</em></li>
916<li><code>"browser"</code> - any web browser environment.</li>
917<li><code>"development"</code> - can be used to define a development-only environment
918entry point, for example to provide additional debugging context such as
919better error messages when running in a development mode. <em>Must always be
920mutually exclusive with <code>"production"</code>.</em></li>
921<li><code>"production"</code> - can be used to define a production environment entry
922point. <em>Must always be mutually exclusive with <code>"development"</code>.</em></li>
923</ul>
924<p>For other runtimes, platform-specific condition key definitions are maintained
925by the <a href="https://wintercg.org/">WinterCG</a> in the <a href="https://runtime-keys.proposal.wintercg.org/">Runtime Keys</a> proposal specification.</p>
926<p>New conditions definitions may be added to this list by creating a pull request
927to the <a href="https://github.com/nodejs/node/blob/HEAD/doc/api/packages.md#conditions-definitions">Node.js documentation for this section</a>. The requirements for listing
928a new condition definition here are that:</p>
929<ul>
930<li>The definition should be clear and unambiguous for all implementers.</li>
931<li>The use case for why the condition is needed should be clearly justified.</li>
932<li>There should exist sufficient existing implementation usage.</li>
933<li>The condition name should not conflict with another condition definition or
934condition in wide usage.</li>
935<li>The listing of the condition definition should provide a coordination
936benefit to the ecosystem that wouldn't otherwise be possible. For example,
937this would not necessarily be the case for company-specific or
938application-specific conditions.</li>
939<li>The condition should be such that a Node.js user would expect it to be in
940Node.js core documentation. The <code>"types"</code> condition is a good example: It
941doesn't really belong in the <a href="https://runtime-keys.proposal.wintercg.org/">Runtime Keys</a> proposal but is a good fit
942here in the Node.js docs.</li>
943</ul>
944<p>The above definitions may be moved to a dedicated conditions registry in due
945course.</p>
946<h4>Self-referencing a package using its name<span><a class="mark" href="#self-referencing-a-package-using-its-name" id="self-referencing-a-package-using-its-name">#</a></span><a aria-hidden="true" class="legacy" id="packages_self_referencing_a_package_using_its_name"></a></h4>
947<div class="api_metadata">
948<details class="changelog"><summary>History</summary>
949<table>
950<tbody><tr><th>Version</th><th>Changes</th></tr>
951<tr><td>v13.6.0, v12.16.0</td>
952<td><p>Unflag self-referencing a package using its name.</p></td></tr>
953<tr><td>v13.1.0, v12.16.0</td>
954<td><p><span>Added in: v13.1.0, v12.16.0</span></p></td></tr>
955</tbody></table>
956</details>
957</div>
958<p>Within a package, the values defined in the package's
959<code>package.json</code> <a href="#exports"><code>"exports"</code></a> field can be referenced via the package's name.
960For example, assuming the <code>package.json</code> is:</p>
961<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
962<span class="hljs-punctuation">{</span>
963  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"a-package"</span><span class="hljs-punctuation">,</span>
964  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
965    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.mjs"</span><span class="hljs-punctuation">,</span>
966    <span class="hljs-attr">"./foo.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./foo.js"</span>
967  <span class="hljs-punctuation">}</span>
968<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
969<p>Then any module <em>in that package</em> can reference an export in the package itself:</p>
970<pre><code class="language-js"><span class="hljs-comment">// ./a-module.mjs</span>
971<span class="hljs-keyword">import</span> { something } <span class="hljs-keyword">from</span> <span class="hljs-string">'a-package'</span>; <span class="hljs-comment">// Imports "something" from ./index.mjs.</span></code> <button class="copy-button">copy</button></pre>
972<p>Self-referencing is available only if <code>package.json</code> has <a href="#exports"><code>"exports"</code></a>, and
973will allow importing only what that <a href="#exports"><code>"exports"</code></a> (in the <code>package.json</code>)
974allows. So the code below, given the previous package, will generate a runtime
975error:</p>
976<pre><code class="language-js"><span class="hljs-comment">// ./another-module.mjs</span>
977
978<span class="hljs-comment">// Imports "another" from ./m.mjs. Fails because</span>
979<span class="hljs-comment">// the "package.json" "exports" field</span>
980<span class="hljs-comment">// does not provide an export named "./m.mjs".</span>
981<span class="hljs-keyword">import</span> { another } <span class="hljs-keyword">from</span> <span class="hljs-string">'a-package/m.mjs'</span>;</code> <button class="copy-button">copy</button></pre>
982<p>Self-referencing is also available when using <code>require</code>, both in an ES module,
983and in a CommonJS one. For example, this code will also work:</p>
984<pre><code class="language-js cjs"><span class="hljs-comment">// ./a-module.js</span>
985<span class="hljs-keyword">const</span> { something } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'a-package/foo.js'</span>); <span class="hljs-comment">// Loads from ./foo.js.</span></code> <button class="copy-button">copy</button></pre>
986<p>Finally, self-referencing also works with scoped packages. For example, this
987code will also work:</p>
988<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
989<span class="hljs-punctuation">{</span>
990  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"@my/package"</span><span class="hljs-punctuation">,</span>
991  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.js"</span>
992<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
993<pre><code class="language-js cjs"><span class="hljs-comment">// ./index.js</span>
994<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-number">42</span>;</code> <button class="copy-button">copy</button></pre>
995<pre><code class="language-js cjs"><span class="hljs-comment">// ./other.js</span>
996<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-built_in">require</span>(<span class="hljs-string">'@my/package'</span>));</code> <button class="copy-button">copy</button></pre>
997<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node other.js</span>
99842</code> <button class="copy-button">copy</button></pre>
999</section><section><h3>Dual CommonJS/ES module packages<span><a class="mark" href="#dual-commonjses-module-packages" id="dual-commonjses-module-packages">#</a></span><a aria-hidden="true" class="legacy" id="packages_dual_commonjs_es_module_packages"></a></h3>
1000<p>Prior to the introduction of support for ES modules in Node.js, it was a common
1001pattern for package authors to include both CommonJS and ES module JavaScript
1002sources in their package, with <code>package.json</code> <a href="#main"><code>"main"</code></a> specifying the
1003CommonJS entry point and <code>package.json</code> <code>"module"</code> specifying the ES module
1004entry point.
1005This enabled Node.js to run the CommonJS entry point while build tools such as
1006bundlers used the ES module entry point, since Node.js ignored (and still
1007ignores) the top-level <code>"module"</code> field.</p>
1008<p>Node.js can now run ES module entry points, and a package can contain both
1009CommonJS and ES module entry points (either via separate specifiers such as
1010<code>'pkg'</code> and <code>'pkg/es-module'</code>, or both at the same specifier via <a href="#conditional-exports">Conditional
1011exports</a>). Unlike in the scenario where <code>"module"</code> is only used by bundlers,
1012or ES module files are transpiled into CommonJS on the fly before evaluation by
1013Node.js, the files referenced by the ES module entry point are evaluated as ES
1014modules.</p>
1015<h4>Dual package hazard<span><a class="mark" href="#dual-package-hazard" id="dual-package-hazard">#</a></span><a aria-hidden="true" class="legacy" id="packages_dual_package_hazard"></a></h4>
1016<p>When an application is using a package that provides both CommonJS and ES module
1017sources, there is a risk of certain bugs if both versions of the package get
1018loaded. This potential comes from the fact that the <code>pkgInstance</code> created by
1019<code>const pkgInstance = require('pkg')</code> is not the same as the <code>pkgInstance</code>
1020created by <code>import pkgInstance from 'pkg'</code> (or an alternative main path like
1021<code>'pkg/module'</code>). This is the “dual package hazard,” where two versions of the
1022same package can be loaded within the same runtime environment. While it is
1023unlikely that an application or package would intentionally load both versions
1024directly, it is common for an application to load one version while a dependency
1025of the application loads the other version. This hazard can happen because
1026Node.js supports intermixing CommonJS and ES modules, and can lead to unexpected
1027behavior.</p>
1028<p>If the package main export is a constructor, an <code>instanceof</code> comparison of
1029instances created by the two versions returns <code>false</code>, and if the export is an
1030object, properties added to one (like <code>pkgInstance.foo = 3</code>) are not present on
1031the other. This differs from how <code>import</code> and <code>require</code> statements work in
1032all-CommonJS or all-ES module environments, respectively, and therefore is
1033surprising to users. It also differs from the behavior users are familiar with
1034when using transpilation via tools like <a href="https://babeljs.io/">Babel</a> or <a href="https://github.com/standard-things/esm#readme"><code>esm</code></a>.</p>
1035<h4>Writing dual packages while avoiding or minimizing hazards<span><a class="mark" href="#writing-dual-packages-while-avoiding-or-minimizing-hazards" id="writing-dual-packages-while-avoiding-or-minimizing-hazards">#</a></span><a aria-hidden="true" class="legacy" id="packages_writing_dual_packages_while_avoiding_or_minimizing_hazards"></a></h4>
1036<p>First, the hazard described in the previous section occurs when a package
1037contains both CommonJS and ES module sources and both sources are provided for
1038use in Node.js, either via separate main entry points or exported paths. A
1039package might instead be written where any version of Node.js receives only
1040CommonJS sources, and any separate ES module sources the package might contain
1041are intended only for other environments such as browsers. Such a package
1042would be usable by any version of Node.js, since <code>import</code> can refer to CommonJS
1043files; but it would not provide any of the advantages of using ES module syntax.</p>
1044<p>A package might also switch from CommonJS to ES module syntax in a <a href="https://semver.org/">breaking
1045change</a> version bump. This has the disadvantage that the
1046newest version of the package would only be usable in ES module-supporting
1047versions of Node.js.</p>
1048<p>Every pattern has tradeoffs, but there are two broad approaches that satisfy the
1049following conditions:</p>
1050<ol>
1051<li>The package is usable via both <code>require</code> and <code>import</code>.</li>
1052<li>The package is usable in both current Node.js and older versions of Node.js
1053that lack support for ES modules.</li>
1054<li>The package main entry point, e.g. <code>'pkg'</code> can be used by both <code>require</code> to
1055resolve to a CommonJS file and by <code>import</code> to resolve to an ES module file.
1056(And likewise for exported paths, e.g. <code>'pkg/feature'</code>.)</li>
1057<li>The package provides named exports, e.g. <code>import { name } from 'pkg'</code> rather
1058than <code>import pkg from 'pkg'; pkg.name</code>.</li>
1059<li>The package is potentially usable in other ES module environments such as
1060browsers.</li>
1061<li>The hazards described in the previous section are avoided or minimized.</li>
1062</ol>
1063<h5>Approach #1: Use an ES module wrapper<span><a class="mark" href="#approach-1-use-an-es-module-wrapper" id="approach-1-use-an-es-module-wrapper">#</a></span><a aria-hidden="true" class="legacy" id="packages_approach_1_use_an_es_module_wrapper"></a></h5>
1064<p>Write the package in CommonJS or transpile ES module sources into CommonJS, and
1065create an ES module wrapper file that defines the named exports. Using
1066<a href="#conditional-exports">Conditional exports</a>, the ES module wrapper is used for <code>import</code> and the
1067CommonJS entry point for <code>require</code>.</p>
1068<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</span>
1069<span class="hljs-punctuation">{</span>
1070  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span><span class="hljs-punctuation">,</span>
1071  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
1072    <span class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./wrapper.mjs"</span><span class="hljs-punctuation">,</span>
1073    <span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span>
1074  <span class="hljs-punctuation">}</span>
1075<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
1076<p>The preceding example uses explicit extensions <code>.mjs</code> and <code>.cjs</code>.
1077If your files use the <code>.js</code> extension, <code>"type": "module"</code> will cause such files
1078to be treated as ES modules, just as <code>"type": "commonjs"</code> would cause them
1079to be treated as CommonJS.
1080See <a href="esm.html#enabling">Enabling</a>.</p>
1081<pre><code class="language-js cjs"><span class="hljs-comment">// ./node_modules/pkg/index.cjs</span>
1082<span class="hljs-built_in">exports</span>.<span class="hljs-property">name</span> = <span class="hljs-string">'value'</span>;</code> <button class="copy-button">copy</button></pre>
1083<pre><code class="language-js"><span class="hljs-comment">// ./node_modules/pkg/wrapper.mjs</span>
1084<span class="hljs-keyword">import</span> cjsModule <span class="hljs-keyword">from</span> <span class="hljs-string">'./index.cjs'</span>;
1085<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> name = cjsModule.<span class="hljs-property">name</span>;</code> <button class="copy-button">copy</button></pre>
1086<p>In this example, the <code>name</code> from <code>import { name } from 'pkg'</code> is the same
1087singleton as the <code>name</code> from <code>const { name } = require('pkg')</code>. Therefore <code>===</code>
1088returns <code>true</code> when comparing the two <code>name</code>s and the divergent specifier hazard
1089is avoided.</p>
1090<p>If the module is not simply a list of named exports, but rather contains a
1091unique function or object export like <code>module.exports = function () { ... }</code>,
1092or if support in the wrapper for the <code>import pkg from 'pkg'</code> pattern is desired,
1093then the wrapper would instead be written to export the default optionally
1094along with any named exports as well:</p>
1095<pre><code class="language-js"><span class="hljs-keyword">import</span> cjsModule <span class="hljs-keyword">from</span> <span class="hljs-string">'./index.cjs'</span>;
1096<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> name = cjsModule.<span class="hljs-property">name</span>;
1097<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> cjsModule;</code> <button class="copy-button">copy</button></pre>
1098<p>This approach is appropriate for any of the following use cases:</p>
1099<ul>
1100<li>The package is currently written in CommonJS and the author would prefer not
1101to refactor it into ES module syntax, but wishes to provide named exports for
1102ES module consumers.</li>
1103<li>The package has other packages that depend on it, and the end user might
1104install both this package and those other packages. For example a <code>utilities</code>
1105package is used directly in an application, and a <code>utilities-plus</code> package
1106adds a few more functions to <code>utilities</code>. Because the wrapper exports
1107underlying CommonJS files, it doesn't matter if <code>utilities-plus</code> is written in
1108CommonJS or ES module syntax; it will work either way.</li>
1109<li>The package stores internal state, and the package author would prefer not to
1110refactor the package to isolate its state management. See the next section.</li>
1111</ul>
1112<p>A variant of this approach not requiring conditional exports for consumers could
1113be to add an export, e.g. <code>"./module"</code>, to point to an all-ES module-syntax
1114version of the package. This could be used via <code>import 'pkg/module'</code> by users
1115who are certain that the CommonJS version will not be loaded anywhere in the
1116application, such as by dependencies; or if the CommonJS version can be loaded
1117but doesn't affect the ES module version (for example, because the package is
1118stateless):</p>
1119<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</span>
1120<span class="hljs-punctuation">{</span>
1121  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span><span class="hljs-punctuation">,</span>
1122  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
1123    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span><span class="hljs-punctuation">,</span>
1124    <span class="hljs-attr">"./module"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./wrapper.mjs"</span>
1125  <span class="hljs-punctuation">}</span>
1126<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
1127<h5>Approach #2: Isolate state<span><a class="mark" href="#approach-2-isolate-state" id="approach-2-isolate-state">#</a></span><a aria-hidden="true" class="legacy" id="packages_approach_2_isolate_state"></a></h5>
1128<p>A <a href="#nodejs-packagejson-field-definitions"><code>package.json</code></a> file can define the separate CommonJS and ES module entry
1129points directly:</p>
1130<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</span>
1131<span class="hljs-punctuation">{</span>
1132  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span><span class="hljs-punctuation">,</span>
1133  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
1134    <span class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.mjs"</span><span class="hljs-punctuation">,</span>
1135    <span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span>
1136  <span class="hljs-punctuation">}</span>
1137<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
1138<p>This can be done if both the CommonJS and ES module versions of the package are
1139equivalent, for example because one is the transpiled output of the other; and
1140the package's management of state is carefully isolated (or the package is
1141stateless).</p>
1142<p>The reason that state is an issue is because both the CommonJS and ES module
1143versions of the package might get used within an application; for example, the
1144user's application code could <code>import</code> the ES module version while a dependency
1145<code>require</code>s the CommonJS version. If that were to occur, two copies of the
1146package would be loaded in memory and therefore two separate states would be
1147present. This would likely cause hard-to-troubleshoot bugs.</p>
1148<p>Aside from writing a stateless package (if JavaScript's <code>Math</code> were a package,
1149for example, it would be stateless as all of its methods are static), there are
1150some ways to isolate state so that it's shared between the potentially loaded
1151CommonJS and ES module instances of the package:</p>
1152<ol>
1153<li>
1154<p>If possible, contain all state within an instantiated object. JavaScript's
1155<code>Date</code>, for example, needs to be instantiated to contain state; if it were a
1156package, it would be used like this:</p>
1157<pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-title class_">Date</span> <span class="hljs-keyword">from</span> <span class="hljs-string">'date'</span>;
1158<span class="hljs-keyword">const</span> someDate = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>();
1159<span class="hljs-comment">// someDate contains state; Date does not</span></code> <button class="copy-button">copy</button></pre>
1160<p>The <code>new</code> keyword isn't required; a package's function can return a new
1161object, or modify a passed-in object, to keep the state external to the
1162package.</p>
1163</li>
1164<li>
1165<p>Isolate the state in one or more CommonJS files that are shared between the
1166CommonJS and ES module versions of the package. For example, if the CommonJS
1167and ES module entry points are <code>index.cjs</code> and <code>index.mjs</code>, respectively:</p>
1168<pre><code class="language-js cjs"><span class="hljs-comment">// ./node_modules/pkg/index.cjs</span>
1169<span class="hljs-keyword">const</span> state = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./state.cjs'</span>);
1170<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<span class="hljs-property">state</span> = state;</code> <button class="copy-button">copy</button></pre>
1171<pre><code class="language-js"><span class="hljs-comment">// ./node_modules/pkg/index.mjs</span>
1172<span class="hljs-keyword">import</span> state <span class="hljs-keyword">from</span> <span class="hljs-string">'./state.cjs'</span>;
1173<span class="hljs-keyword">export</span> {
1174  state,
1175};</code> <button class="copy-button">copy</button></pre>
1176<p>Even if <code>pkg</code> is used via both <code>require</code> and <code>import</code> in an application (for
1177example, via <code>import</code> in application code and via <code>require</code> by a dependency)
1178each reference of <code>pkg</code> will contain the same state; and modifying that
1179state from either module system will apply to both.</p>
1180</li>
1181</ol>
1182<p>Any plugins that attach to the package's singleton would need to separately
1183attach to both the CommonJS and ES module singletons.</p>
1184<p>This approach is appropriate for any of the following use cases:</p>
1185<ul>
1186<li>The package is currently written in ES module syntax and the package author
1187wants that version to be used wherever such syntax is supported.</li>
1188<li>The package is stateless or its state can be isolated without too much
1189difficulty.</li>
1190<li>The package is unlikely to have other public packages that depend on it, or if
1191it does, the package is stateless or has state that need not be shared between
1192dependencies or with the overall application.</li>
1193</ul>
1194<p>Even with isolated state, there is still the cost of possible extra code
1195execution between the CommonJS and ES module versions of a package.</p>
1196<p>As with the previous approach, a variant of this approach not requiring
1197conditional exports for consumers could be to add an export, e.g.
1198<code>"./module"</code>, to point to an all-ES module-syntax version of the package:</p>
1199<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</span>
1200<span class="hljs-punctuation">{</span>
1201  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span><span class="hljs-punctuation">,</span>
1202  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
1203    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span><span class="hljs-punctuation">,</span>
1204    <span class="hljs-attr">"./module"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.mjs"</span>
1205  <span class="hljs-punctuation">}</span>
1206<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
1207</section><section><h3>Node.js <code>package.json</code> field definitions<span><a class="mark" href="#nodejs-packagejson-field-definitions" id="nodejs-packagejson-field-definitions">#</a></span><a aria-hidden="true" class="legacy" id="packages_node_js_package_json_field_definitions"></a></h3>
1208<p>This section describes the fields used by the Node.js runtime. Other tools (such
1209as <a href="https://docs.npmjs.com/cli/v8/configuring-npm/package-json">npm</a>) use
1210additional fields which are ignored by Node.js and not documented here.</p>
1211<p>The following fields in <code>package.json</code> files are used in Node.js:</p>
1212<ul>
1213<li><a href="#name"><code>"name"</code></a> - Relevant when using named imports within a package. Also used
1214by package managers as the name of the package.</li>
1215<li><a href="#main"><code>"main"</code></a> - The default module when loading the package, if exports is not
1216specified, and in versions of Node.js prior to the introduction of exports.</li>
1217<li><a href="#packagemanager"><code>"packageManager"</code></a> - The package manager recommended when contributing to
1218the package. Leveraged by the <a href="corepack.html">Corepack</a> shims.</li>
1219<li><a href="#type"><code>"type"</code></a> - The package type determining whether to load <code>.js</code> files as
1220CommonJS or ES modules.</li>
1221<li><a href="#exports"><code>"exports"</code></a> - Package exports and conditional exports. When present,
1222limits which submodules can be loaded from within the package.</li>
1223<li><a href="#imports"><code>"imports"</code></a> - Package imports, for use by modules within the package
1224itself.</li>
1225</ul>
1226<h4><code>"name"</code><span><a class="mark" href="#name" id="name">#</a></span><a aria-hidden="true" class="legacy" id="packages_name"></a></h4>
1227<div class="api_metadata">
1228<details class="changelog"><summary>History</summary>
1229<table>
1230<tbody><tr><th>Version</th><th>Changes</th></tr>
1231<tr><td>v13.6.0, v12.16.0</td>
1232<td><p>Remove the <code>--experimental-resolve-self</code> option.</p></td></tr>
1233<tr><td>v13.1.0, v12.16.0</td>
1234<td><p><span>Added in: v13.1.0, v12.16.0</span></p></td></tr>
1235</tbody></table>
1236</details>
1237</div>
1238<ul>
1239<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1240</ul>
1241<pre><code class="language-json"><span class="hljs-punctuation">{</span>
1242  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"package-name"</span>
1243<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
1244<p>The <code>"name"</code> field defines your package's name. Publishing to the
1245<em>npm</em> registry requires a name that satisfies
1246<a href="https://docs.npmjs.com/files/package.json#name">certain requirements</a>.</p>
1247<p>The <code>"name"</code> field can be used in addition to the <a href="#exports"><code>"exports"</code></a> field to
1248<a href="#self-referencing-a-package-using-its-name">self-reference</a> a package using its name.</p>
1249<h4><code>"main"</code><span><a class="mark" href="#main" id="main">#</a></span><a aria-hidden="true" class="legacy" id="packages_main"></a></h4>
1250<div class="api_metadata">
1251<span>Added in: v0.4.0</span>
1252</div>
1253<ul>
1254<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1255</ul>
1256<pre><code class="language-json"><span class="hljs-punctuation">{</span>
1257  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.js"</span>
1258<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
1259<p>The <code>"main"</code> field defines the entry point of a package when imported by name
1260via a <code>node_modules</code> lookup.  Its value is a path.</p>
1261<p>When a package has an <a href="#exports"><code>"exports"</code></a> field, this will take precedence over the
1262<code>"main"</code> field when importing the package by name.</p>
1263<p>It also defines the script that is used when the <a href="modules.html#folders-as-modules">package directory is loaded
1264via <code>require()</code></a>.</p>
1265<pre><code class="language-js cjs"><span class="hljs-comment">// This resolves to ./path/to/directory/index.js.</span>
1266<span class="hljs-built_in">require</span>(<span class="hljs-string">'./path/to/directory'</span>);</code> <button class="copy-button">copy</button></pre>
1267<h4><code>"packageManager"</code><span><a class="mark" href="#packagemanager" id="packagemanager">#</a></span><a aria-hidden="true" class="legacy" id="packages_packagemanager"></a></h4>
1268<div class="api_metadata">
1269<span>Added in: v16.9.0, v14.19.0</span>
1270</div>
1271<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
1272<ul>
1273<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1274</ul>
1275<pre><code class="language-json"><span class="hljs-punctuation">{</span>
1276  <span class="hljs-attr">"packageManager"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"&#x3C;package manager name>@&#x3C;version>"</span>
1277<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
1278<p>The <code>"packageManager"</code> field defines which package manager is expected to be
1279used when working on the current project. It can be set to any of the
1280<a href="corepack.html#supported-package-managers">supported package managers</a>, and will ensure that your teams use the exact
1281same package manager versions without having to install anything else other than
1282Node.js.</p>
1283<p>This field is currently experimental and needs to be opted-in; check the
1284<a href="corepack.html">Corepack</a> page for details about the procedure.</p>
1285<h4><code>"type"</code><span><a class="mark" href="#type" id="type">#</a></span><a aria-hidden="true" class="legacy" id="packages_type"></a></h4>
1286<div class="api_metadata">
1287<details class="changelog"><summary>History</summary>
1288<table>
1289<tbody><tr><th>Version</th><th>Changes</th></tr>
1290<tr><td>v13.2.0, v12.17.0</td>
1291<td><p>Unflag <code>--experimental-modules</code>.</p></td></tr>
1292<tr><td>v12.0.0</td>
1293<td><p><span>Added in: v12.0.0</span></p></td></tr>
1294</tbody></table>
1295</details>
1296</div>
1297<ul>
1298<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1299</ul>
1300<p>The <code>"type"</code> field defines the module format that Node.js uses for all
1301<code>.js</code> files that have that <code>package.json</code> file as their nearest parent.</p>
1302<p>Files ending with <code>.js</code> are loaded as ES modules when the nearest parent
1303<code>package.json</code> file contains a top-level field <code>"type"</code> with a value of
1304<code>"module"</code>.</p>
1305<p>The nearest parent <code>package.json</code> is defined as the first <code>package.json</code> found
1306when searching in the current folder, that folder's parent, and so on up
1307until a node_modules folder or the volume root is reached.</p>
1308<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
1309<span class="hljs-punctuation">{</span>
1310  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span>
1311<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
1312<pre><code class="language-bash"><span class="hljs-comment"># In same folder as preceding package.json</span>
1313node my-app.js <span class="hljs-comment"># Runs as ES module</span></code> <button class="copy-button">copy</button></pre>
1314<p>If the nearest parent <code>package.json</code> lacks a <code>"type"</code> field, or contains
1315<code>"type": "commonjs"</code>, <code>.js</code> files are treated as <a href="modules.html">CommonJS</a>. If the volume
1316root is reached and no <code>package.json</code> is found, <code>.js</code> files are treated as
1317<a href="modules.html">CommonJS</a>.</p>
1318<p><code>import</code> statements of <code>.js</code> files are treated as ES modules if the nearest
1319parent <code>package.json</code> contains <code>"type": "module"</code>.</p>
1320<pre><code class="language-js"><span class="hljs-comment">// my-app.js, part of the same example as above</span>
1321<span class="hljs-keyword">import</span> <span class="hljs-string">'./startup.js'</span>; <span class="hljs-comment">// Loaded as ES module because of package.json</span></code> <button class="copy-button">copy</button></pre>
1322<p>Regardless of the value of the <code>"type"</code> field, <code>.mjs</code> files are always treated
1323as ES modules and <code>.cjs</code> files are always treated as CommonJS.</p>
1324<h4><code>"exports"</code><span><a class="mark" href="#exports" id="exports">#</a></span><a aria-hidden="true" class="legacy" id="packages_exports"></a></h4>
1325<div class="api_metadata">
1326<details class="changelog"><summary>History</summary>
1327<table>
1328<tbody><tr><th>Version</th><th>Changes</th></tr>
1329<tr><td>v14.13.0, v12.20.0</td>
1330<td><p>Add support for <code>"exports"</code> patterns.</p></td></tr>
1331<tr><td>v13.7.0, v12.17.0</td>
1332<td><p>Unflag conditional exports.</p></td></tr>
1333<tr><td>v13.7.0, v12.16.0</td>
1334<td><p>Implement logical conditional exports ordering.</p></td></tr>
1335<tr><td>v13.7.0, v12.16.0</td>
1336<td><p>Remove the <code>--experimental-conditional-exports</code> option. In 12.16.0, conditional exports are still behind <code>--experimental-modules</code>.</p></td></tr>
1337<tr><td>v13.2.0, v12.16.0</td>
1338<td><p>Implement conditional exports.</p></td></tr>
1339<tr><td>v12.7.0</td>
1340<td><p><span>Added in: v12.7.0</span></p></td></tr>
1341</tbody></table>
1342</details>
1343</div>
1344<ul>
1345<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
1346</ul>
1347<pre><code class="language-json"><span class="hljs-punctuation">{</span>
1348  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.js"</span>
1349<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
1350<p>The <code>"exports"</code> field allows defining the <a href="#package-entry-points">entry points</a> of a package when
1351imported by name loaded either via a <code>node_modules</code> lookup or a
1352<a href="#self-referencing-a-package-using-its-name">self-reference</a> to its own name. It is supported in Node.js 12+ as an
1353alternative to the <a href="#main"><code>"main"</code></a> that can support defining <a href="#subpath-exports">subpath exports</a>
1354and <a href="#conditional-exports">conditional exports</a> while encapsulating internal unexported modules.</p>
1355<p><a href="#conditional-exports">Conditional Exports</a> can also be used within <code>"exports"</code> to define different
1356package entry points per environment, including whether the package is
1357referenced via <code>require</code> or via <code>import</code>.</p>
1358<p>All paths defined in the <code>"exports"</code> must be relative file URLs starting with
1359<code>./</code>.</p>
1360<h4><code>"imports"</code><span><a class="mark" href="#imports" id="imports">#</a></span><a aria-hidden="true" class="legacy" id="packages_imports"></a></h4>
1361<div class="api_metadata">
1362<span>Added in: v14.6.0, v12.19.0</span>
1363</div>
1364<ul>
1365<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
1366</ul>
1367<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
1368<span class="hljs-punctuation">{</span>
1369  <span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
1370    <span class="hljs-attr">"#dep"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
1371      <span class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"dep-node-native"</span><span class="hljs-punctuation">,</span>
1372      <span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./dep-polyfill.js"</span>
1373    <span class="hljs-punctuation">}</span>
1374  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
1375  <span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
1376    <span class="hljs-attr">"dep-node-native"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"^1.0.0"</span>
1377  <span class="hljs-punctuation">}</span>
1378<span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
1379<p>Entries in the imports field must be strings starting with <code>#</code>.</p>
1380<p>Package imports permit mapping to external packages.</p>
1381<p>This field defines <a href="#subpath-imports">subpath imports</a> for the current package.</p></section>
1382        <!-- API END -->
1383      </div>
1384    </div>
1385  </div>
1386</body>
1387</html>
1388