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