• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v14.19.1">
7  <title>Modules: Packages | Node.js v14.19.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</head>
13<body class="alt apidoc" id="api-section-packages">
14  <div id="content" class="clearfix">
15    <div id="column2" class="interior">
16      <div id="intro" class="interior">
17        <a href="/" title="Go back to the home page">
18          Node.js
19        </a>
20      </div>
21      <ul>
22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
24</ul>
25<hr class="line">
26<ul>
27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
30<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
35<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
36<li><a href="console.html" class="nav-console">Console</a></li>
37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
42<li><a href="dns.html" class="nav-dns">DNS</a></li>
43<li><a href="domain.html" class="nav-domain">Domain</a></li>
44<li><a href="errors.html" class="nav-errors">Errors</a></li>
45<li><a href="events.html" class="nav-events">Events</a></li>
46<li><a href="fs.html" class="nav-fs">File system</a></li>
47<li><a href="globals.html" class="nav-globals">Globals</a></li>
48<li><a href="http.html" class="nav-http">HTTP</a></li>
49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
50<li><a href="https.html" class="nav-https">HTTPS</a></li>
51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
52<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
56<li><a href="packages.html" class="nav-packages active">Modules: Packages</a></li>
57<li><a href="net.html" class="nav-net">Net</a></li>
58<li><a href="os.html" class="nav-os">OS</a></li>
59<li><a href="path.html" class="nav-path">Path</a></li>
60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
61<li><a href="policy.html" class="nav-policy">Policies</a></li>
62<li><a href="process.html" class="nav-process">Process</a></li>
63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
65<li><a href="readline.html" class="nav-readline">Readline</a></li>
66<li><a href="repl.html" class="nav-repl">REPL</a></li>
67<li><a href="report.html" class="nav-report">Report</a></li>
68<li><a href="stream.html" class="nav-stream">Stream</a></li>
69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
70<li><a href="timers.html" class="nav-timers">Timers</a></li>
71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
73<li><a href="tty.html" class="nav-tty">TTY</a></li>
74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
75<li><a href="url.html" class="nav-url">URL</a></li>
76<li><a href="util.html" class="nav-util">Utilities</a></li>
77<li><a href="v8.html" class="nav-v8">V8</a></li>
78<li><a href="vm.html" class="nav-vm">VM</a></li>
79<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
82</ul>
83<hr class="line">
84<ul>
85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
86</ul>
87    </div>
88
89    <div id="column1" data-id="packages" class="interior">
90      <header>
91        <div class="header-container">
92          <h1>Node.js v14.19.1 documentation</h1>
93          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
94            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
95              <path fill="none" d="M0 0h24v24H0z" />
96              <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"/>
97              <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"/>
98            </svg>
99            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
100              <path d="M0 0h24v24H0z" fill="none" />
101              <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"/>
102            </svg>
103          </button>
104        </div>
105        <div id="gtoc">
106          <ul>
107            <li>
108              <a href="index.html">Index</a>
109            </li>
110            <li>
111              <a href="all.html">View on single page</a>
112            </li>
113            <li>
114              <a href="packages.json">View as JSON</a>
115            </li>
116
117    <li class="version-picker">
118      <a href="#">View another version <span>&#x25bc;</span></a>
119      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v17.x/api/packages.html">17.x</a></li>
120<li><a href="https://nodejs.org/docs/latest-v16.x/api/packages.html">16.x <b>LTS</b></a></li>
121<li><a href="https://nodejs.org/docs/latest-v15.x/api/packages.html">15.x</a></li>
122<li><a href="https://nodejs.org/docs/latest-v14.x/api/packages.html">14.x <b>LTS</b></a></li>
123<li><a href="https://nodejs.org/docs/latest-v13.x/api/packages.html">13.x</a></li>
124<li><a href="https://nodejs.org/docs/latest-v12.x/api/packages.html">12.x <b>LTS</b></a></li></ol>
125    </li>
126
127            <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/packages.md">Edit on GitHub</a></li>
128          </ul>
129        </div>
130        <hr>
131      </header>
132
133      <details id="toc" open><summary>Table of contents</summary><ul>
134<li><a href="#packages_modules_packages">Modules: Packages</a>
135<ul>
136<li><a href="#packages_introduction">Introduction</a></li>
137<li><a href="#packages_determining_module_system">Determining module system</a>
138<ul>
139<li><a href="#packages_package_json_and_file_extensions"><code>package.json</code> and file extensions</a></li>
140<li><a href="#packages_input_type_flag"><code>--input-type</code> flag</a></li>
141</ul>
142</li>
143<li><span class="stability_1"><a href="#packages_determining_package_manager">Determining package manager</a></span></li>
144<li><a href="#packages_package_entry_points">Package entry points</a>
145<ul>
146<li><a href="#packages_main_entry_point_export">Main entry point export</a></li>
147<li><a href="#packages_subpath_exports">Subpath exports</a></li>
148<li><a href="#packages_subpath_imports">Subpath imports</a></li>
149<li><a href="#packages_subpath_patterns">Subpath patterns</a></li>
150<li><a href="#packages_exports_sugar">Exports sugar</a></li>
151<li><a href="#packages_conditional_exports">Conditional exports</a></li>
152<li><a href="#packages_nested_conditions">Nested conditions</a></li>
153<li><a href="#packages_resolving_user_conditions">Resolving user conditions</a></li>
154<li><a href="#packages_conditions_definitions">Conditions Definitions</a></li>
155<li><a href="#packages_self_referencing_a_package_using_its_name">Self-referencing a package using its name</a></li>
156</ul>
157</li>
158<li><a href="#packages_dual_commonjs_es_module_packages">Dual CommonJS/ES module packages</a>
159<ul>
160<li><a href="#packages_dual_package_hazard">Dual package hazard</a></li>
161<li><a href="#packages_writing_dual_packages_while_avoiding_or_minimizing_hazards">Writing dual packages while avoiding or minimizing hazards</a>
162<ul>
163<li><a href="#packages_approach_1_use_an_es_module_wrapper">Approach #1: Use an ES module wrapper</a></li>
164<li><a href="#packages_approach_2_isolate_state">Approach #2: Isolate state</a></li>
165</ul>
166</li>
167</ul>
168</li>
169<li><a href="#packages_node_js_package_json_field_definitions">Node.js <code>package.json</code> field definitions</a>
170<ul>
171<li><a href="#packages_name"><code>"name"</code></a></li>
172<li><a href="#packages_main"><code>"main"</code></a></li>
173<li><span class="stability_1"><a href="#packages_packagemanager"><code>"packageManager"</code></a></span></li>
174<li><a href="#packages_type"><code>"type"</code></a></li>
175<li><a href="#packages_exports"><code>"exports"</code></a></li>
176<li><a href="#packages_imports"><code>"imports"</code></a></li>
177</ul>
178</li>
179</ul>
180</li>
181</ul></details>
182
183      <div id="apicontent">
184        <h2>Modules: Packages<span><a class="mark" href="#packages_modules_packages" id="packages_modules_packages">#</a></span></h2>
185
186
187<div class="api_metadata">
188<details class="changelog"><summary>History</summary>
189<table>
190<tbody><tr><th>Version</th><th>Changes</th></tr>
191<tr><td>v14.13.0</td>
192<td><p>Add support for <code>"exports"</code> patterns.</p></td></tr>
193<tr><td>v14.6.0, v12.19.0</td>
194<td><p>Add package <code>"imports"</code> field.</p></td></tr>
195<tr><td>v13.7.0, v12.16.0</td>
196<td><p>Unflag conditional exports.</p></td></tr>
197<tr><td>v13.6.0, v12.16.0</td>
198<td><p>Unflag self-referencing a package using its name.</p></td></tr>
199<tr><td>v12.7.0</td>
200<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>
201<tr><td>v12.0.0</td>
202<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>
203</tbody></table>
204</details>
205</div>
206<section><h3>Introduction<span><a class="mark" href="#packages_introduction" id="packages_introduction">#</a></span></h3>
207<p>A package is a folder tree described by a <code>package.json</code> file. The package
208consists of the folder containing the <code>package.json</code> file and all subfolders
209until the next folder containing another <code>package.json</code> file, or a folder
210named <code>node_modules</code>.</p>
211<p>This page provides guidance for package authors writing <code>package.json</code> files
212along with a reference for the <a href="#packages_node_js_package_json_field_definitions"><code>package.json</code></a> fields defined by Node.js.</p>
213</section><section><h3>Determining module system<span><a class="mark" href="#packages_determining_module_system" id="packages_determining_module_system">#</a></span></h3>
214<p>Node.js will treat the following as <a href="esm.html">ES modules</a> when passed to <code>node</code> as the
215initial input, or when referenced by <code>import</code> statements within ES module code:</p>
216<ul>
217<li>
218<p>Files ending in <code>.mjs</code>.</p>
219</li>
220<li>
221<p>Files ending in <code>.js</code> when the nearest parent <code>package.json</code> file contains a
222top-level <a href="#packages_type"><code>"type"</code></a> field with a value of <code>"module"</code>.</p>
223</li>
224<li>
225<p>Strings passed in as an argument to <code>--eval</code>, or piped to <code>node</code> via <code>STDIN</code>,
226with the flag <code>--input-type=module</code>.</p>
227</li>
228</ul>
229<p>Node.js will treat as <a href="modules.html">CommonJS</a> all other forms of input, such as <code>.js</code> files
230where the nearest parent <code>package.json</code> file contains no top-level <code>"type"</code>
231field, or string input without the flag <code>--input-type</code>. This behavior is to
232preserve backward compatibility. However, now that Node.js supports both
233CommonJS and ES modules, it is best to be explicit whenever possible. Node.js
234will treat the following as CommonJS when passed to <code>node</code> as the initial input,
235or when referenced by <code>import</code> statements within ES module code:</p>
236<ul>
237<li>
238<p>Files ending in <code>.cjs</code>.</p>
239</li>
240<li>
241<p>Files ending in <code>.js</code> when the nearest parent <code>package.json</code> file contains a
242top-level field <a href="#packages_type"><code>"type"</code></a> with a value of <code>"commonjs"</code>.</p>
243</li>
244<li>
245<p>Strings passed in as an argument to <code>--eval</code> or <code>--print</code>, or piped to <code>node</code>
246via <code>STDIN</code>, with the flag <code>--input-type=commonjs</code>.</p>
247</li>
248</ul>
249<p>Package authors should include the <a href="#packages_type"><code>"type"</code></a> field, even in packages where
250all sources are CommonJS. Being explicit about the <code>type</code> of the package will
251future-proof the package in case the default type of Node.js ever changes, and
252it will also make things easier for build tools and loaders to determine how the
253files in the package should be interpreted.</p>
254<h4><code>package.json</code> and file extensions<span><a class="mark" href="#packages_package_json_and_file_extensions" id="packages_package_json_and_file_extensions">#</a></span></h4>
255<p>Within a package, the <a href="#packages_node_js_package_json_field_definitions"><code>package.json</code></a> <a href="#packages_type"><code>"type"</code></a> field defines how
256Node.js should interpret <code>.js</code> files. If a <code>package.json</code> file does not have a
257<code>"type"</code> field, <code>.js</code> files are treated as <a href="modules.html">CommonJS</a>.</p>
258<p>A <code>package.json</code> <code>"type"</code> value of <code>"module"</code> tells Node.js to interpret <code>.js</code>
259files within that package as using <a href="esm.html">ES module</a> syntax.</p>
260<p>The <code>"type"</code> field applies not only to initial entry points (<code>node my-app.js</code>)
261but also to files referenced by <code>import</code> statements and <code>import()</code> expressions.</p>
262<pre><code class="language-js"><span class="hljs-comment">// my-app.js, treated as an ES module because there is a package.json</span>
263<span class="hljs-comment">// file in the same folder with "type": "module".</span>
264
265<span class="hljs-keyword">import</span> <span class="hljs-string">'./startup/init.js'</span>;
266<span class="hljs-comment">// Loaded as ES module since ./startup contains no package.json file,</span>
267<span class="hljs-comment">// and therefore inherits the "type" value from one level up.</span>
268
269<span class="hljs-keyword">import</span> <span class="hljs-string">'commonjs-package'</span>;
270<span class="hljs-comment">// Loaded as CommonJS since ./node_modules/commonjs-package/package.json</span>
271<span class="hljs-comment">// lacks a "type" field or contains "type": "commonjs".</span>
272
273<span class="hljs-keyword">import</span> <span class="hljs-string">'./node_modules/commonjs-package/index.js'</span>;
274<span class="hljs-comment">// Loaded as CommonJS since ./node_modules/commonjs-package/package.json</span>
275<span class="hljs-comment">// lacks a "type" field or contains "type": "commonjs".</span></code></pre>
276<p>Files ending with <code>.mjs</code> are always loaded as <a href="esm.html">ES modules</a> regardless of
277the nearest parent <code>package.json</code>.</p>
278<p>Files ending with <code>.cjs</code> are always loaded as <a href="modules.html">CommonJS</a> regardless of the
279nearest parent <code>package.json</code>.</p>
280<pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'./legacy-file.cjs'</span>;
281<span class="hljs-comment">// Loaded as CommonJS since .cjs is always loaded as CommonJS.</span>
282
283<span class="hljs-keyword">import</span> <span class="hljs-string">'commonjs-package/src/index.mjs'</span>;
284<span class="hljs-comment">// Loaded as ES module since .mjs is always loaded as ES module.</span></code></pre>
285<p>The <code>.mjs</code> and <code>.cjs</code> extensions can be used to mix types within the same
286package:</p>
287<ul>
288<li>
289<p>Within a <code>"type": "module"</code> package, Node.js can be instructed to
290interpret a particular file as <a href="modules.html">CommonJS</a> by naming it with a <code>.cjs</code>
291extension (since both <code>.js</code> and <code>.mjs</code> files are treated as ES modules within
292a <code>"module"</code> package).</p>
293</li>
294<li>
295<p>Within a <code>"type": "commonjs"</code> package, Node.js can be instructed to
296interpret a particular file as an <a href="esm.html">ES module</a> by naming it with an <code>.mjs</code>
297extension (since both <code>.js</code> and <code>.cjs</code> files are treated as CommonJS within a
298<code>"commonjs"</code> package).</p>
299</li>
300</ul>
301<h4><code>--input-type</code> flag<span><a class="mark" href="#packages_input_type_flag" id="packages_input_type_flag">#</a></span></h4>
302<div class="api_metadata">
303<span>Added in: v12.0.0</span>
304</div>
305<p>Strings passed in as an argument to <code>--eval</code> (or <code>-e</code>), or piped to <code>node</code> via
306<code>STDIN</code>, are treated as <a href="esm.html">ES modules</a> when the <code>--input-type=module</code> flag
307is set.</p>
308<pre><code class="language-bash">node --input-type=module --<span class="hljs-built_in">eval</span> <span class="hljs-string">"import { sep } from 'path'; console.log(sep);"</span>
309
310<span class="hljs-built_in">echo</span> <span class="hljs-string">"import { sep } from 'path'; console.log(sep);"</span> | node --input-type=module</code></pre>
311<p>For completeness there is also <code>--input-type=commonjs</code>, for explicitly running
312string input as CommonJS. This is the default behavior if <code>--input-type</code> is
313unspecified.</p>
314</section><section><h3>Determining package manager<span><a class="mark" href="#packages_determining_package_manager" id="packages_determining_package_manager">#</a></span></h3>
315<p></p><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental</div><p></p>
316<p>While all Node.js projects are expected to be installable by all package
317managers once published, their development teams are often required to use one
318specific package manager. To make this process easier, Node.js ships with a
319tool called <a href="corepack.html">Corepack</a> that aims to make all package managers transparently
320available in your environment - provided you have Node.js installed.</p>
321<p>By default Corepack won't enforce any specific package manager and will use
322the generic "Last Known Good" versions associated with each Node.js release,
323but you can improve this experience by setting the <a href="#packages_packagemanager"><code>"packageManager"</code></a> field
324in your project's <code>package.json</code>.</p>
325</section><section><h3>Package entry points<span><a class="mark" href="#packages_package_entry_points" id="packages_package_entry_points">#</a></span></h3>
326<p>In a package’s <code>package.json</code> file, two fields can define entry points for a
327package: <a href="#packages_main"><code>"main"</code></a> and <a href="#packages_exports"><code>"exports"</code></a>. The <a href="#packages_main"><code>"main"</code></a> field is supported
328in all versions of Node.js, but its capabilities are limited: it only defines
329the main entry point of the package.</p>
330<p>The <a href="#packages_exports"><code>"exports"</code></a> field provides an alternative to <a href="#packages_main"><code>"main"</code></a> where the
331package main entry point can be defined while also encapsulating the package,
332<strong>preventing any other entry points besides those defined in <a href="#packages_exports"><code>"exports"</code></a></strong>.
333This encapsulation allows module authors to define a public interface for
334their package.</p>
335<p>If both <a href="#packages_exports"><code>"exports"</code></a> and <a href="#packages_main"><code>"main"</code></a> are defined, the <a href="#packages_exports"><code>"exports"</code></a> field
336takes precedence over <a href="#packages_main"><code>"main"</code></a>. <a href="#packages_exports"><code>"exports"</code></a> are not specific to ES
337modules or CommonJS; <a href="#packages_main"><code>"main"</code></a> is overridden by <a href="#packages_exports"><code>"exports"</code></a> if it
338exists. As such <a href="#packages_main"><code>"main"</code></a> cannot be used as a fallback for CommonJS but it
339can be used as a fallback for legacy versions of Node.js that do not support the
340<a href="#packages_exports"><code>"exports"</code></a> field.</p>
341<p><a href="#packages_conditional_exports">Conditional exports</a> can be used within <a href="#packages_exports"><code>"exports"</code></a> to define different
342package entry points per environment, including whether the package is
343referenced via <code>require</code> or via <code>import</code>. For more information about supporting
344both CommonJS and ES Modules in a single package please consult
345<a href="#packages_dual_commonjs_es_module_packages">the dual CommonJS/ES module packages section</a>.</p>
346<p><strong>Warning</strong>: Introducing the <a href="#packages_exports"><code>"exports"</code></a> field prevents consumers of a
347package from using any entry points that are not defined, including the
348<a href="#packages_node_js_package_json_field_definitions"><code>package.json</code></a> (e.g. <code>require('your-package/package.json')</code>. <strong>This will
349likely be a breaking change.</strong></p>
350<p>To make the introduction of <a href="#packages_exports"><code>"exports"</code></a> non-breaking, ensure that every
351previously supported entry point is exported. It is best to explicitly specify
352entry points so that the package’s public API is well-defined. For example,
353a project that previous exported <code>main</code>, <code>lib</code>,
354<code>feature</code>, and the <code>package.json</code> could use the following <code>package.exports</code>:</p>
355<pre><code class="language-json"><span class="hljs-punctuation">{</span>
356  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"my-mod"</span><span class="hljs-punctuation">,</span>
357  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
358    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/index.js"</span><span class="hljs-punctuation">,</span>
359    <span class="hljs-attr">"./lib"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/index.js"</span><span class="hljs-punctuation">,</span>
360    <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>
361    <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>
362    <span class="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature/index.js"</span><span class="hljs-punctuation">,</span>
363    <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>
364    <span class="hljs-attr">"./package.json"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./package.json"</span>
365  <span class="hljs-punctuation">}</span>
366<span class="hljs-punctuation">}</span></code></pre>
367<p>Alternatively a project could choose to export entire folders:</p>
368<pre><code class="language-json"><span class="hljs-punctuation">{</span>
369  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"my-mod"</span><span class="hljs-punctuation">,</span>
370  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
371    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/index.js"</span><span class="hljs-punctuation">,</span>
372    <span class="hljs-attr">"./lib"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/index.js"</span><span class="hljs-punctuation">,</span>
373    <span class="hljs-attr">"./lib/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/*.js"</span><span class="hljs-punctuation">,</span>
374    <span class="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature/index.js"</span><span class="hljs-punctuation">,</span>
375    <span class="hljs-attr">"./feature/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature/*.js"</span><span class="hljs-punctuation">,</span>
376    <span class="hljs-attr">"./package.json"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./package.json"</span>
377  <span class="hljs-punctuation">}</span>
378<span class="hljs-punctuation">}</span></code></pre>
379<p>As a last resort, package encapsulation can be disabled entirely by creating an
380export for the root of the package <code>"./*": "./*"</code>. This exposes every file
381in the package at the cost of disabling the encapsulation and potential tooling
382benefits this provides. As the ES Module loader in Node.js enforces the use of
383<a href="esm.html#esm_mandatory_file_extensions">the full specifier path</a>, exporting the root rather than being explicit
384about entry is less expressive than either of the prior examples. Not only
385is encapsulation lost but module consumers are unable to
386<code>import feature from 'my-mod/feature'</code> as they need to provide the full
387path <code>import feature from 'my-mod/feature/index.js</code>.</p>
388<h4>Main entry point export<span><a class="mark" href="#packages_main_entry_point_export" id="packages_main_entry_point_export">#</a></span></h4>
389<p>To set the main entry point for a package, it is advisable to define both
390<a href="#packages_exports"><code>"exports"</code></a> and <a href="#packages_main"><code>"main"</code></a> in the package’s <a href="#packages_node_js_package_json_field_definitions"><code>package.json</code></a> file:</p>
391<pre><code class="language-json"><span class="hljs-punctuation">{</span>
392  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span><span class="hljs-punctuation">,</span>
393  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span>
394<span class="hljs-punctuation">}</span></code></pre>
395<p>When the <a href="#packages_exports"><code>"exports"</code></a> field is defined, all subpaths of the package are
396encapsulated and no longer available to importers. For example,
397<code>require('pkg/subpath.js')</code> throws an <a href="errors.html#errors_err_package_path_not_exported"><code>ERR_PACKAGE_PATH_NOT_EXPORTED</code></a>
398error.</p>
399<p>This encapsulation of exports provides more reliable guarantees
400about package interfaces for tools and when handling semver upgrades for a
401package. It is not a strong encapsulation since a direct require of any
402absolute subpath of the package such as
403<code>require('/path/to/node_modules/pkg/subpath.js')</code> will still load <code>subpath.js</code>.</p>
404<h4>Subpath exports<span><a class="mark" href="#packages_subpath_exports" id="packages_subpath_exports">#</a></span></h4>
405<div class="api_metadata">
406<span>Added in: v12.7.0</span>
407</div>
408<p>When using the <a href="#packages_exports"><code>"exports"</code></a> field, custom subpaths can be defined along
409with the main entry point by treating the main entry point as the
410<code>"."</code> subpath:</p>
411<pre><code class="language-json"><span class="hljs-punctuation">{</span>
412  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span><span class="hljs-punctuation">,</span>
413  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
414    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span><span class="hljs-punctuation">,</span>
415    <span class="hljs-attr">"./submodule"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./src/submodule.js"</span>
416  <span class="hljs-punctuation">}</span>
417<span class="hljs-punctuation">}</span></code></pre>
418<p>Now only the defined subpath in <a href="#packages_exports"><code>"exports"</code></a> can be imported by a consumer:</p>
419<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'</span>;
420<span class="hljs-comment">// Loads ./node_modules/es-module-package/src/submodule.js</span></code></pre>
421<p>While other subpaths will error:</p>
422<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>;
423<span class="hljs-comment">// Throws ERR_PACKAGE_PATH_NOT_EXPORTED</span></code></pre>
424<h4>Subpath imports<span><a class="mark" href="#packages_subpath_imports" id="packages_subpath_imports">#</a></span></h4>
425<div class="api_metadata">
426<span>Added in: v14.6.0, v12.19.0</span>
427</div>
428<p>In addition to the <a href="#packages_exports"><code>"exports"</code></a> field, it is possible to define internal
429package import maps that only apply to import specifiers from within the package
430itself.</p>
431<p>Entries in the imports field must always start with <code>#</code> to ensure they are
432disambiguated from package specifiers.</p>
433<p>For example, the imports field can be used to gain the benefits of conditional
434exports for internal modules:</p>
435<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
436<span class="hljs-punctuation">{</span>
437  <span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
438    <span class="hljs-attr">"#dep"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
439      <span class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"dep-node-native"</span><span class="hljs-punctuation">,</span>
440      <span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./dep-polyfill.js"</span>
441    <span class="hljs-punctuation">}</span>
442  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
443  <span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
444    <span class="hljs-attr">"dep-node-native"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"^1.0.0"</span>
445  <span class="hljs-punctuation">}</span>
446<span class="hljs-punctuation">}</span></code></pre>
447<p>where <code>import '#dep'</code> does not get the resolution of the external package
448<code>dep-node-native</code> (including its exports in turn), and instead gets the local
449file <code>./dep-polyfill.js</code> relative to the package in other environments.</p>
450<p>Unlike the <code>"exports"</code> field, the <code>"imports"</code> field permits mapping to external
451packages.</p>
452<p>The resolution rules for the imports field are otherwise
453analogous to the exports field.</p>
454<h4>Subpath patterns<span><a class="mark" href="#packages_subpath_patterns" id="packages_subpath_patterns">#</a></span></h4>
455<div class="api_metadata">
456<span>Added in: v14.13.0, v12.20.0</span>
457</div>
458<p>For packages with a small number of exports or imports, we recommend
459explicitly listing each exports subpath entry. But for packages that have
460large numbers of subpaths, this might cause <code>package.json</code> bloat and
461maintenance issues.</p>
462<p>For these use cases, subpath export patterns can be used instead:</p>
463<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/es-module-package/package.json</span>
464<span class="hljs-punctuation">{</span>
465  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
466    <span class="hljs-attr">"./features/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./src/features/*.js"</span>
467  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
468  <span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
469    <span class="hljs-attr">"#internal/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./src/internal/*.js"</span>
470  <span class="hljs-punctuation">}</span>
471<span class="hljs-punctuation">}</span></code></pre>
472<p><strong><code>*</code> maps expose nested subpaths as it is a string replacement syntax
473only.</strong></p>
474<p>All instances of <code>*</code> on the right hand side will then be replaced with this
475value, including if it contains any <code>/</code> separators.</p>
476<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'</span>;
477<span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/x.js</span>
478
479<span class="hljs-keyword">import</span> featureY <span class="hljs-keyword">from</span> <span class="hljs-string">'es-module-package/features/y/y'</span>;
480<span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/y/y.js</span>
481
482<span class="hljs-keyword">import</span> internalZ <span class="hljs-keyword">from</span> <span class="hljs-string">'#internal/z'</span>;
483<span class="hljs-comment">// Loads ./node_modules/es-module-package/src/internal/z.js</span></code></pre>
484<p>This is a direct static replacement without any special handling for file
485extensions. In the previous example, <code>pkg/features/x.json</code> would be resolved to
486<code>./src/features/x.json.js</code> in the mapping.</p>
487<p>The property of exports being statically enumerable is maintained with exports
488patterns since the individual exports for a package can be determined by
489treating the right hand side target pattern as a <code>**</code> glob against the list of
490files within the package. Because <code>node_modules</code> paths are forbidden in exports
491targets, this expansion is dependent on only the files of the package itself.</p>
492<p>To exclude private subfolders from patterns, <code>null</code> targets can be used:</p>
493<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/es-module-package/package.json</span>
494<span class="hljs-punctuation">{</span>
495  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
496    <span class="hljs-attr">"./features/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./src/features/*.js"</span><span class="hljs-punctuation">,</span>
497    <span class="hljs-attr">"./features/private-internal/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-keyword">null</span>
498  <span class="hljs-punctuation">}</span>
499<span class="hljs-punctuation">}</span></code></pre>
500<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'</span>;
501<span class="hljs-comment">// Throws: ERR_PACKAGE_PATH_NOT_EXPORTED</span>
502
503<span class="hljs-keyword">import</span> featureX <span class="hljs-keyword">from</span> <span class="hljs-string">'es-module-package/features/x'</span>;
504<span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/x.js</span></code></pre>
505<h4>Exports sugar<span><a class="mark" href="#packages_exports_sugar" id="packages_exports_sugar">#</a></span></h4>
506<div class="api_metadata">
507<span>Added in: v12.11.0</span>
508</div>
509<p>If the <code>"."</code> export is the only export, the <a href="#packages_exports"><code>"exports"</code></a> field provides sugar
510for this case being the direct <a href="#packages_exports"><code>"exports"</code></a> field value.</p>
511<p>If the <code>"."</code> export has a fallback array or string value, then the
512<a href="#packages_exports"><code>"exports"</code></a> field can be set to this value directly.</p>
513<pre><code class="language-json"><span class="hljs-punctuation">{</span>
514  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
515    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span>
516  <span class="hljs-punctuation">}</span>
517<span class="hljs-punctuation">}</span></code></pre>
518<p>can be written:</p>
519<pre><code class="language-json"><span class="hljs-punctuation">{</span>
520  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span>
521<span class="hljs-punctuation">}</span></code></pre>
522<h4>Conditional exports<span><a class="mark" href="#packages_conditional_exports" id="packages_conditional_exports">#</a></span></h4>
523<div class="api_metadata">
524<details class="changelog"><summary>History</summary>
525<table>
526<tbody><tr><th>Version</th><th>Changes</th></tr>
527<tr><td>v13.2.0, v12.16.0</td>
528<td><p><span>Added in: v13.2.0, v12.16.0</span></p></td></tr>
529<tr><td>v13.7.0, v12.16.0</td>
530<td><p>Unflag conditional exports.</p></td></tr>
531</tbody></table>
532</details>
533</div>
534<p>Conditional exports provide a way to map to different paths depending on
535certain conditions. They are supported for both CommonJS and ES module imports.</p>
536<p>For example, a package that wants to provide different ES module exports for
537<code>require()</code> and <code>import</code> can be written:</p>
538<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
539<span class="hljs-punctuation">{</span>
540  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main-require.cjs"</span><span class="hljs-punctuation">,</span>
541  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
542    <span class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main-module.js"</span><span class="hljs-punctuation">,</span>
543    <span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main-require.cjs"</span>
544  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
545  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span>
546<span class="hljs-punctuation">}</span></code></pre>
547<p>Node.js implements the following conditions:</p>
548<ul>
549<li><code>"import"</code> - matches when the package is loaded via <code>import</code> or
550<code>import()</code>, or via any top-level import or resolve operation by the
551ECMAScript module loader. Applies regardless of the module format of the
552target file. <em>Always mutually exclusive with <code>"require"</code>.</em></li>
553<li><code>"require"</code> - matches when the package is loaded via <code>require()</code>. The
554referenced file should be loadable with <code>require()</code> although the condition
555matches regardless of the module format of the target file. Expected
556formats include CommonJS, JSON, and native addons but not ES modules as
557<code>require()</code> doesn't support them. <em>Always mutually exclusive with
558<code>"import"</code>.</em></li>
559<li><code>"node"</code> - matches for any Node.js environment. Can be a CommonJS or ES
560module file. <em>This condition should always come after <code>"import"</code> or
561<code>"require"</code>.</em></li>
562<li><code>"node-addons"</code> - similar to <code>"node"</code> and matches for any Node.js environment.
563This condition can be used to provide an entry point which uses native C++
564addons as opposed to an entry point which is more universal and doesn't rely
565on native addons. This condition can be disabled via the
566<a href="cli.html#cli_no_addons"><code>--no-addons</code> flag</a>.</li>
567<li><code>"default"</code> - the generic fallback that always matches. Can be a CommonJS
568or ES module file. <em>This condition should always come last.</em></li>
569</ul>
570<p>Within the <a href="#packages_exports"><code>"exports"</code></a> object, key order is significant. During condition
571matching, earlier entries have higher priority and take precedence over later
572entries. <em>The general rule is that conditions should be from most specific to
573least specific in object order</em>.</p>
574<p>Using the <code>"import"</code> and <code>"require"</code> conditions can lead to some hazards,
575which are further explained in <a href="#packages_dual_commonjs_es_module_packages">the dual CommonJS/ES module packages section</a>.</p>
576<p>Conditional exports can also be extended to exports subpaths, for example:</p>
577<pre><code class="language-json"><span class="hljs-punctuation">{</span>
578  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span><span class="hljs-punctuation">,</span>
579  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
580    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span><span class="hljs-punctuation">,</span>
581    <span class="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
582      <span class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature-node.js"</span><span class="hljs-punctuation">,</span>
583      <span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature.js"</span>
584    <span class="hljs-punctuation">}</span>
585  <span class="hljs-punctuation">}</span>
586<span class="hljs-punctuation">}</span></code></pre>
587<p>Defines a package where <code>require('pkg/feature')</code> and <code>import 'pkg/feature'</code>
588could provide different implementations between Node.js and other JS
589environments.</p>
590<p>When using environment branches, always include a <code>"default"</code> condition where
591possible. Providing a <code>"default"</code> condition ensures that any unknown JS
592environments are able to use this universal implementation, which helps avoid
593these JS environments from having to pretend to be existing environments in
594order to support packages with conditional exports. For this reason, using
595<code>"node"</code> and <code>"default"</code> condition branches is usually preferable to using
596<code>"node"</code> and <code>"browser"</code> condition branches.</p>
597<h4>Nested conditions<span><a class="mark" href="#packages_nested_conditions" id="packages_nested_conditions">#</a></span></h4>
598<p>In addition to direct mappings, Node.js also supports nested condition objects.</p>
599<p>For example, to define a package that only has dual mode entry points for
600use in Node.js but not the browser:</p>
601<pre><code class="language-json"><span class="hljs-punctuation">{</span>
602  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span><span class="hljs-punctuation">,</span>
603  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
604    <span class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
605      <span class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature-node.mjs"</span><span class="hljs-punctuation">,</span>
606      <span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature-node.cjs"</span>
607    <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
608    <span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature.mjs"</span><span class="hljs-punctuation">,</span>
609  <span class="hljs-punctuation">}</span>
610<span class="hljs-punctuation">}</span></code></pre>
611<p>Conditions continue to be matched in order as with flat conditions. If
612a nested conditional does not have any mapping it will continue checking
613the remaining conditions of the parent condition. In this way nested
614conditions behave analogously to nested JavaScript <code>if</code> statements.</p>
615<h4>Resolving user conditions<span><a class="mark" href="#packages_resolving_user_conditions" id="packages_resolving_user_conditions">#</a></span></h4>
616<div class="api_metadata">
617<span>Added in: v14.9.0, v12.19.0</span>
618</div>
619<p>When running Node.js, custom user conditions can be added with the
620<code>--conditions</code> flag:</p>
621<pre><code class="language-bash">node --conditions=development main.js</code></pre>
622<p>which would then resolve the <code>"development"</code> condition in package imports and
623exports, while resolving the existing <code>"node"</code>, <code>"node-addons"</code>, <code>"default"</code>,
624<code>"import"</code>, and <code>"require"</code> conditions as appropriate.</p>
625<p>Any number of custom conditions can be set with repeat flags.</p>
626<h4>Conditions Definitions<span><a class="mark" href="#packages_conditions_definitions" id="packages_conditions_definitions">#</a></span></h4>
627<p>The <code>"import"</code>, <code>"require"</code>, <code>"node"</code>, <code>"node-addons"</code> and <code>"default"</code>
628conditions are defined and implemented in Node.js core,
629<a href="#packages_conditional_exports">as specified above</a>.</p>
630<p>The <code>"node-addons"</code> condition can be used to provide an entry point which
631uses native C++ addons. However, this condition can be disabled via the
632<a href="cli.html#cli_no_addons"><code>--no-addons</code> flag</a>. When using <code>"node-addons"</code>, it's recommended to treat
633<code>"default"</code> as an enhancement that provides a more universal entry point, e.g.
634using WebAssembly instead of a native addon.</p>
635<p>Other condition strings are unknown to Node.js and thus ignored by default.
636Runtimes or tools other than Node.js can use them at their discretion.</p>
637<p>These user conditions can be enabled in Node.js via the <a href="#packages_resolving_user_conditions"><code>--conditions</code>
638flag</a>.</p>
639<p>The following condition definitions are currently endorsed by Node.js:</p>
640<ul>
641<li><code>"browser"</code> - any environment which implements a standard subset of global
642browser APIs available from JavaScript in web browsers, including the DOM
643APIs.</li>
644<li><code>"development"</code> - can be used to define a development-only environment
645entry point. <em>Must always be mutually exclusive with <code>"production"</code>.</em></li>
646<li><code>"production"</code> - can be used to define a production environment entry
647point. <em>Must always be mutually exclusive with <code>"development"</code>.</em></li>
648</ul>
649<p>The above user conditions can be enabled in Node.js via the <a href="#packages_resolving_user_conditions"><code>--conditions</code>
650flag</a>.</p>
651<p>Platform specific conditions such as <code>"deno"</code>, <code>"electron"</code>, or <code>"react-native"</code>
652may be used, but while there remain no implementation or integration intent
653from these platforms, the above are not explicitly endorsed by Node.js.</p>
654<p>New conditions definitions may be added to this list by creating a pull request
655to 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
656a new condition definition here are that:</p>
657<ul>
658<li>The definition should be clear and unambiguous for all implementers.</li>
659<li>The use case for why the condition is needed should be clearly justified.</li>
660<li>There should exist sufficient existing implementation usage.</li>
661<li>The condition name should not conflict with another condition definition or
662condition in wide usage.</li>
663<li>The listing of the condition definition should provide a coordination
664benefit to the ecosystem that wouldn't otherwise be possible. For example,
665this would not necessarily be the case for company-specific or
666application-specific conditions.</li>
667</ul>
668<p>The above definitions may be moved to a dedicated conditions registry in due
669course.</p>
670<h4>Self-referencing a package using its name<span><a class="mark" href="#packages_self_referencing_a_package_using_its_name" id="packages_self_referencing_a_package_using_its_name">#</a></span></h4>
671<div class="api_metadata">
672<details class="changelog"><summary>History</summary>
673<table>
674<tbody><tr><th>Version</th><th>Changes</th></tr>
675<tr><td>v13.1.0, v12.16.0</td>
676<td><p><span>Added in: v13.1.0, v12.16.0</span></p></td></tr>
677<tr><td>v13.6.0, v12.16.0</td>
678<td><p>Unflag self-referencing a package using its name.</p></td></tr>
679</tbody></table>
680</details>
681</div>
682<p>Within a package, the values defined in the package’s
683<code>package.json</code> <a href="#packages_exports"><code>"exports"</code></a> field can be referenced via the package’s name.
684For example, assuming the <code>package.json</code> is:</p>
685<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
686<span class="hljs-punctuation">{</span>
687  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"a-package"</span><span class="hljs-punctuation">,</span>
688  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
689    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.mjs"</span><span class="hljs-punctuation">,</span>
690    <span class="hljs-attr">"./foo"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./foo.js"</span>
691  <span class="hljs-punctuation">}</span>
692<span class="hljs-punctuation">}</span></code></pre>
693<p>Then any module <em>in that package</em> can reference an export in the package itself:</p>
694<pre><code class="language-js"><span class="hljs-comment">// ./a-module.mjs</span>
695<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 ./main.mjs.</span></code></pre>
696<p>Self-referencing is available only if <code>package.json</code> has <a href="#packages_exports"><code>"exports"</code></a>, and
697will allow importing only what that <a href="#packages_exports"><code>"exports"</code></a> (in the <code>package.json</code>)
698allows. So the code below, given the previous package, will generate a runtime
699error:</p>
700<pre><code class="language-js"><span class="hljs-comment">// ./another-module.mjs</span>
701
702<span class="hljs-comment">// Imports "another" from ./m.mjs. Fails because</span>
703<span class="hljs-comment">// the "package.json" "exports" field</span>
704<span class="hljs-comment">// does not provide an export named "./m.mjs".</span>
705<span class="hljs-keyword">import</span> { another } <span class="hljs-keyword">from</span> <span class="hljs-string">'a-package/m.mjs'</span>;</code></pre>
706<p>Self-referencing is also available when using <code>require</code>, both in an ES module,
707and in a CommonJS one. For example, this code will also work:</p>
708<pre><code class="language-js cjs"><span class="hljs-comment">// ./a-module.js</span>
709<span class="hljs-keyword">const</span> { something } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'a-package/foo'</span>); <span class="hljs-comment">// Loads from ./foo.js.</span></code></pre>
710<p>Finally, self-referencing also works with scoped packages. For example, this
711code will also work:</p>
712<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
713<span class="hljs-punctuation">{</span>
714  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"@my/package"</span><span class="hljs-punctuation">,</span>
715  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.js"</span>
716<span class="hljs-punctuation">}</span></code></pre>
717
718<pre><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><pre><code class="language-js cjs"><span class="hljs-comment">// ./index.js</span>
719<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-number">42</span>;</code></pre><code class="language-js cjs"><span class="hljs-comment">// ./other.js</span>
720<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></pre>
721<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node other.js</span>
72242</code></pre>
723</section><section><h3>Dual CommonJS/ES module packages<span><a class="mark" href="#packages_dual_commonjs_es_module_packages" id="packages_dual_commonjs_es_module_packages">#</a></span></h3>
724<p>Prior to the introduction of support for ES modules in Node.js, it was a common
725pattern for package authors to include both CommonJS and ES module JavaScript
726sources in their package, with <code>package.json</code> <a href="#packages_main"><code>"main"</code></a> specifying the
727CommonJS entry point and <code>package.json</code> <code>"module"</code> specifying the ES module
728entry point.
729This enabled Node.js to run the CommonJS entry point while build tools such as
730bundlers used the ES module entry point, since Node.js ignored (and still
731ignores) the top-level <code>"module"</code> field.</p>
732<p>Node.js can now run ES module entry points, and a package can contain both
733CommonJS and ES module entry points (either via separate specifiers such as
734<code>'pkg'</code> and <code>'pkg/es-module'</code>, or both at the same specifier via <a href="#packages_conditional_exports">Conditional
735exports</a>). Unlike in the scenario where <code>"module"</code> is only used by bundlers,
736or ES module files are transpiled into CommonJS on the fly before evaluation by
737Node.js, the files referenced by the ES module entry point are evaluated as ES
738modules.</p>
739<h4>Dual package hazard<span><a class="mark" href="#packages_dual_package_hazard" id="packages_dual_package_hazard">#</a></span></h4>
740<p>When an application is using a package that provides both CommonJS and ES module
741sources, there is a risk of certain bugs if both versions of the package get
742loaded. This potential comes from the fact that the <code>pkgInstance</code> created by
743<code>const pkgInstance = require('pkg')</code> is not the same as the <code>pkgInstance</code>
744created by <code>import pkgInstance from 'pkg'</code> (or an alternative main path like
745<code>'pkg/module'</code>). This is the “dual package hazard,” where two versions of the
746same package can be loaded within the same runtime environment. While it is
747unlikely that an application or package would intentionally load both versions
748directly, it is common for an application to load one version while a dependency
749of the application loads the other version. This hazard can happen because
750Node.js supports intermixing CommonJS and ES modules, and can lead to unexpected
751behavior.</p>
752<p>If the package main export is a constructor, an <code>instanceof</code> comparison of
753instances created by the two versions returns <code>false</code>, and if the export is an
754object, properties added to one (like <code>pkgInstance.foo = 3</code>) are not present on
755the other. This differs from how <code>import</code> and <code>require</code> statements work in
756all-CommonJS or all-ES module environments, respectively, and therefore is
757surprising to users. It also differs from the behavior users are familiar with
758when 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>
759<h4>Writing dual packages while avoiding or minimizing hazards<span><a class="mark" href="#packages_writing_dual_packages_while_avoiding_or_minimizing_hazards" id="packages_writing_dual_packages_while_avoiding_or_minimizing_hazards">#</a></span></h4>
760<p>First, the hazard described in the previous section occurs when a package
761contains both CommonJS and ES module sources and both sources are provided for
762use in Node.js, either via separate main entry points or exported paths. A
763package might instead be written where any version of Node.js receives only
764CommonJS sources, and any separate ES module sources the package might contain
765are intended only for other environments such as browsers. Such a package
766would be usable by any version of Node.js, since <code>import</code> can refer to CommonJS
767files; but it would not provide any of the advantages of using ES module syntax.</p>
768<p>A package might also switch from CommonJS to ES module syntax in a <a href="https://semver.org/">breaking
769change</a> version bump. This has the disadvantage that the
770newest version of the package would only be usable in ES module-supporting
771versions of Node.js.</p>
772<p>Every pattern has tradeoffs, but there are two broad approaches that satisfy the
773following conditions:</p>
774<ol>
775<li>The package is usable via both <code>require</code> and <code>import</code>.</li>
776<li>The package is usable in both current Node.js and older versions of Node.js
777that lack support for ES modules.</li>
778<li>The package main entry point, e.g. <code>'pkg'</code> can be used by both <code>require</code> to
779resolve to a CommonJS file and by <code>import</code> to resolve to an ES module file.
780(And likewise for exported paths, e.g. <code>'pkg/feature'</code>.)</li>
781<li>The package provides named exports, e.g. <code>import { name } from 'pkg'</code> rather
782than <code>import pkg from 'pkg'; pkg.name</code>.</li>
783<li>The package is potentially usable in other ES module environments such as
784browsers.</li>
785<li>The hazards described in the previous section are avoided or minimized.</li>
786</ol>
787<h5>Approach #1: Use an ES module wrapper<span><a class="mark" href="#packages_approach_1_use_an_es_module_wrapper" id="packages_approach_1_use_an_es_module_wrapper">#</a></span></h5>
788<p>Write the package in CommonJS or transpile ES module sources into CommonJS, and
789create an ES module wrapper file that defines the named exports. Using
790<a href="#packages_conditional_exports">Conditional exports</a>, the ES module wrapper is used for <code>import</code> and the
791CommonJS entry point for <code>require</code>.</p>
792<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</span>
793<span class="hljs-punctuation">{</span>
794  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span><span class="hljs-punctuation">,</span>
795  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span><span class="hljs-punctuation">,</span>
796  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
797    <span class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./wrapper.mjs"</span><span class="hljs-punctuation">,</span>
798    <span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span>
799  <span class="hljs-punctuation">}</span>
800<span class="hljs-punctuation">}</span></code></pre>
801<p>The preceding example uses explicit extensions <code>.mjs</code> and <code>.cjs</code>.
802If your files use the <code>.js</code> extension, <code>"type": "module"</code> will cause such files
803to be treated as ES modules, just as <code>"type": "commonjs"</code> would cause them
804to be treated as CommonJS.
805See <a href="esm.html#esm_enabling">Enabling</a>.</p>
806<pre><code class="language-js cjs"><span class="hljs-comment">// ./node_modules/pkg/index.cjs</span>
807<span class="hljs-built_in">exports</span>.<span class="hljs-property">name</span> = <span class="hljs-string">'value'</span>;</code></pre>
808<pre><code class="language-js"><span class="hljs-comment">// ./node_modules/pkg/wrapper.mjs</span>
809<span class="hljs-keyword">import</span> cjsModule <span class="hljs-keyword">from</span> <span class="hljs-string">'./index.cjs'</span>;
810<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> name = cjsModule.<span class="hljs-property">name</span>;</code></pre>
811<p>In this example, the <code>name</code> from <code>import { name } from 'pkg'</code> is the same
812singleton as the <code>name</code> from <code>const { name } = require('pkg')</code>. Therefore <code>===</code>
813returns <code>true</code> when comparing the two <code>name</code>s and the divergent specifier hazard
814is avoided.</p>
815<p>If the module is not simply a list of named exports, but rather contains a
816unique function or object export like <code>module.exports = function () { ... }</code>,
817or if support in the wrapper for the <code>import pkg from 'pkg'</code> pattern is desired,
818then the wrapper would instead be written to export the default optionally
819along with any named exports as well:</p>
820<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>;
821<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> name = cjsModule.<span class="hljs-property">name</span>;
822<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> cjsModule;</code></pre>
823<p>This approach is appropriate for any of the following use cases:</p>
824<ul>
825<li>The package is currently written in CommonJS and the author would prefer not
826to refactor it into ES module syntax, but wishes to provide named exports for
827ES module consumers.</li>
828<li>The package has other packages that depend on it, and the end user might
829install both this package and those other packages. For example a <code>utilities</code>
830package is used directly in an application, and a <code>utilities-plus</code> package
831adds a few more functions to <code>utilities</code>. Because the wrapper exports
832underlying CommonJS files, it doesn’t matter if <code>utilities-plus</code> is written in
833CommonJS or ES module syntax; it will work either way.</li>
834<li>The package stores internal state, and the package author would prefer not to
835refactor the package to isolate its state management. See the next section.</li>
836</ul>
837<p>A variant of this approach not requiring conditional exports for consumers could
838be to add an export, e.g. <code>"./module"</code>, to point to an all-ES module-syntax
839version of the package. This could be used via <code>import 'pkg/module'</code> by users
840who are certain that the CommonJS version will not be loaded anywhere in the
841application, such as by dependencies; or if the CommonJS version can be loaded
842but doesn’t affect the ES module version (for example, because the package is
843stateless):</p>
844<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</span>
845<span class="hljs-punctuation">{</span>
846  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span><span class="hljs-punctuation">,</span>
847  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span><span class="hljs-punctuation">,</span>
848  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
849    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span><span class="hljs-punctuation">,</span>
850    <span class="hljs-attr">"./module"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./wrapper.mjs"</span>
851  <span class="hljs-punctuation">}</span>
852<span class="hljs-punctuation">}</span></code></pre>
853<h5>Approach #2: Isolate state<span><a class="mark" href="#packages_approach_2_isolate_state" id="packages_approach_2_isolate_state">#</a></span></h5>
854<p>A <a href="#packages_node_js_package_json_field_definitions"><code>package.json</code></a> file can define the separate CommonJS and ES module entry
855points directly:</p>
856<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</span>
857<span class="hljs-punctuation">{</span>
858  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span><span class="hljs-punctuation">,</span>
859  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span><span class="hljs-punctuation">,</span>
860  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
861    <span class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.mjs"</span><span class="hljs-punctuation">,</span>
862    <span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span>
863  <span class="hljs-punctuation">}</span>
864<span class="hljs-punctuation">}</span></code></pre>
865<p>This can be done if both the CommonJS and ES module versions of the package are
866equivalent, for example because one is the transpiled output of the other; and
867the package’s management of state is carefully isolated (or the package is
868stateless).</p>
869<p>The reason that state is an issue is because both the CommonJS and ES module
870versions of the package might get used within an application; for example, the
871user’s application code could <code>import</code> the ES module version while a dependency
872<code>require</code>s the CommonJS version. If that were to occur, two copies of the
873package would be loaded in memory and therefore two separate states would be
874present. This would likely cause hard-to-troubleshoot bugs.</p>
875<p>Aside from writing a stateless package (if JavaScript’s <code>Math</code> were a package,
876for example, it would be stateless as all of its methods are static), there are
877some ways to isolate state so that it’s shared between the potentially loaded
878CommonJS and ES module instances of the package:</p>
879<ol>
880<li>
881<p>If possible, contain all state within an instantiated object. JavaScript’s
882<code>Date</code>, for example, needs to be instantiated to contain state; if it were a
883package, it would be used like this:</p>
884<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>;
885<span class="hljs-keyword">const</span> someDate = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>();
886<span class="hljs-comment">// someDate contains state; Date does not</span></code></pre>
887<p>The <code>new</code> keyword isn’t required; a package’s function can return a new
888object, or modify a passed-in object, to keep the state external to the
889package.</p>
890</li>
891<li>
892<p>Isolate the state in one or more CommonJS files that are shared between the
893CommonJS and ES module versions of the package. For example, if the CommonJS
894and ES module entry points are <code>index.cjs</code> and <code>index.mjs</code>, respectively:</p>
895<pre><code class="language-js cjs"><span class="hljs-comment">// ./node_modules/pkg/index.cjs</span>
896<span class="hljs-keyword">const</span> state = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./state.cjs'</span>);
897<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<span class="hljs-property">state</span> = state;</code></pre>
898<pre><code class="language-js"><span class="hljs-comment">// ./node_modules/pkg/index.mjs</span>
899<span class="hljs-keyword">import</span> state <span class="hljs-keyword">from</span> <span class="hljs-string">'./state.cjs'</span>;
900<span class="hljs-keyword">export</span> {
901  state
902};</code></pre>
903<p>Even if <code>pkg</code> is used via both <code>require</code> and <code>import</code> in an application (for
904example, via <code>import</code> in application code and via <code>require</code> by a dependency)
905each reference of <code>pkg</code> will contain the same state; and modifying that
906state from either module system will apply to both.</p>
907</li>
908</ol>
909<p>Any plugins that attach to the package’s singleton would need to separately
910attach to both the CommonJS and ES module singletons.</p>
911<p>This approach is appropriate for any of the following use cases:</p>
912<ul>
913<li>The package is currently written in ES module syntax and the package author
914wants that version to be used wherever such syntax is supported.</li>
915<li>The package is stateless or its state can be isolated without too much
916difficulty.</li>
917<li>The package is unlikely to have other public packages that depend on it, or if
918it does, the package is stateless or has state that need not be shared between
919dependencies or with the overall application.</li>
920</ul>
921<p>Even with isolated state, there is still the cost of possible extra code
922execution between the CommonJS and ES module versions of a package.</p>
923<p>As with the previous approach, a variant of this approach not requiring
924conditional exports for consumers could be to add an export, e.g.
925<code>"./module"</code>, to point to an all-ES module-syntax version of the package:</p>
926<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</span>
927<span class="hljs-punctuation">{</span>
928  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span><span class="hljs-punctuation">,</span>
929  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span><span class="hljs-punctuation">,</span>
930  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
931    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span><span class="hljs-punctuation">,</span>
932    <span class="hljs-attr">"./module"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.mjs"</span>
933  <span class="hljs-punctuation">}</span>
934<span class="hljs-punctuation">}</span></code></pre>
935</section><section><h3>Node.js <code>package.json</code> field definitions<span><a class="mark" href="#packages_node_js_package_json_field_definitions" id="packages_node_js_package_json_field_definitions">#</a></span></h3>
936<p>This section describes the fields used by the Node.js runtime. Other tools (such
937as <a href="https://docs.npmjs.com/creating-a-package-json-file">npm</a>) use
938additional fields which are ignored by Node.js and not documented here.</p>
939<p>The following fields in <code>package.json</code> files are used in Node.js:</p>
940<ul>
941<li><a href="#packages_name"><code>"name"</code></a> - Relevant when using named imports within a package. Also used
942by package managers as the name of the package.</li>
943<li><a href="#packages_main"><code>"main"</code></a> - The default module when loading the package, if exports is not
944specified, and in versions of Node.js prior to the introduction of exports.</li>
945<li><a href="#packages_packagemanager"><code>"packageManager"</code></a> - The package manager recommended when contributing to
946the package. Leveraged by the <a href="corepack.html">Corepack</a> shims.</li>
947<li><a href="#packages_type"><code>"type"</code></a> - The package type determining whether to load <code>.js</code> files as
948CommonJS or ES modules.</li>
949<li><a href="#packages_exports"><code>"exports"</code></a> - Package exports and conditional exports. When present,
950limits which submodules can be loaded from within the package.</li>
951<li><a href="#packages_imports"><code>"imports"</code></a> - Package imports, for use by modules within the package
952itself.</li>
953</ul>
954<h4><code>"name"</code><span><a class="mark" href="#packages_name" id="packages_name">#</a></span></h4>
955<div class="api_metadata">
956<details class="changelog"><summary>History</summary>
957<table>
958<tbody><tr><th>Version</th><th>Changes</th></tr>
959<tr><td>v13.1.0, v12.16.0</td>
960<td><p><span>Added in: v13.1.0, v12.16.0</span></p></td></tr>
961<tr><td>v13.6.0, v12.16.0</td>
962<td><p>Remove the <code>--experimental-resolve-self</code> option.</p></td></tr>
963</tbody></table>
964</details>
965</div>
966<ul>
967<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
968</ul>
969<pre><code class="language-json"><span class="hljs-punctuation">{</span>
970  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"package-name"</span>
971<span class="hljs-punctuation">}</span></code></pre>
972<p>The <code>"name"</code> field defines your package’s name. Publishing to the
973<em>npm</em> registry requires a name that satisfies
974<a href="https://docs.npmjs.com/files/package.json#name">certain requirements</a>.</p>
975<p>The <code>"name"</code> field can be used in addition to the <a href="#packages_exports"><code>"exports"</code></a> field to
976<a href="#packages_self_referencing_a_package_using_its_name">self-reference</a> a package using its name.</p>
977<h4><code>"main"</code><span><a class="mark" href="#packages_main" id="packages_main">#</a></span></h4>
978<div class="api_metadata">
979<span>Added in: v0.4.0</span>
980</div>
981<ul>
982<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
983</ul>
984<pre><code class="language-json"><span class="hljs-punctuation">{</span>
985  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span>
986<span class="hljs-punctuation">}</span></code></pre>
987<p>The <code>"main"</code> field defines the script that is used when the <a href="modules.html#modules_folders_as_modules">package directory
988is loaded via <code>require()</code></a>. Its value
989is a path.</p>
990<pre><code class="language-js cjs"><span class="hljs-built_in">require</span>(<span class="hljs-string">'./path/to/directory'</span>); <span class="hljs-comment">// This resolves to ./path/to/directory/main.js.</span></code></pre>
991<p>When a package has an <a href="#packages_exports"><code>"exports"</code></a> field, this will take precedence over the
992<code>"main"</code> field when importing the package by name.</p>
993<h4><code>"packageManager"</code><span><a class="mark" href="#packages_packagemanager" id="packages_packagemanager">#</a></span></h4>
994<div class="api_metadata">
995<span>Added in: v14.19.0</span>
996</div>
997<p></p><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental</div><p></p>
998<ul>
999<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1000</ul>
1001<pre><code class="language-json"><span class="hljs-punctuation">{</span>
1002  <span class="hljs-attr">"packageManager"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"&#x3C;package manager name>@&#x3C;version>"</span>
1003<span class="hljs-punctuation">}</span></code></pre>
1004<p>The <code>"packageManager"</code> field defines which package manager is expected to be
1005used when working on the current project. It can set to any of the
1006<a href="corepack.html#corepack_supported_package_managers">supported package managers</a>, and will ensure that your teams use the exact
1007same package manager versions without having to install anything else than
1008Node.js.</p>
1009<p>This field is currently experimental and needs to be opted-in; check the
1010<a href="corepack.html">Corepack</a> page for details about the procedure.</p>
1011<h4><code>"type"</code><span><a class="mark" href="#packages_type" id="packages_type">#</a></span></h4>
1012<div class="api_metadata">
1013<details class="changelog"><summary>History</summary>
1014<table>
1015<tbody><tr><th>Version</th><th>Changes</th></tr>
1016<tr><td>v13.2.0, v12.17.0</td>
1017<td><p>Unflag <code>--experimental-modules</code>.</p></td></tr>
1018<tr><td>v12.0.0</td>
1019<td><p><span>Added in: v12.0.0</span></p></td></tr>
1020</tbody></table>
1021</details>
1022</div>
1023<ul>
1024<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1025</ul>
1026<p>The <code>"type"</code> field defines the module format that Node.js uses for all
1027<code>.js</code> files that have that <code>package.json</code> file as their nearest parent.</p>
1028<p>Files ending with <code>.js</code> are loaded as ES modules when the nearest parent
1029<code>package.json</code> file contains a top-level field <code>"type"</code> with a value of
1030<code>"module"</code>.</p>
1031<p>The nearest parent <code>package.json</code> is defined as the first <code>package.json</code> found
1032when searching in the current folder, that folder’s parent, and so on up
1033until a node_modules folder or the volume root is reached.</p>
1034<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
1035<span class="hljs-punctuation">{</span>
1036  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span>
1037<span class="hljs-punctuation">}</span></code></pre>
1038<pre><code class="language-bash"><span class="hljs-comment"># In same folder as preceding package.json</span>
1039node my-app.js <span class="hljs-comment"># Runs as ES module</span></code></pre>
1040<p>If the nearest parent <code>package.json</code> lacks a <code>"type"</code> field, or contains
1041<code>"type": "commonjs"</code>, <code>.js</code> files are treated as <a href="modules.html">CommonJS</a>. If the volume
1042root is reached and no <code>package.json</code> is found, <code>.js</code> files are treated as
1043<a href="modules.html">CommonJS</a>.</p>
1044<p><code>import</code> statements of <code>.js</code> files are treated as ES modules if the nearest
1045parent <code>package.json</code> contains <code>"type": "module"</code>.</p>
1046<pre><code class="language-js"><span class="hljs-comment">// my-app.js, part of the same example as above</span>
1047<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></pre>
1048<p>Regardless of the value of the <code>"type"</code> field, <code>.mjs</code> files are always treated
1049as ES modules and <code>.cjs</code> files are always treated as CommonJS.</p>
1050<h4><code>"exports"</code><span><a class="mark" href="#packages_exports" id="packages_exports">#</a></span></h4>
1051<div class="api_metadata">
1052<details class="changelog"><summary>History</summary>
1053<table>
1054<tbody><tr><th>Version</th><th>Changes</th></tr>
1055<tr><td>v14.13.0, v12.20.0</td>
1056<td><p>Add support for <code>"exports"</code> patterns.</p></td></tr>
1057<tr><td>v13.7.0, v12.16.0</td>
1058<td><p>Implement logical conditional exports ordering.</p></td></tr>
1059<tr><td>v13.7.0, v12.16.0</td>
1060<td><p>Remove the <code>--experimental-conditional-exports</code> option.</p></td></tr>
1061<tr><td>v13.2.0, v12.16.0</td>
1062<td><p>Implement conditional exports.</p></td></tr>
1063<tr><td>v12.7.0</td>
1064<td><p><span>Added in: v12.7.0</span></p></td></tr>
1065</tbody></table>
1066</details>
1067</div>
1068<ul>
1069<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>
1070</ul>
1071<pre><code class="language-json"><span class="hljs-punctuation">{</span>
1072  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.js"</span>
1073<span class="hljs-punctuation">}</span></code></pre>
1074<p>The <code>"exports"</code> field allows defining the <a href="#packages_package_entry_points">entry points</a> of a package when
1075imported by name loaded either via a <code>node_modules</code> lookup or a
1076<a href="#packages_self_referencing_a_package_using_its_name">self-reference</a> to its own name. It is supported in Node.js 12+ as an
1077alternative to the <a href="#packages_main"><code>"main"</code></a> that can support defining <a href="#packages_subpath_exports">subpath exports</a>
1078and <a href="#packages_conditional_exports">conditional exports</a> while encapsulating internal unexported modules.</p>
1079<p><a href="#packages_conditional_exports">Conditional Exports</a> can also be used within <code>"exports"</code> to define different
1080package entry points per environment, including whether the package is
1081referenced via <code>require</code> or via <code>import</code>.</p>
1082<p>All paths defined in the <code>"exports"</code> must be relative file URLs starting with
1083<code>./</code>.</p>
1084<h4><code>"imports"</code><span><a class="mark" href="#packages_imports" id="packages_imports">#</a></span></h4>
1085<div class="api_metadata">
1086<span>Added in: v14.6.0, v12.19.0</span>
1087</div>
1088<ul>
1089<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
1090</ul>
1091<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
1092<span class="hljs-punctuation">{</span>
1093  <span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
1094    <span class="hljs-attr">"#dep"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
1095      <span class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"dep-node-native"</span><span class="hljs-punctuation">,</span>
1096      <span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./dep-polyfill.js"</span>
1097    <span class="hljs-punctuation">}</span>
1098  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
1099  <span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
1100    <span class="hljs-attr">"dep-node-native"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"^1.0.0"</span>
1101  <span class="hljs-punctuation">}</span>
1102<span class="hljs-punctuation">}</span></code></pre>
1103<p>Entries in the imports field must be strings starting with <code>#</code>.</p>
1104<p>Import maps permit mapping to external packages.</p>
1105<p>This field defines <a href="#packages_subpath_imports">subpath imports</a> for the current package.</p></section>
1106        <!-- API END -->
1107      </div>
1108    </div>
1109  </div>
1110  <script>
1111    'use strict';
1112    {
1113      const kCustomPreference = 'customDarkTheme';
1114      const userSettings = sessionStorage.getItem(kCustomPreference);
1115      const themeToggleButton = document.getElementById('theme-toggle-btn');
1116      if (userSettings === null && window.matchMedia) {
1117        const mq = window.matchMedia('(prefers-color-scheme: dark)');
1118        if ('onchange' in mq) {
1119          function mqChangeListener(e) {
1120            document.body.classList.toggle('dark-mode', e.matches);
1121          }
1122          mq.addEventListener('change', mqChangeListener);
1123          if (themeToggleButton) {
1124            themeToggleButton.addEventListener('click', function() {
1125              mq.removeEventListener('change', mqChangeListener);
1126            }, { once: true });
1127          }
1128        }
1129        if (mq.matches) {
1130          document.body.classList.add('dark-mode');
1131        }
1132      } else if (userSettings === 'true') {
1133        document.body.classList.add('dark-mode');
1134      }
1135      if (themeToggleButton) {
1136        themeToggleButton.hidden = false;
1137        themeToggleButton.addEventListener('click', function() {
1138          sessionStorage.setItem(
1139            kCustomPreference,
1140            document.body.classList.toggle('dark-mode')
1141          );
1142        });
1143      }
1144    }
1145  </script>
1146</body>
1147</html>
1148