%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /proc/self/root/proc/thread-self/root/usr/share/doc/nodejs/api/
Upload File :
Create Path :
Current File : //proc/self/root/proc/thread-self/root/usr/share/doc/nodejs/api/packages.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <meta name="nodejs.org:node-version" content="v16.16.0">
  <title>Modules: Packages | Node.js v16.16.0 Documentation</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
  <link rel="stylesheet" href="assets/style.css">
  <link rel="stylesheet" href="assets/hljs.css">
  <link rel="canonical" href="https://nodejs.org/api/packages.html">
</head>
<body class="alt apidoc" id="api-section-packages">
  <div id="content" class="clearfix">
    <div id="column2" class="interior">
      <div id="intro" class="interior">
        <a href="/" title="Go back to the home page">
          Node.js
        </a>
      </div>
      <ul>
<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs">File system</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
<li><a href="packages.html" class="nav-packages active">Modules: Packages</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
<li><a href="policy.html" class="nav-policy">Policies</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="report.html" class="nav-report">Report</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
<li><a href="url.html" class="nav-url">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
</ul>
    </div>

    <div id="column1" data-id="packages" class="interior">
      <header class="header">
        <div class="header-container">
          <h1>Node.js v16.16.0 documentation</h1>
          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
              <path fill="none" d="M0 0h24v24H0z" />
              <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"/>
              <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"/>
            </svg>
            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
              <path d="M0 0h24v24H0z" fill="none" />
              <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"/>
            </svg>
          </button>
        </div>
        <div id="gtoc">
          <ul>
            
    <li class="picker-header">
      <a href="#">
        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
        Table of contents
      </a>

      <div class="picker"><div class="toc"><ul>
<li><a href="#modules-packages">Modules: Packages</a>
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#determining-module-system">Determining module system</a>
<ul>
<li><a href="#modules-loaders">Modules loaders</a></li>
<li><a href="#packagejson-and-file-extensions"><code>package.json</code> and file extensions</a></li>
<li><a href="#--input-type-flag"><code>--input-type</code> flag</a></li>
</ul>
</li>
<li><span class="stability_1"><a href="#determining-package-manager">Determining package manager</a></span></li>
<li><a href="#package-entry-points">Package entry points</a>
<ul>
<li><a href="#main-entry-point-export">Main entry point export</a></li>
<li><a href="#subpath-exports">Subpath exports</a></li>
<li><a href="#subpath-imports">Subpath imports</a></li>
<li><a href="#subpath-patterns">Subpath patterns</a></li>
<li><span class="stability_0"><a href="#subpath-folder-mappings">Subpath folder mappings</a></span></li>
<li><a href="#exports-sugar">Exports sugar</a></li>
<li><a href="#conditional-exports">Conditional exports</a></li>
<li><a href="#nested-conditions">Nested conditions</a></li>
<li><a href="#resolving-user-conditions">Resolving user conditions</a></li>
<li><a href="#community-conditions-definitions">Community Conditions Definitions</a></li>
<li><a href="#self-referencing-a-package-using-its-name">Self-referencing a package using its name</a></li>
</ul>
</li>
<li><a href="#dual-commonjses-module-packages">Dual CommonJS/ES module packages</a>
<ul>
<li><a href="#dual-package-hazard">Dual package hazard</a></li>
<li><a href="#writing-dual-packages-while-avoiding-or-minimizing-hazards">Writing dual packages while avoiding or minimizing hazards</a>
<ul>
<li><a href="#approach-1-use-an-es-module-wrapper">Approach #1: Use an ES module wrapper</a></li>
<li><a href="#approach-2-isolate-state">Approach #2: Isolate state</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#nodejs-packagejson-field-definitions">Node.js <code>package.json</code> field definitions</a>
<ul>
<li><a href="#name"><code>"name"</code></a></li>
<li><a href="#main"><code>"main"</code></a></li>
<li><span class="stability_1"><a href="#packagemanager"><code>"packageManager"</code></a></span></li>
<li><a href="#type"><code>"type"</code></a></li>
<li><a href="#exports"><code>"exports"</code></a></li>
<li><a href="#imports"><code>"imports"</code></a></li>
</ul>
</li>
</ul>
</li>
</ul></div></div>
    </li>
  
            
    <li class="picker-header">
      <a href="#">
        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
        Index
      </a>

      <div class="picker"><ul>
<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>

      <li>
        <a href="index.html">Index</a>
      </li>
    </ul>
  
<hr class="line">
<ul>
<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs">File system</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
<li><a href="packages.html" class="nav-packages active">Modules: Packages</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
<li><a href="policy.html" class="nav-policy">Policies</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="report.html" class="nav-report">Report</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
<li><a href="url.html" class="nav-url">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
</ul></div>
    </li>
  
            
    <li class="picker-header">
      <a href="#">
        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
        Other versions
      </a>
      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v18.x/api/packages.html">18.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v17.x/api/packages.html">17.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v16.x/api/packages.html">16.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v15.x/api/packages.html">15.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v14.x/api/packages.html">14.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v13.x/api/packages.html">13.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v12.x/api/packages.html">12.x <b>LTS</b></a></li></ol></div>
    </li>
  
            <li class="picker-header">
              <a href="#">
                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
                Options
              </a>
        
              <div class="picker">
                <ul>
                  <li>
                    <a href="all.html">View on single page</a>
                  </li>
                  <li>
                    <a href="packages.json">View as JSON</a>
                  </li>
                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/packages.md">Edit on GitHub</a></li>    
                </ul>
              </div>
            </li>
          </ul>
        </div>
        <hr>
      </header>

      <details id="toc" open><summary>Table of contents</summary><ul>
<li><a href="#modules-packages">Modules: Packages</a>
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#determining-module-system">Determining module system</a>
<ul>
<li><a href="#modules-loaders">Modules loaders</a></li>
<li><a href="#packagejson-and-file-extensions"><code>package.json</code> and file extensions</a></li>
<li><a href="#--input-type-flag"><code>--input-type</code> flag</a></li>
</ul>
</li>
<li><span class="stability_1"><a href="#determining-package-manager">Determining package manager</a></span></li>
<li><a href="#package-entry-points">Package entry points</a>
<ul>
<li><a href="#main-entry-point-export">Main entry point export</a></li>
<li><a href="#subpath-exports">Subpath exports</a></li>
<li><a href="#subpath-imports">Subpath imports</a></li>
<li><a href="#subpath-patterns">Subpath patterns</a></li>
<li><span class="stability_0"><a href="#subpath-folder-mappings">Subpath folder mappings</a></span></li>
<li><a href="#exports-sugar">Exports sugar</a></li>
<li><a href="#conditional-exports">Conditional exports</a></li>
<li><a href="#nested-conditions">Nested conditions</a></li>
<li><a href="#resolving-user-conditions">Resolving user conditions</a></li>
<li><a href="#community-conditions-definitions">Community Conditions Definitions</a></li>
<li><a href="#self-referencing-a-package-using-its-name">Self-referencing a package using its name</a></li>
</ul>
</li>
<li><a href="#dual-commonjses-module-packages">Dual CommonJS/ES module packages</a>
<ul>
<li><a href="#dual-package-hazard">Dual package hazard</a></li>
<li><a href="#writing-dual-packages-while-avoiding-or-minimizing-hazards">Writing dual packages while avoiding or minimizing hazards</a>
<ul>
<li><a href="#approach-1-use-an-es-module-wrapper">Approach #1: Use an ES module wrapper</a></li>
<li><a href="#approach-2-isolate-state">Approach #2: Isolate state</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#nodejs-packagejson-field-definitions">Node.js <code>package.json</code> field definitions</a>
<ul>
<li><a href="#name"><code>"name"</code></a></li>
<li><a href="#main"><code>"main"</code></a></li>
<li><span class="stability_1"><a href="#packagemanager"><code>"packageManager"</code></a></span></li>
<li><a href="#type"><code>"type"</code></a></li>
<li><a href="#exports"><code>"exports"</code></a></li>
<li><a href="#imports"><code>"imports"</code></a></li>
</ul>
</li>
</ul>
</li>
</ul></details>

      <div id="apicontent">
        <h2>Modules: Packages<span><a class="mark" href="#modules-packages" id="modules-packages">#</a></span><a aria-hidden="true" class="legacy" id="packages_modules_packages"></a></h2>


<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v14.13.0, v12.20.0</td>
<td><p>Add support for <code>"exports"</code> patterns.</p></td></tr>
<tr><td>v14.6.0, v12.19.0</td>
<td><p>Add package <code>"imports"</code> field.</p></td></tr>
<tr><td>v13.7.0, v12.16.0</td>
<td><p>Unflag conditional exports.</p></td></tr>
<tr><td>v13.6.0, v12.16.0</td>
<td><p>Unflag self-referencing a package using its name.</p></td></tr>
<tr><td>v12.7.0</td>
<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>
<tr><td>v12.0.0</td>
<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>
</tbody></table>
</details>
</div>
<section><h3>Introduction<span><a class="mark" href="#introduction" id="introduction">#</a></span><a aria-hidden="true" class="legacy" id="packages_introduction"></a></h3>
<p>A package is a folder tree described by a <code>package.json</code> file. The package
consists of the folder containing the <code>package.json</code> file and all subfolders
until the next folder containing another <code>package.json</code> file, or a folder
named <code>node_modules</code>.</p>
<p>This page provides guidance for package authors writing <code>package.json</code> files
along with a reference for the <a href="#nodejs-packagejson-field-definitions"><code>package.json</code></a> fields defined by Node.js.</p>
</section><section><h3>Determining module system<span><a class="mark" href="#determining-module-system" id="determining-module-system">#</a></span><a aria-hidden="true" class="legacy" id="packages_determining_module_system"></a></h3>
<p>Node.js will treat the following as <a href="esm.html">ES modules</a> when passed to <code>node</code> as the
initial input, or when referenced by <code>import</code> statements or <code>import()</code>
expressions:</p>
<ul>
<li>
<p>Files with an <code>.mjs</code> extension.</p>
</li>
<li>
<p>Files with a <code>.js</code> extension when the nearest parent <code>package.json</code> file
contains a top-level <a href="#type"><code>"type"</code></a> field with a value of <code>"module"</code>.</p>
</li>
<li>
<p>Strings passed in as an argument to <code>--eval</code>, or piped to <code>node</code> via <code>STDIN</code>,
with the flag <code>--input-type=module</code>.</p>
</li>
</ul>
<p>Node.js will treat as <a href="modules.html">CommonJS</a> all other forms of input, such as <code>.js</code> files
where the nearest parent <code>package.json</code> file contains no top-level <code>"type"</code>
field, or string input without the flag <code>--input-type</code>. This behavior is to
preserve backward compatibility. However, now that Node.js supports both
CommonJS and ES modules, it is best to be explicit whenever possible. Node.js
will treat the following as CommonJS when passed to <code>node</code> as the initial input,
or when referenced by <code>import</code> statements, <code>import()</code> expressions, or
<code>require()</code> expressions:</p>
<ul>
<li>
<p>Files with a <code>.cjs</code> extension.</p>
</li>
<li>
<p>Files with a <code>.js</code> extension when the nearest parent <code>package.json</code> file
contains a top-level field <a href="#type"><code>"type"</code></a> with a value of <code>"commonjs"</code>.</p>
</li>
<li>
<p>Strings passed in as an argument to <code>--eval</code> or <code>--print</code>, or piped to <code>node</code>
via <code>STDIN</code>, with the flag <code>--input-type=commonjs</code>.</p>
</li>
</ul>
<p>Package authors should include the <a href="#type"><code>"type"</code></a> field, even in packages where
all sources are CommonJS. Being explicit about the <code>type</code> of the package will
future-proof the package in case the default type of Node.js ever changes, and
it will also make things easier for build tools and loaders to determine how the
files in the package should be interpreted.</p>
<h4>Modules loaders<span><a class="mark" href="#modules-loaders" id="modules-loaders">#</a></span><a aria-hidden="true" class="legacy" id="packages_modules_loaders"></a></h4>
<p>Node.js has two systems for resolving a specifier and loading modules.</p>
<p>There is the CommonJS module loader:</p>
<ul>
<li>It is fully synchronous.</li>
<li>It is responsible for handling <code>require()</code> calls.</li>
<li>It is monkey patchable.</li>
<li>It supports <a href="modules.html#folders-as-modules">folders as modules</a>.</li>
<li>When resolving a specifier, if no exact match is found, it will try to add
extensions (<code>.js</code>, <code>.json</code>, and finally <code>.node</code>) and then attempt to resolve
<a href="modules.html#folders-as-modules">folders as modules</a>.</li>
<li>It treats <code>.json</code> as JSON text files.</li>
<li><code>.node</code> files are interpreted as compiled addon modules loaded with
<code>process.dlopen()</code>.</li>
<li>It treats all files that lack <code>.json</code> or <code>.node</code> extensions as JavaScript
text files.</li>
<li>It cannot be used to load ECMAScript modules (although it is possible to
<a href="modules.html#the-mjs-extension">load ECMASCript modules from CommonJS modules</a>). When used to load a
JavaScript text file that is not an ECMAScript module, it loads it as a
CommonJS module.</li>
</ul>
<p>There is the ECMAScript module loader:</p>
<ul>
<li>It is asynchronous.</li>
<li>It is responsible for handling <code>import</code> statements and <code>import()</code> expressions.</li>
<li>It is not monkey patchable, can be customized using <a href="esm.html#loaders">loader hooks</a>.</li>
<li>It does not support folders as modules, directory indexes (e.g.
<code>'./startup/index.js'</code>) must be fully specified.</li>
<li>It does no extension searching. A file extension must be provided
when the specifier is a relative or absolute file URL.</li>
<li>It can load JSON modules, but an import assertion is required.</li>
<li>It accepts only <code>.js</code>, <code>.mjs</code>, and <code>.cjs</code> extensions for JavaScript text
files.</li>
<li>It can be used to load JavaScript CommonJS modules. Such modules
are passed through the <code>es-module-lexer</code> to try to identify named exports,
which are available if they can be determined through static analysis.
Imported CommonJS modules have their URLs converted to absolute
paths and are then loaded via the CommonJS module loader.</li>
</ul>
<h4><code>package.json</code> and file extensions<span><a class="mark" href="#packagejson-and-file-extensions" id="packagejson-and-file-extensions">#</a></span><a aria-hidden="true" class="legacy" id="packages_package_json_and_file_extensions"></a></h4>
<p>Within a package, the <a href="#nodejs-packagejson-field-definitions"><code>package.json</code></a> <a href="#type"><code>"type"</code></a> field defines how
Node.js should interpret <code>.js</code> files. If a <code>package.json</code> file does not have a
<code>"type"</code> field, <code>.js</code> files are treated as <a href="modules.html">CommonJS</a>.</p>
<p>A <code>package.json</code> <code>"type"</code> value of <code>"module"</code> tells Node.js to interpret <code>.js</code>
files within that package as using <a href="esm.html">ES module</a> syntax.</p>
<p>The <code>"type"</code> field applies not only to initial entry points (<code>node my-app.js</code>)
but also to files referenced by <code>import</code> statements and <code>import()</code> expressions.</p>
<pre><code class="language-js"><span class="hljs-comment">// my-app.js, treated as an ES module because there is a package.json</span>
<span class="hljs-comment">// file in the same folder with "type": "module".</span>

<span class="hljs-keyword">import</span> <span class="hljs-string">'./startup/init.js'</span>;
<span class="hljs-comment">// Loaded as ES module since ./startup contains no package.json file,</span>
<span class="hljs-comment">// and therefore inherits the "type" value from one level up.</span>

<span class="hljs-keyword">import</span> <span class="hljs-string">'commonjs-package'</span>;
<span class="hljs-comment">// Loaded as CommonJS since ./node_modules/commonjs-package/package.json</span>
<span class="hljs-comment">// lacks a "type" field or contains "type": "commonjs".</span>

<span class="hljs-keyword">import</span> <span class="hljs-string">'./node_modules/commonjs-package/index.js'</span>;
<span class="hljs-comment">// Loaded as CommonJS since ./node_modules/commonjs-package/package.json</span>
<span class="hljs-comment">// lacks a "type" field or contains "type": "commonjs".</span></code></pre>
<p>Files ending with <code>.mjs</code> are always loaded as <a href="esm.html">ES modules</a> regardless of
the nearest parent <code>package.json</code>.</p>
<p>Files ending with <code>.cjs</code> are always loaded as <a href="modules.html">CommonJS</a> regardless of the
nearest parent <code>package.json</code>.</p>
<pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'./legacy-file.cjs'</span>;
<span class="hljs-comment">// Loaded as CommonJS since .cjs is always loaded as CommonJS.</span>

<span class="hljs-keyword">import</span> <span class="hljs-string">'commonjs-package/src/index.mjs'</span>;
<span class="hljs-comment">// Loaded as ES module since .mjs is always loaded as ES module.</span></code></pre>
<p>The <code>.mjs</code> and <code>.cjs</code> extensions can be used to mix types within the same
package:</p>
<ul>
<li>
<p>Within a <code>"type": "module"</code> package, Node.js can be instructed to
interpret a particular file as <a href="modules.html">CommonJS</a> by naming it with a <code>.cjs</code>
extension (since both <code>.js</code> and <code>.mjs</code> files are treated as ES modules within
a <code>"module"</code> package).</p>
</li>
<li>
<p>Within a <code>"type": "commonjs"</code> package, Node.js can be instructed to
interpret a particular file as an <a href="esm.html">ES module</a> by naming it with an <code>.mjs</code>
extension (since both <code>.js</code> and <code>.cjs</code> files are treated as CommonJS within a
<code>"commonjs"</code> package).</p>
</li>
</ul>
<h4><code>--input-type</code> flag<span><a class="mark" href="#--input-type-flag" id="--input-type-flag">#</a></span><a aria-hidden="true" class="legacy" id="packages_input_type_flag"></a></h4>
<div class="api_metadata">
<span>Added in: v12.0.0</span>
</div>
<p>Strings passed in as an argument to <code>--eval</code> (or <code>-e</code>), or piped to <code>node</code> via
<code>STDIN</code>, are treated as <a href="esm.html">ES modules</a> when the <code>--input-type=module</code> flag
is set.</p>
<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>

<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>
<p>For completeness there is also <code>--input-type=commonjs</code>, for explicitly running
string input as CommonJS. This is the default behavior if <code>--input-type</code> is
unspecified.</p>
</section><section><h3>Determining package manager<span><a class="mark" href="#determining-package-manager" id="determining-package-manager">#</a></span><a aria-hidden="true" class="legacy" id="packages_determining_package_manager"></a></h3>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
<p>While all Node.js projects are expected to be installable by all package
managers once published, their development teams are often required to use one
specific package manager. To make this process easier, Node.js ships with a
tool called <a href="corepack.html">Corepack</a> that aims to make all package managers transparently
available in your environment - provided you have Node.js installed.</p>
<p>By default Corepack won't enforce any specific package manager and will use
the generic "Last Known Good" versions associated with each Node.js release,
but you can improve this experience by setting the <a href="#packagemanager"><code>"packageManager"</code></a> field
in your project's <code>package.json</code>.</p>
</section><section><h3>Package entry points<span><a class="mark" href="#package-entry-points" id="package-entry-points">#</a></span><a aria-hidden="true" class="legacy" id="packages_package_entry_points"></a></h3>
<p>In a package’s <code>package.json</code> file, two fields can define entry points for a
package: <a href="#main"><code>"main"</code></a> and <a href="#exports"><code>"exports"</code></a>. The <a href="#main"><code>"main"</code></a> field is supported
in all versions of Node.js, but its capabilities are limited: it only defines
the main entry point of the package.</p>
<p>The <a href="#exports"><code>"exports"</code></a> field provides an alternative to <a href="#main"><code>"main"</code></a> where the
package main entry point can be defined while also encapsulating the package,
<strong>preventing any other entry points besides those defined in <a href="#exports"><code>"exports"</code></a></strong>.
This encapsulation allows module authors to define a public interface for
their package.</p>
<p>If both <a href="#exports"><code>"exports"</code></a> and <a href="#main"><code>"main"</code></a> are defined, the <a href="#exports"><code>"exports"</code></a> field
takes precedence over <a href="#main"><code>"main"</code></a>. <a href="#exports"><code>"exports"</code></a> are not specific to ES
modules or CommonJS; <a href="#main"><code>"main"</code></a> is overridden by <a href="#exports"><code>"exports"</code></a> if it
exists. As such <a href="#main"><code>"main"</code></a> cannot be used as a fallback for CommonJS but it
can be used as a fallback for legacy versions of Node.js that do not support the
<a href="#exports"><code>"exports"</code></a> field.</p>
<p><a href="#conditional-exports">Conditional exports</a> can be used within <a href="#exports"><code>"exports"</code></a> to define different
package entry points per environment, including whether the package is
referenced via <code>require</code> or via <code>import</code>. For more information about supporting
both CommonJS and ES Modules in a single package please consult
<a href="#dual-commonjses-module-packages">the dual CommonJS/ES module packages section</a>.</p>
<p><strong>Warning</strong>: Introducing the <a href="#exports"><code>"exports"</code></a> field prevents consumers of a
package from using any entry points that are not defined, including the
<a href="#nodejs-packagejson-field-definitions"><code>package.json</code></a> (e.g. <code>require('your-package/package.json')</code>. <strong>This will
likely be a breaking change.</strong></p>
<p>To make the introduction of <a href="#exports"><code>"exports"</code></a> non-breaking, ensure that every
previously supported entry point is exported. It is best to explicitly specify
entry points so that the package’s public API is well-defined. For example,
a project that previous exported <code>main</code>, <code>lib</code>,
<code>feature</code>, and the <code>package.json</code> could use the following <code>package.exports</code>:</p>
<pre><code class="language-json"><span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"my-mod"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/index.js"</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"./lib"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/index.js"</span><span class="hljs-punctuation">,</span>
    <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>
    <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>
    <span class="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature/index.js"</span><span class="hljs-punctuation">,</span>
    <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>
    <span class="hljs-attr">"./package.json"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./package.json"</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>Alternatively a project could choose to export entire folders:</p>
<pre><code class="language-json"><span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"my-mod"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/index.js"</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"./lib"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/index.js"</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"./lib/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/*.js"</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature/index.js"</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"./feature/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature/*.js"</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"./package.json"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./package.json"</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>As a last resort, package encapsulation can be disabled entirely by creating an
export for the root of the package <code>"./*": "./*"</code>. This exposes every file
in the package at the cost of disabling the encapsulation and potential tooling
benefits this provides. As the ES Module loader in Node.js enforces the use of
<a href="esm.html#mandatory-file-extensions">the full specifier path</a>, exporting the root rather than being explicit
about entry is less expressive than either of the prior examples. Not only
is encapsulation lost but module consumers are unable to
<code>import feature from 'my-mod/feature'</code> as they need to provide the full
path <code>import feature from 'my-mod/feature/index.js</code>.</p>
<h4>Main entry point export<span><a class="mark" href="#main-entry-point-export" id="main-entry-point-export">#</a></span><a aria-hidden="true" class="legacy" id="packages_main_entry_point_export"></a></h4>
<p>To set the main entry point for a package, it is advisable to define both
<a href="#exports"><code>"exports"</code></a> and <a href="#main"><code>"main"</code></a> in the package’s <a href="#nodejs-packagejson-field-definitions"><code>package.json</code></a> file:</p>
<pre><code class="language-json"><span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>When the <a href="#exports"><code>"exports"</code></a> field is defined, all subpaths of the package are
encapsulated and no longer available to importers. For example,
<code>require('pkg/subpath.js')</code> throws an <a href="errors.html#err_package_path_not_exported"><code>ERR_PACKAGE_PATH_NOT_EXPORTED</code></a>
error.</p>
<p>This encapsulation of exports provides more reliable guarantees
about package interfaces for tools and when handling semver upgrades for a
package. It is not a strong encapsulation since a direct require of any
absolute subpath of the package such as
<code>require('/path/to/node_modules/pkg/subpath.js')</code> will still load <code>subpath.js</code>.</p>
<h4>Subpath exports<span><a class="mark" href="#subpath-exports" id="subpath-exports">#</a></span><a aria-hidden="true" class="legacy" id="packages_subpath_exports"></a></h4>
<div class="api_metadata">
<span>Added in: v12.7.0</span>
</div>
<p>When using the <a href="#exports"><code>"exports"</code></a> field, custom subpaths can be defined along
with the main entry point by treating the main entry point as the
<code>"."</code> subpath:</p>
<pre><code class="language-json"><span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"./submodule"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./src/submodule.js"</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>Now only the defined subpath in <a href="#exports"><code>"exports"</code></a> can be imported by a consumer:</p>
<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>;
<span class="hljs-comment">// Loads ./node_modules/es-module-package/src/submodule.js</span></code></pre>
<p>While other subpaths will error:</p>
<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>;
<span class="hljs-comment">// Throws ERR_PACKAGE_PATH_NOT_EXPORTED</span></code></pre>
<h4>Subpath imports<span><a class="mark" href="#subpath-imports" id="subpath-imports">#</a></span><a aria-hidden="true" class="legacy" id="packages_subpath_imports"></a></h4>
<div class="api_metadata">
<span>Added in: v14.6.0, v12.19.0</span>
</div>
<p>In addition to the <a href="#exports"><code>"exports"</code></a> field, it is possible to define internal
package import maps that only apply to import specifiers from within the package
itself.</p>
<p>Entries in the imports field must always start with <code>#</code> to ensure they are
disambiguated from package specifiers.</p>
<p>For example, the imports field can be used to gain the benefits of conditional
exports for internal modules:</p>
<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
<span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"#dep"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
      <span class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"dep-node-native"</span><span class="hljs-punctuation">,</span>
      <span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./dep-polyfill.js"</span>
    <span class="hljs-punctuation">}</span>
  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"dep-node-native"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"^1.0.0"</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>where <code>import '#dep'</code> does not get the resolution of the external package
<code>dep-node-native</code> (including its exports in turn), and instead gets the local
file <code>./dep-polyfill.js</code> relative to the package in other environments.</p>
<p>Unlike the <code>"exports"</code> field, the <code>"imports"</code> field permits mapping to external
packages.</p>
<p>The resolution rules for the imports field are otherwise
analogous to the exports field.</p>
<h4>Subpath patterns<span><a class="mark" href="#subpath-patterns" id="subpath-patterns">#</a></span><a aria-hidden="true" class="legacy" id="packages_subpath_patterns"></a></h4>
<div class="api_metadata">
<span>Added in: v14.13.0, v12.20.0</span>
</div>
<p>For packages with a small number of exports or imports, we recommend
explicitly listing each exports subpath entry. But for packages that have
large numbers of subpaths, this might cause <code>package.json</code> bloat and
maintenance issues.</p>
<p>For these use cases, subpath export patterns can be used instead:</p>
<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/es-module-package/package.json</span>
<span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"./features/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./src/features/*.js"</span>
  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"#internal/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./src/internal/*.js"</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<p><strong><code>*</code> maps expose nested subpaths as it is a string replacement syntax
only.</strong></p>
<p>All instances of <code>*</code> on the right hand side will then be replaced with this
value, including if it contains any <code>/</code> separators.</p>
<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>;
<span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/x.js</span>

<span class="hljs-keyword">import</span> featureY <span class="hljs-keyword">from</span> <span class="hljs-string">'es-module-package/features/y/y'</span>;
<span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/y/y.js</span>

<span class="hljs-keyword">import</span> internalZ <span class="hljs-keyword">from</span> <span class="hljs-string">'#internal/z'</span>;
<span class="hljs-comment">// Loads ./node_modules/es-module-package/src/internal/z.js</span></code></pre>
<p>This is a direct static replacement without any special handling for file
extensions. In the previous example, <code>pkg/features/x.json</code> would be resolved to
<code>./src/features/x.json.js</code> in the mapping.</p>
<p>The property of exports being statically enumerable is maintained with exports
patterns since the individual exports for a package can be determined by
treating the right hand side target pattern as a <code>**</code> glob against the list of
files within the package. Because <code>node_modules</code> paths are forbidden in exports
targets, this expansion is dependent on only the files of the package itself.</p>
<p>To exclude private subfolders from patterns, <code>null</code> targets can be used:</p>
<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/es-module-package/package.json</span>
<span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"./features/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./src/features/*.js"</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"./features/private-internal/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-keyword">null</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<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>;
<span class="hljs-comment">// Throws: ERR_PACKAGE_PATH_NOT_EXPORTED</span>

<span class="hljs-keyword">import</span> featureX <span class="hljs-keyword">from</span> <span class="hljs-string">'es-module-package/features/x'</span>;
<span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/x.js</span></code></pre>
<h4>Subpath folder mappings<span><a class="mark" href="#subpath-folder-mappings" id="subpath-folder-mappings">#</a></span><a aria-hidden="true" class="legacy" id="packages_subpath_folder_mappings"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v16.0.0</td>
<td><p>Runtime deprecation.</p></td></tr>
<tr><td>v15.1.0</td>
<td><p>Runtime deprecation for self-referencing imports.</p></td></tr>
<tr><td>v14.13.0, v12.20.0</td>
<td><p>Documentation-only deprecation.</p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use subpath patterns instead.</div><p></p>
<p>Before subpath patterns were supported, a trailing <code>"/"</code> suffix was used to
support folder mappings:</p>
<pre><code class="language-json"><span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"./features/"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./features/"</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<p><em>This feature will be removed in a future release.</em></p>
<p>Instead, use direct <a href="#subpath-patterns">subpath patterns</a>:</p>
<pre><code class="language-json"><span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"./features/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./features/*.js"</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>The benefit of patterns over folder exports is that packages can always be
imported by consumers without subpath file extensions being necessary.</p>
<h4>Exports sugar<span><a class="mark" href="#exports-sugar" id="exports-sugar">#</a></span><a aria-hidden="true" class="legacy" id="packages_exports_sugar"></a></h4>
<div class="api_metadata">
<span>Added in: v12.11.0</span>
</div>
<p>If the <code>"."</code> export is the only export, the <a href="#exports"><code>"exports"</code></a> field provides sugar
for this case being the direct <a href="#exports"><code>"exports"</code></a> field value.</p>
<p>If the <code>"."</code> export has a fallback array or string value, then the
<a href="#exports"><code>"exports"</code></a> field can be set to this value directly.</p>
<pre><code class="language-json"><span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>can be written:</p>
<pre><code class="language-json"><span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span>
<span class="hljs-punctuation">}</span></code></pre>
<h4>Conditional exports<span><a class="mark" href="#conditional-exports" id="conditional-exports">#</a></span><a aria-hidden="true" class="legacy" id="packages_conditional_exports"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v13.2.0, v12.16.0</td>
<td><p><span>Added in: v13.2.0, v12.16.0</span></p></td></tr>
<tr><td>v13.7.0, v12.16.0</td>
<td><p>Unflag conditional exports.</p></td></tr>
</tbody></table>
</details>
</div>
<p>Conditional exports provide a way to map to different paths depending on
certain conditions. They are supported for both CommonJS and ES module imports.</p>
<p>For example, a package that wants to provide different ES module exports for
<code>require()</code> and <code>import</code> can be written:</p>
<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
<span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main-require.cjs"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main-module.js"</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main-require.cjs"</span>
  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>Node.js implements the following conditions, listed in order from most
specific to least specific as conditions should be defined:</p>
<ul>
<li><code>"node-addons"</code> - similar to <code>"node"</code> and matches for any Node.js environment.
This condition can be used to provide an entry point which uses native C++
addons as opposed to an entry point which is more universal and doesn't rely
on native addons. This condition can be disabled via the
<a href="cli.html#--no-addons"><code>--no-addons</code> flag</a>.</li>
<li><code>"node"</code> - matches for any Node.js environment. Can be a CommonJS or ES
module file. <em>In most cases explicitly calling out the Node.js platform is
not necessary.</em></li>
<li><code>"import"</code> - matches when the package is loaded via <code>import</code> or
<code>import()</code>, or via any top-level import or resolve operation by the
ECMAScript module loader. Applies regardless of the module format of the
target file. <em>Always mutually exclusive with <code>"require"</code>.</em></li>
<li><code>"require"</code> - matches when the package is loaded via <code>require()</code>. The
referenced file should be loadable with <code>require()</code> although the condition
matches regardless of the module format of the target file. Expected
formats include CommonJS, JSON, and native addons but not ES modules as
<code>require()</code> doesn't support them. <em>Always mutually exclusive with
<code>"import"</code>.</em></li>
<li><code>"default"</code> - the generic fallback that always matches. Can be a CommonJS
or ES module file. <em>This condition should always come last.</em></li>
</ul>
<p>Within the <a href="#exports"><code>"exports"</code></a> object, key order is significant. During condition
matching, earlier entries have higher priority and take precedence over later
entries. <em>The general rule is that conditions should be from most specific to
least specific in object order</em>.</p>
<p>Using the <code>"import"</code> and <code>"require"</code> conditions can lead to some hazards,
which are further explained in <a href="#dual-commonjses-module-packages">the dual CommonJS/ES module packages section</a>.</p>
<p>The <code>"node-addons"</code> condition can be used to provide an entry point which
uses native C++ addons. However, this condition can be disabled via the
<a href="cli.html#--no-addons"><code>--no-addons</code> flag</a>. When using <code>"node-addons"</code>, it's recommended to treat
<code>"default"</code> as an enhancement that provides a more universal entry point, e.g.
using WebAssembly instead of a native addon.</p>
<p>Conditional exports can also be extended to exports subpaths, for example:</p>
<pre><code class="language-json"><span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
      <span class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature-node.js"</span><span class="hljs-punctuation">,</span>
      <span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature.js"</span>
    <span class="hljs-punctuation">}</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>Defines a package where <code>require('pkg/feature')</code> and <code>import 'pkg/feature'</code>
could provide different implementations between Node.js and other JS
environments.</p>
<p>When using environment branches, always include a <code>"default"</code> condition where
possible. Providing a <code>"default"</code> condition ensures that any unknown JS
environments are able to use this universal implementation, which helps avoid
these JS environments from having to pretend to be existing environments in
order to support packages with conditional exports. For this reason, using
<code>"node"</code> and <code>"default"</code> condition branches is usually preferable to using
<code>"node"</code> and <code>"browser"</code> condition branches.</p>
<h4>Nested conditions<span><a class="mark" href="#nested-conditions" id="nested-conditions">#</a></span><a aria-hidden="true" class="legacy" id="packages_nested_conditions"></a></h4>
<p>In addition to direct mappings, Node.js also supports nested condition objects.</p>
<p>For example, to define a package that only has dual mode entry points for
use in Node.js but not the browser:</p>
<pre><code class="language-json"><span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
      <span class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature-node.mjs"</span><span class="hljs-punctuation">,</span>
      <span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature-node.cjs"</span>
    <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./feature.mjs"</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>Conditions continue to be matched in order as with flat conditions. If
a nested condition does not have any mapping it will continue checking
the remaining conditions of the parent condition. In this way nested
conditions behave analogously to nested JavaScript <code>if</code> statements.</p>
<h4>Resolving user conditions<span><a class="mark" href="#resolving-user-conditions" id="resolving-user-conditions">#</a></span><a aria-hidden="true" class="legacy" id="packages_resolving_user_conditions"></a></h4>
<div class="api_metadata">
<span>Added in: v14.9.0, v12.19.0</span>
</div>
<p>When running Node.js, custom user conditions can be added with the
<code>--conditions</code> flag:</p>
<pre><code class="language-bash">node --conditions=development main.js</code></pre>
<p>which would then resolve the <code>"development"</code> condition in package imports and
exports, while resolving the existing <code>"node"</code>, <code>"node-addons"</code>, <code>"default"</code>,
<code>"import"</code>, and <code>"require"</code> conditions as appropriate.</p>
<p>Any number of custom conditions can be set with repeat flags.</p>
<h4>Community Conditions Definitions<span><a class="mark" href="#community-conditions-definitions" id="community-conditions-definitions">#</a></span><a aria-hidden="true" class="legacy" id="packages_community_conditions_definitions"></a></h4>
<p>Condition strings other than the <code>"import"</code>, <code>"require"</code>, <code>"node"</code>,
<code>"node-addons"</code> and <code>"default"</code> conditions
<a href="#conditional-exports">implemented in Node.js core</a> are ignored by default.</p>
<p>Other platforms may implement other conditions and user conditions can be
enabled in Node.js via the <a href="#resolving-user-conditions"><code>--conditions</code> / <code>-C</code> flag</a>.</p>
<p>Since custom package conditions require clear definitions to ensure correct
usage, a list of common known package conditions and their strict definitions
is provided below to assist with ecosystem coordination.</p>
<ul>
<li><code>"types"</code> - can be used by typing systems to resolve the typing file for
the given export. <em>This condition should always be included first.</em></li>
<li><code>"deno"</code> - indicates a variation for the Deno platform.</li>
<li><code>"browser"</code> - any web browser environment.</li>
<li><code>"development"</code> - can be used to define a development-only environment
entry point, for example to provide additional debugging context such as
better error messages when running in a development mode. <em>Must always be
mutually exclusive with <code>"production"</code>.</em></li>
<li><code>"production"</code> - can be used to define a production environment entry
point. <em>Must always be mutually exclusive with <code>"development"</code>.</em></li>
</ul>
<p>New conditions definitions may be added to this list by creating a pull request
to 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
a new condition definition here are that:</p>
<ul>
<li>The definition should be clear and unambiguous for all implementers.</li>
<li>The use case for why the condition is needed should be clearly justified.</li>
<li>There should exist sufficient existing implementation usage.</li>
<li>The condition name should not conflict with another condition definition or
condition in wide usage.</li>
<li>The listing of the condition definition should provide a coordination
benefit to the ecosystem that wouldn't otherwise be possible. For example,
this would not necessarily be the case for company-specific or
application-specific conditions.</li>
</ul>
<p>The above definitions may be moved to a dedicated conditions registry in due
course.</p>
<h4>Self-referencing a package using its name<span><a class="mark" href="#self-referencing-a-package-using-its-name" id="self-referencing-a-package-using-its-name">#</a></span><a aria-hidden="true" class="legacy" id="packages_self_referencing_a_package_using_its_name"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v13.1.0, v12.16.0</td>
<td><p><span>Added in: v13.1.0, v12.16.0</span></p></td></tr>
<tr><td>v13.6.0, v12.16.0</td>
<td><p>Unflag self-referencing a package using its name.</p></td></tr>
</tbody></table>
</details>
</div>
<p>Within a package, the values defined in the package’s
<code>package.json</code> <a href="#exports"><code>"exports"</code></a> field can be referenced via the package’s name.
For example, assuming the <code>package.json</code> is:</p>
<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
<span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"a-package"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.mjs"</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"./foo"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./foo.js"</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>Then any module <em>in that package</em> can reference an export in the package itself:</p>
<pre><code class="language-js"><span class="hljs-comment">// ./a-module.mjs</span>
<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>
<p>Self-referencing is available only if <code>package.json</code> has <a href="#exports"><code>"exports"</code></a>, and
will allow importing only what that <a href="#exports"><code>"exports"</code></a> (in the <code>package.json</code>)
allows. So the code below, given the previous package, will generate a runtime
error:</p>
<pre><code class="language-js"><span class="hljs-comment">// ./another-module.mjs</span>

<span class="hljs-comment">// Imports "another" from ./m.mjs. Fails because</span>
<span class="hljs-comment">// the "package.json" "exports" field</span>
<span class="hljs-comment">// does not provide an export named "./m.mjs".</span>
<span class="hljs-keyword">import</span> { another } <span class="hljs-keyword">from</span> <span class="hljs-string">'a-package/m.mjs'</span>;</code></pre>
<p>Self-referencing is also available when using <code>require</code>, both in an ES module,
and in a CommonJS one. For example, this code will also work:</p>
<pre><code class="language-js cjs"><span class="hljs-comment">// ./a-module.js</span>
<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>
<p>Finally, self-referencing also works with scoped packages. For example, this
code will also work:</p>
<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
<span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"@my/package"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.js"</span>
<span class="hljs-punctuation">}</span></code></pre>
<pre><code class="language-js cjs"><span class="hljs-comment">// ./index.js</span>
<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-number">42</span>;</code></pre>
<pre><code class="language-js cjs"><span class="hljs-comment">// ./other.js</span>
<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>
<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node other.js</span>
42</code></pre>
</section><section><h3>Dual CommonJS/ES module packages<span><a class="mark" href="#dual-commonjses-module-packages" id="dual-commonjses-module-packages">#</a></span><a aria-hidden="true" class="legacy" id="packages_dual_commonjs_es_module_packages"></a></h3>
<p>Prior to the introduction of support for ES modules in Node.js, it was a common
pattern for package authors to include both CommonJS and ES module JavaScript
sources in their package, with <code>package.json</code> <a href="#main"><code>"main"</code></a> specifying the
CommonJS entry point and <code>package.json</code> <code>"module"</code> specifying the ES module
entry point.
This enabled Node.js to run the CommonJS entry point while build tools such as
bundlers used the ES module entry point, since Node.js ignored (and still
ignores) the top-level <code>"module"</code> field.</p>
<p>Node.js can now run ES module entry points, and a package can contain both
CommonJS and ES module entry points (either via separate specifiers such as
<code>'pkg'</code> and <code>'pkg/es-module'</code>, or both at the same specifier via <a href="#conditional-exports">Conditional
exports</a>). Unlike in the scenario where <code>"module"</code> is only used by bundlers,
or ES module files are transpiled into CommonJS on the fly before evaluation by
Node.js, the files referenced by the ES module entry point are evaluated as ES
modules.</p>
<h4>Dual package hazard<span><a class="mark" href="#dual-package-hazard" id="dual-package-hazard">#</a></span><a aria-hidden="true" class="legacy" id="packages_dual_package_hazard"></a></h4>
<p>When an application is using a package that provides both CommonJS and ES module
sources, there is a risk of certain bugs if both versions of the package get
loaded. This potential comes from the fact that the <code>pkgInstance</code> created by
<code>const pkgInstance = require('pkg')</code> is not the same as the <code>pkgInstance</code>
created by <code>import pkgInstance from 'pkg'</code> (or an alternative main path like
<code>'pkg/module'</code>). This is the “dual package hazard,” where two versions of the
same package can be loaded within the same runtime environment. While it is
unlikely that an application or package would intentionally load both versions
directly, it is common for an application to load one version while a dependency
of the application loads the other version. This hazard can happen because
Node.js supports intermixing CommonJS and ES modules, and can lead to unexpected
behavior.</p>
<p>If the package main export is a constructor, an <code>instanceof</code> comparison of
instances created by the two versions returns <code>false</code>, and if the export is an
object, properties added to one (like <code>pkgInstance.foo = 3</code>) are not present on
the other. This differs from how <code>import</code> and <code>require</code> statements work in
all-CommonJS or all-ES module environments, respectively, and therefore is
surprising to users. It also differs from the behavior users are familiar with
when 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>
<h4>Writing dual packages while avoiding or minimizing hazards<span><a class="mark" href="#writing-dual-packages-while-avoiding-or-minimizing-hazards" id="writing-dual-packages-while-avoiding-or-minimizing-hazards">#</a></span><a aria-hidden="true" class="legacy" id="packages_writing_dual_packages_while_avoiding_or_minimizing_hazards"></a></h4>
<p>First, the hazard described in the previous section occurs when a package
contains both CommonJS and ES module sources and both sources are provided for
use in Node.js, either via separate main entry points or exported paths. A
package might instead be written where any version of Node.js receives only
CommonJS sources, and any separate ES module sources the package might contain
are intended only for other environments such as browsers. Such a package
would be usable by any version of Node.js, since <code>import</code> can refer to CommonJS
files; but it would not provide any of the advantages of using ES module syntax.</p>
<p>A package might also switch from CommonJS to ES module syntax in a <a href="https://semver.org/">breaking
change</a> version bump. This has the disadvantage that the
newest version of the package would only be usable in ES module-supporting
versions of Node.js.</p>
<p>Every pattern has tradeoffs, but there are two broad approaches that satisfy the
following conditions:</p>
<ol>
<li>The package is usable via both <code>require</code> and <code>import</code>.</li>
<li>The package is usable in both current Node.js and older versions of Node.js
that lack support for ES modules.</li>
<li>The package main entry point, e.g. <code>'pkg'</code> can be used by both <code>require</code> to
resolve to a CommonJS file and by <code>import</code> to resolve to an ES module file.
(And likewise for exported paths, e.g. <code>'pkg/feature'</code>.)</li>
<li>The package provides named exports, e.g. <code>import { name } from 'pkg'</code> rather
than <code>import pkg from 'pkg'; pkg.name</code>.</li>
<li>The package is potentially usable in other ES module environments such as
browsers.</li>
<li>The hazards described in the previous section are avoided or minimized.</li>
</ol>
<h5>Approach #1: Use an ES module wrapper<span><a class="mark" href="#approach-1-use-an-es-module-wrapper" id="approach-1-use-an-es-module-wrapper">#</a></span><a aria-hidden="true" class="legacy" id="packages_approach_1_use_an_es_module_wrapper"></a></h5>
<p>Write the package in CommonJS or transpile ES module sources into CommonJS, and
create an ES module wrapper file that defines the named exports. Using
<a href="#conditional-exports">Conditional exports</a>, the ES module wrapper is used for <code>import</code> and the
CommonJS entry point for <code>require</code>.</p>
<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</span>
<span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./wrapper.mjs"</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>The preceding example uses explicit extensions <code>.mjs</code> and <code>.cjs</code>.
If your files use the <code>.js</code> extension, <code>"type": "module"</code> will cause such files
to be treated as ES modules, just as <code>"type": "commonjs"</code> would cause them
to be treated as CommonJS.
See <a href="esm.html#enabling">Enabling</a>.</p>
<pre><code class="language-js cjs"><span class="hljs-comment">// ./node_modules/pkg/index.cjs</span>
<span class="hljs-built_in">exports</span>.<span class="hljs-property">name</span> = <span class="hljs-string">'value'</span>;</code></pre>
<pre><code class="language-js"><span class="hljs-comment">// ./node_modules/pkg/wrapper.mjs</span>
<span class="hljs-keyword">import</span> cjsModule <span class="hljs-keyword">from</span> <span class="hljs-string">'./index.cjs'</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> name = cjsModule.<span class="hljs-property">name</span>;</code></pre>
<p>In this example, the <code>name</code> from <code>import { name } from 'pkg'</code> is the same
singleton as the <code>name</code> from <code>const { name } = require('pkg')</code>. Therefore <code>===</code>
returns <code>true</code> when comparing the two <code>name</code>s and the divergent specifier hazard
is avoided.</p>
<p>If the module is not simply a list of named exports, but rather contains a
unique function or object export like <code>module.exports = function () { ... }</code>,
or if support in the wrapper for the <code>import pkg from 'pkg'</code> pattern is desired,
then the wrapper would instead be written to export the default optionally
along with any named exports as well:</p>
<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>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> name = cjsModule.<span class="hljs-property">name</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> cjsModule;</code></pre>
<p>This approach is appropriate for any of the following use cases:</p>
<ul>
<li>The package is currently written in CommonJS and the author would prefer not
to refactor it into ES module syntax, but wishes to provide named exports for
ES module consumers.</li>
<li>The package has other packages that depend on it, and the end user might
install both this package and those other packages. For example a <code>utilities</code>
package is used directly in an application, and a <code>utilities-plus</code> package
adds a few more functions to <code>utilities</code>. Because the wrapper exports
underlying CommonJS files, it doesn’t matter if <code>utilities-plus</code> is written in
CommonJS or ES module syntax; it will work either way.</li>
<li>The package stores internal state, and the package author would prefer not to
refactor the package to isolate its state management. See the next section.</li>
</ul>
<p>A variant of this approach not requiring conditional exports for consumers could
be to add an export, e.g. <code>"./module"</code>, to point to an all-ES module-syntax
version of the package. This could be used via <code>import 'pkg/module'</code> by users
who are certain that the CommonJS version will not be loaded anywhere in the
application, such as by dependencies; or if the CommonJS version can be loaded
but doesn’t affect the ES module version (for example, because the package is
stateless):</p>
<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</span>
<span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"./module"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./wrapper.mjs"</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<h5>Approach #2: Isolate state<span><a class="mark" href="#approach-2-isolate-state" id="approach-2-isolate-state">#</a></span><a aria-hidden="true" class="legacy" id="packages_approach_2_isolate_state"></a></h5>
<p>A <a href="#nodejs-packagejson-field-definitions"><code>package.json</code></a> file can define the separate CommonJS and ES module entry
points directly:</p>
<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</span>
<span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.mjs"</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>This can be done if both the CommonJS and ES module versions of the package are
equivalent, for example because one is the transpiled output of the other; and
the package’s management of state is carefully isolated (or the package is
stateless).</p>
<p>The reason that state is an issue is because both the CommonJS and ES module
versions of the package might get used within an application; for example, the
user’s application code could <code>import</code> the ES module version while a dependency
<code>require</code>s the CommonJS version. If that were to occur, two copies of the
package would be loaded in memory and therefore two separate states would be
present. This would likely cause hard-to-troubleshoot bugs.</p>
<p>Aside from writing a stateless package (if JavaScript’s <code>Math</code> were a package,
for example, it would be stateless as all of its methods are static), there are
some ways to isolate state so that it’s shared between the potentially loaded
CommonJS and ES module instances of the package:</p>
<ol>
<li>
<p>If possible, contain all state within an instantiated object. JavaScript’s
<code>Date</code>, for example, needs to be instantiated to contain state; if it were a
package, it would be used like this:</p>
<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>;
<span class="hljs-keyword">const</span> someDate = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>();
<span class="hljs-comment">// someDate contains state; Date does not</span></code></pre>
<p>The <code>new</code> keyword isn’t required; a package’s function can return a new
object, or modify a passed-in object, to keep the state external to the
package.</p>
</li>
<li>
<p>Isolate the state in one or more CommonJS files that are shared between the
CommonJS and ES module versions of the package. For example, if the CommonJS
and ES module entry points are <code>index.cjs</code> and <code>index.mjs</code>, respectively:</p>
<pre><code class="language-js cjs"><span class="hljs-comment">// ./node_modules/pkg/index.cjs</span>
<span class="hljs-keyword">const</span> state = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./state.cjs'</span>);
<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<span class="hljs-property">state</span> = state;</code></pre>
<pre><code class="language-js"><span class="hljs-comment">// ./node_modules/pkg/index.mjs</span>
<span class="hljs-keyword">import</span> state <span class="hljs-keyword">from</span> <span class="hljs-string">'./state.cjs'</span>;
<span class="hljs-keyword">export</span> {
  state
};</code></pre>
<p>Even if <code>pkg</code> is used via both <code>require</code> and <code>import</code> in an application (for
example, via <code>import</code> in application code and via <code>require</code> by a dependency)
each reference of <code>pkg</code> will contain the same state; and modifying that
state from either module system will apply to both.</p>
</li>
</ol>
<p>Any plugins that attach to the package’s singleton would need to separately
attach to both the CommonJS and ES module singletons.</p>
<p>This approach is appropriate for any of the following use cases:</p>
<ul>
<li>The package is currently written in ES module syntax and the package author
wants that version to be used wherever such syntax is supported.</li>
<li>The package is stateless or its state can be isolated without too much
difficulty.</li>
<li>The package is unlikely to have other public packages that depend on it, or if
it does, the package is stateless or has state that need not be shared between
dependencies or with the overall application.</li>
</ul>
<p>Even with isolated state, there is still the cost of possible extra code
execution between the CommonJS and ES module versions of a package.</p>
<p>As with the previous approach, a variant of this approach not requiring
conditional exports for consumers could be to add an export, e.g.
<code>"./module"</code>, to point to an all-ES module-syntax version of the package:</p>
<pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</span>
<span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.cjs"</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">"./module"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.mjs"</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
</section><section><h3>Node.js <code>package.json</code> field definitions<span><a class="mark" href="#nodejs-packagejson-field-definitions" id="nodejs-packagejson-field-definitions">#</a></span><a aria-hidden="true" class="legacy" id="packages_node_js_package_json_field_definitions"></a></h3>
<p>This section describes the fields used by the Node.js runtime. Other tools (such
as <a href="https://docs.npmjs.com/cli/v8/configuring-npm/package-json">npm</a>) use
additional fields which are ignored by Node.js and not documented here.</p>
<p>The following fields in <code>package.json</code> files are used in Node.js:</p>
<ul>
<li><a href="#name"><code>"name"</code></a> - Relevant when using named imports within a package. Also used
by package managers as the name of the package.</li>
<li><a href="#main"><code>"main"</code></a> - The default module when loading the package, if exports is not
specified, and in versions of Node.js prior to the introduction of exports.</li>
<li><a href="#packagemanager"><code>"packageManager"</code></a> - The package manager recommended when contributing to
the package. Leveraged by the <a href="corepack.html">Corepack</a> shims.</li>
<li><a href="#type"><code>"type"</code></a> - The package type determining whether to load <code>.js</code> files as
CommonJS or ES modules.</li>
<li><a href="#exports"><code>"exports"</code></a> - Package exports and conditional exports. When present,
limits which submodules can be loaded from within the package.</li>
<li><a href="#imports"><code>"imports"</code></a> - Package imports, for use by modules within the package
itself.</li>
</ul>
<h4><code>"name"</code><span><a class="mark" href="#name" id="name">#</a></span><a aria-hidden="true" class="legacy" id="packages_name"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v13.1.0, v12.16.0</td>
<td><p><span>Added in: v13.1.0, v12.16.0</span></p></td></tr>
<tr><td>v13.6.0, v12.16.0</td>
<td><p>Remove the <code>--experimental-resolve-self</code> option.</p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
</ul>
<pre><code class="language-json"><span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"package-name"</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>The <code>"name"</code> field defines your package’s name. Publishing to the
<em>npm</em> registry requires a name that satisfies
<a href="https://docs.npmjs.com/files/package.json#name">certain requirements</a>.</p>
<p>The <code>"name"</code> field can be used in addition to the <a href="#exports"><code>"exports"</code></a> field to
<a href="#self-referencing-a-package-using-its-name">self-reference</a> a package using its name.</p>
<h4><code>"main"</code><span><a class="mark" href="#main" id="main">#</a></span><a aria-hidden="true" class="legacy" id="packages_main"></a></h4>
<div class="api_metadata">
<span>Added in: v0.4.0</span>
</div>
<ul>
<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
</ul>
<pre><code class="language-json"><span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./main.js"</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>The <code>"main"</code> field defines the entry point of a package when imported by name
via a <code>node_modules</code> lookup.  Its value is a path.</p>
<p>When a package has an <a href="#exports"><code>"exports"</code></a> field, this will take precedence over the
<code>"main"</code> field when importing the package by name.</p>
<p>It also defines the script that is used when the <a href="modules.html#folders-as-modules">package directory is loaded
via <code>require()</code></a>.</p>
<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>
<h4><code>"packageManager"</code><span><a class="mark" href="#packagemanager" id="packagemanager">#</a></span><a aria-hidden="true" class="legacy" id="packages_packagemanager"></a></h4>
<div class="api_metadata">
<span>Added in: v16.9.0</span>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
<ul>
<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
</ul>
<pre><code class="language-json"><span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"packageManager"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"&#x3C;package manager name>@&#x3C;version>"</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>The <code>"packageManager"</code> field defines which package manager is expected to be
used when working on the current project. It can set to any of the
<a href="corepack.html#supported-package-managers">supported package managers</a>, and will ensure that your teams use the exact
same package manager versions without having to install anything else than
Node.js.</p>
<p>This field is currently experimental and needs to be opted-in; check the
<a href="corepack.html">Corepack</a> page for details about the procedure.</p>
<h4><code>"type"</code><span><a class="mark" href="#type" id="type">#</a></span><a aria-hidden="true" class="legacy" id="packages_type"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v13.2.0, v12.17.0</td>
<td><p>Unflag <code>--experimental-modules</code>.</p></td></tr>
<tr><td>v12.0.0</td>
<td><p><span>Added in: v12.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
</ul>
<p>The <code>"type"</code> field defines the module format that Node.js uses for all
<code>.js</code> files that have that <code>package.json</code> file as their nearest parent.</p>
<p>Files ending with <code>.js</code> are loaded as ES modules when the nearest parent
<code>package.json</code> file contains a top-level field <code>"type"</code> with a value of
<code>"module"</code>.</p>
<p>The nearest parent <code>package.json</code> is defined as the first <code>package.json</code> found
when searching in the current folder, that folder’s parent, and so on up
until a node_modules folder or the volume root is reached.</p>
<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
<span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"module"</span>
<span class="hljs-punctuation">}</span></code></pre>
<pre><code class="language-bash"><span class="hljs-comment"># In same folder as preceding package.json</span>
node my-app.js <span class="hljs-comment"># Runs as ES module</span></code></pre>
<p>If the nearest parent <code>package.json</code> lacks a <code>"type"</code> field, or contains
<code>"type": "commonjs"</code>, <code>.js</code> files are treated as <a href="modules.html">CommonJS</a>. If the volume
root is reached and no <code>package.json</code> is found, <code>.js</code> files are treated as
<a href="modules.html">CommonJS</a>.</p>
<p><code>import</code> statements of <code>.js</code> files are treated as ES modules if the nearest
parent <code>package.json</code> contains <code>"type": "module"</code>.</p>
<pre><code class="language-js"><span class="hljs-comment">// my-app.js, part of the same example as above</span>
<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>
<p>Regardless of the value of the <code>"type"</code> field, <code>.mjs</code> files are always treated
as ES modules and <code>.cjs</code> files are always treated as CommonJS.</p>
<h4><code>"exports"</code><span><a class="mark" href="#exports" id="exports">#</a></span><a aria-hidden="true" class="legacy" id="packages_exports"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v14.13.0, v12.20.0</td>
<td><p>Add support for <code>"exports"</code> patterns.</p></td></tr>
<tr><td>v13.7.0, v12.16.0</td>
<td><p>Implement logical conditional exports ordering.</p></td></tr>
<tr><td>v13.7.0, v12.16.0</td>
<td><p>Remove the <code>--experimental-conditional-exports</code> option.</p></td></tr>
<tr><td>v13.2.0, v12.16.0</td>
<td><p>Implement conditional exports.</p></td></tr>
<tr><td>v12.7.0</td>
<td><p><span>Added in: v12.7.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<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>
</ul>
<pre><code class="language-json"><span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./index.js"</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>The <code>"exports"</code> field allows defining the <a href="#package-entry-points">entry points</a> of a package when
imported by name loaded either via a <code>node_modules</code> lookup or a
<a href="#self-referencing-a-package-using-its-name">self-reference</a> to its own name. It is supported in Node.js 12+ as an
alternative to the <a href="#main"><code>"main"</code></a> that can support defining <a href="#subpath-exports">subpath exports</a>
and <a href="#conditional-exports">conditional exports</a> while encapsulating internal unexported modules.</p>
<p><a href="#conditional-exports">Conditional Exports</a> can also be used within <code>"exports"</code> to define different
package entry points per environment, including whether the package is
referenced via <code>require</code> or via <code>import</code>.</p>
<p>All paths defined in the <code>"exports"</code> must be relative file URLs starting with
<code>./</code>.</p>
<h4><code>"imports"</code><span><a class="mark" href="#imports" id="imports">#</a></span><a aria-hidden="true" class="legacy" id="packages_imports"></a></h4>
<div class="api_metadata">
<span>Added in: v14.6.0, v12.19.0</span>
</div>
<ul>
<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
</ul>
<pre><code class="language-json"><span class="hljs-comment">// package.json</span>
<span class="hljs-punctuation">{</span>
  <span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"#dep"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
      <span class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"dep-node-native"</span><span class="hljs-punctuation">,</span>
      <span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./dep-polyfill.js"</span>
    <span class="hljs-punctuation">}</span>
  <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
  <span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
    <span class="hljs-attr">"dep-node-native"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"^1.0.0"</span>
  <span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span></code></pre>
<p>Entries in the imports field must be strings starting with <code>#</code>.</p>
<p>Import maps permit mapping to external packages.</p>
<p>This field defines <a href="#subpath-imports">subpath imports</a> for the current package.</p></section>
        <!-- API END -->
      </div>
    </div>
  </div>
  <script>
    'use strict';
    {
      const kCustomPreference = 'customDarkTheme';
      const userSettings = sessionStorage.getItem(kCustomPreference);
      const themeToggleButton = document.getElementById('theme-toggle-btn');
      if (userSettings === null && window.matchMedia) {
        const mq = window.matchMedia('(prefers-color-scheme: dark)');
        if ('onchange' in mq) {
          function mqChangeListener(e) {
            document.documentElement.classList.toggle('dark-mode', e.matches);
          }
          mq.addEventListener('change', mqChangeListener);
          if (themeToggleButton) {
            themeToggleButton.addEventListener('click', function() {
              mq.removeEventListener('change', mqChangeListener);
            }, { once: true });
          }
        }
        if (mq.matches) {
          document.documentElement.classList.add('dark-mode');
        }
      } else if (userSettings === 'true') {
        document.documentElement.classList.add('dark-mode');
      }
      if (themeToggleButton) {
        themeToggleButton.hidden = false;
        themeToggleButton.addEventListener('click', function() {
          sessionStorage.setItem(
            kCustomPreference,
            document.documentElement.classList.toggle('dark-mode')
          );
        });
      }
    }
  </script>
</body>
</html>

Zerion Mini Shell 1.0