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