%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/share/doc/nodejs/api/
Upload File :
Create Path :
Current File : //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="v18.19.1">
  <title>Modules: Packages | Node.js v18.19.1 Documentation</title>
  <link rel="stylesheet" href="assets/style.css">
  <link rel="stylesheet" href="assets/hljs.css">
  <script async defer src="assets/api.js" type="text/javascript"></script>
  __JS_FLAVORED_DYNAMIC_CSS__
</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="index.html" 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>node: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="permissions.html" class="nav-permissions">Permissions</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="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</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="test.html" class="nav-test">Test runner</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 v18.19.1 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="pinned-header">Node.js v18.19.1</li>
            
    <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="#introduction_1">Introduction</a></li>
<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><a href="#determining-package-manager">Determining package manager</a></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><ul>
<li><a href="#extensions-in-subpaths">Extensions in subpaths</a></li>
</ul>
</li>
<li><a href="#exports-sugar">Exports sugar</a></li>
<li><a href="#subpath-imports">Subpath imports</a></li>
<li><a href="#subpath-patterns">Subpath patterns</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>&quot;name&quot;</code></a></li>
<li><a href="#main"><code>&quot;main&quot;</code></a></li>
<li><a href="#packagemanager"><code>&quot;packageManager&quot;</code></a></li>
<li><a href="#type"><code>&quot;type&quot;</code></a></li>
<li><a href="#exports"><code>&quot;exports&quot;</code></a></li>
<li><a href="#imports"><code>&quot;imports&quot;</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>node: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="permissions.html" class="nav-permissions">Permissions</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="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</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="test.html" class="nav-test">Test runner</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 <b>LTS</b></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</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</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="#introduction_1">Introduction</a></li>
<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><a href="#determining-package-manager">Determining package manager</a></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><ul>
<li><a href="#extensions-in-subpaths">Extensions in subpaths</a></li>
</ul>
</li>
<li><a href="#exports-sugar">Exports sugar</a></li>
<li><a href="#subpath-imports">Subpath imports</a></li>
<li><a href="#subpath-patterns">Subpath patterns</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>&quot;name&quot;</code></a></li>
<li><a href="#main"><code>&quot;main&quot;</code></a></li>
<li><a href="#packagemanager"><code>&quot;packageManager&quot;</code></a></li>
<li><a href="#type"><code>&quot;type&quot;</code></a></li>
<li><a href="#exports"><code>&quot;exports&quot;</code></a></li>
<li><a href="#imports"><code>&quot;imports&quot;</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>
<!--introduced_in=v12.20.0-->

<!-- type=misc -->

<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tr><th>Version</th><th>Changes</th></tr>
<tr><td>v14.13.0, v12.20.0</td>
<td><p>Add support for <code>&quot;exports&quot;</code> patterns.</p>
</td></tr>
<tr><td>v14.6.0, v12.19.0</td>
<td><p>Add package <code>&quot;imports&quot;</code> field.</p>
</td></tr>
<tr><td>v13.7.0, v12.17.0</td>
<td><p>Unflag conditional exports.</p>
</td></tr>
<tr><td>v13.7.0, v12.16.0</td>
<td><p>Remove the <code>--experimental-conditional-exports</code> option. In 12.16.0, conditional exports are still behind <code>--experimental-modules</code>.</p>
</td></tr>
<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>&quot;exports&quot;</code> <code>package.json</code> field as a more powerful alternative to the classic <code>&quot;main&quot;</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>&quot;type&quot;</code> field.</p>
</td></tr>
</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>
<h4>Introduction<span><a class="mark" href="#introduction_1" id="introduction_1">#</a></span><a aria-hidden="true" class="legacy" id="packages_introduction_1"></a></h4>
<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>&quot;type&quot;</code></a> field with a value of <code>&quot;module&quot;</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 the following as <a href="modules.html">CommonJS</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 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>&quot;type&quot;</code></a> with a value of <code>&quot;commonjs&quot;</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>Aside from these explicit cases, there are other cases where Node.js defaults to
one module system or the other based on the value of the
<a href="cli.html#--experimental-default-typetype"><code>--experimental-default-type</code></a> flag:</p>
<ul>
<li><p>Files ending in <code>.js</code> or with no extension, if there is no <code>package.json</code> file
present in the same folder or any parent folder.</p>
</li>
<li><p>Files ending in <code>.js</code> or with no extension, if the nearest parent
<code>package.json</code> field lacks a <code>&quot;type&quot;</code> field; unless the folder is inside a
<code>node_modules</code> folder. (Package scopes under <code>node_modules</code> are always treated
as CommonJS when the <code>package.json</code> file lacks a <code>&quot;type&quot;</code> field, regardless
of <code>--experimental-default-type</code>, for backward compatibility.)</p>
</li>
<li><p>Strings passed in as an argument to <code>--eval</code> or piped to <code>node</code> via <code>STDIN</code>,
when <code>--input-type</code> is unspecified.</p>
</li>
</ul>
<p>This flag currently defaults to <code>&quot;commonjs&quot;</code>, but it may change in the future to
default to <code>&quot;module&quot;</code>. For this reason it is best to be explicit wherever
possible; in particular, package authors should always include the <a href="#type"><code>&quot;type&quot;</code></a>
field in their <code>package.json</code> files, 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>&#39;./startup/index.js&#39;</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>cjs-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>&quot;type&quot;</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>&quot;type&quot;</code> field, <code>.js</code> files are treated as <a href="modules.html">CommonJS</a>.</p>
<p>A <code>package.json</code> <code>&quot;type&quot;</code> value of <code>&quot;module&quot;</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>&quot;type&quot;</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">// my-app.js, treated as an ES module because there is a package.json
// file in the same folder with &quot;type&quot;: &quot;module&quot;.

import &#39;./startup/init.js&#39;;
// Loaded as ES module since ./startup contains no package.json file,
// and therefore inherits the &quot;type&quot; value from one level up.

import &#39;commonjs-package&#39;;
// Loaded as CommonJS since ./node_modules/commonjs-package/package.json
// lacks a &quot;type&quot; field or contains &quot;type&quot;: &quot;commonjs&quot;.

import &#39;./node_modules/commonjs-package/index.js&#39;;
// Loaded as CommonJS since ./node_modules/commonjs-package/package.json
// lacks a &quot;type&quot; field or contains &quot;type&quot;: &quot;commonjs&quot;.
</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">import &#39;./legacy-file.cjs&#39;;
// Loaded as CommonJS since .cjs is always loaded as CommonJS.

import &#39;commonjs-package/src/index.mjs&#39;;
// Loaded as ES module since .mjs is always loaded as ES module.
</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>&quot;type&quot;: &quot;module&quot;</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>&quot;module&quot;</code> package).</p>
</li>
<li><p>Within a <code>&quot;type&quot;: &quot;commonjs&quot;</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>&quot;commonjs&quot;</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 --eval &quot;import { sep } from &#39;node:path&#39;; console.log(sep);&quot;

echo &quot;import { sep } from &#39;node:path&#39;; console.log(sep);&quot; | 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>
<blockquote>
<p>Stability: 1 - Experimental</p>
</blockquote>
<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&#39;t enforce any specific package manager and will use
the generic &quot;Last Known Good&quot; versions associated with each Node.js release,
but you can improve this experience by setting the <a href="#packagemanager"><code>&quot;packageManager&quot;</code></a> field
in your project&#39;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&#39;s <code>package.json</code> file, two fields can define entry points for a
package: <a href="#main"><code>&quot;main&quot;</code></a> and <a href="#exports"><code>&quot;exports&quot;</code></a>. Both fields apply to both ES module
and CommonJS module entry points.</p>
<p>The <a href="#main"><code>&quot;main&quot;</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>&quot;exports&quot;</code></a> provides a modern alternative to <a href="#main"><code>&quot;main&quot;</code></a> allowing
multiple entry points to be defined, conditional entry resolution support
between environments, and <strong>preventing any other entry points besides those
defined in <a href="#exports"><code>&quot;exports&quot;</code></a></strong>. This encapsulation allows module authors to
clearly define the public interface for their package.</p>
<p>For new packages targeting the currently supported versions of Node.js, the
<a href="#exports"><code>&quot;exports&quot;</code></a> field is recommended. For packages supporting Node.js 10 and
below, the <a href="#main"><code>&quot;main&quot;</code></a> field is required. If both <a href="#exports"><code>&quot;exports&quot;</code></a> and
<a href="#main"><code>&quot;main&quot;</code></a> are defined, the <a href="#exports"><code>&quot;exports&quot;</code></a> field takes precedence over
<a href="#main"><code>&quot;main&quot;</code></a> in supported versions of Node.js.</p>
<p><a href="#conditional-exports">Conditional exports</a> can be used within <a href="#exports"><code>&quot;exports&quot;</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>Existing packages introducing the <a href="#exports"><code>&quot;exports&quot;</code></a> field will prevent consumers
of the 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(&#39;your-package/package.json&#39;)</code>. <strong>This will
likely be a breaking change.</strong></p>
<p>To make the introduction of <a href="#exports"><code>&quot;exports&quot;</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&#39;s public API is well-defined. For example,
a project that previously 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">{
  &quot;name&quot;: &quot;my-package&quot;,
  &quot;exports&quot;: {
    &quot;.&quot;: &quot;./lib/index.js&quot;,
    &quot;./lib&quot;: &quot;./lib/index.js&quot;,
    &quot;./lib/index&quot;: &quot;./lib/index.js&quot;,
    &quot;./lib/index.js&quot;: &quot;./lib/index.js&quot;,
    &quot;./feature&quot;: &quot;./feature/index.js&quot;,
    &quot;./feature/index&quot;: &quot;./feature/index.js&quot;,
    &quot;./feature/index.js&quot;: &quot;./feature/index.js&quot;,
    &quot;./package.json&quot;: &quot;./package.json&quot;
  }
}
</code></pre>
<p>Alternatively a project could choose to export entire folders both with and
without extensioned subpaths using export patterns:</p>
<pre><code class="language-json">{
  &quot;name&quot;: &quot;my-package&quot;,
  &quot;exports&quot;: {
    &quot;.&quot;: &quot;./lib/index.js&quot;,
    &quot;./lib&quot;: &quot;./lib/index.js&quot;,
    &quot;./lib/*&quot;: &quot;./lib/*.js&quot;,
    &quot;./lib/*.js&quot;: &quot;./lib/*.js&quot;,
    &quot;./feature&quot;: &quot;./feature/index.js&quot;,
    &quot;./feature/*&quot;: &quot;./feature/*.js&quot;,
    &quot;./feature/*.js&quot;: &quot;./feature/*.js&quot;,
    &quot;./package.json&quot;: &quot;./package.json&quot;
  }
}
</code></pre>
<p>With the above providing backwards-compatibility for any minor package versions,
a future major change for the package can then properly restrict the exports
to only the specific feature exports exposed:</p>
<pre><code class="language-json">{
  &quot;name&quot;: &quot;my-package&quot;,
  &quot;exports&quot;: {
    &quot;.&quot;: &quot;./lib/index.js&quot;,
    &quot;./feature/*.js&quot;: &quot;./feature/*.js&quot;,
    &quot;./feature/internal/*&quot;: null
  }
}
</code></pre>
<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>When writing a new package, it is recommended to use the <a href="#exports"><code>&quot;exports&quot;</code></a> field:</p>
<pre><code class="language-json">{
  &quot;exports&quot;: &quot;./index.js&quot;
}
</code></pre>
<p>When the <a href="#exports"><code>&quot;exports&quot;</code></a> field is defined, all subpaths of the package are
encapsulated and no longer available to importers. For example,
<code>require(&#39;pkg/subpath.js&#39;)</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(&#39;/path/to/node_modules/pkg/subpath.js&#39;)</code> will still load <code>subpath.js</code>.</p>
<p>All currently supported versions of Node.js and modern build tools support the
<code>&quot;exports&quot;</code> field. For projects using an older version of Node.js or a related
build tool, compatibility can be achieved by including the <code>&quot;main&quot;</code> field
alongside <code>&quot;exports&quot;</code> pointing to the same module:</p>
<pre><code class="language-json">{
  &quot;main&quot;: &quot;./index.js&quot;,
  &quot;exports&quot;: &quot;./index.js&quot;
}
</code></pre>
<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>&quot;exports&quot;</code></a> field, custom subpaths can be defined along
with the main entry point by treating the main entry point as the
<code>&quot;.&quot;</code> subpath:</p>
<pre><code class="language-json">{
  &quot;exports&quot;: {
    &quot;.&quot;: &quot;./index.js&quot;,
    &quot;./submodule.js&quot;: &quot;./src/submodule.js&quot;
  }
}
</code></pre>
<p>Now only the defined subpath in <a href="#exports"><code>&quot;exports&quot;</code></a> can be imported by a consumer:</p>
<pre><code class="language-js">import submodule from &#39;es-module-package/submodule.js&#39;;
// Loads ./node_modules/es-module-package/src/submodule.js
</code></pre>
<p>While other subpaths will error:</p>
<pre><code class="language-js">import submodule from &#39;es-module-package/private-module.js&#39;;
// Throws ERR_PACKAGE_PATH_NOT_EXPORTED
</code></pre>
<h5>Extensions in subpaths<span><a class="mark" href="#extensions-in-subpaths" id="extensions-in-subpaths">#</a></span><a aria-hidden="true" class="legacy" id="packages_extensions_in_subpaths"></a></h5>
<p>Package authors should provide either extensioned (<code>import &#39;pkg/subpath.js&#39;</code>) or
extensionless (<code>import &#39;pkg/subpath&#39;</code>) subpaths in their exports. This ensures
that there is only one subpath for each exported module so that all dependents
import the same consistent specifier, keeping the package contract clear for
consumers and simplifying package subpath completions.</p>
<p>Traditionally, packages tended to use the extensionless style, which has the
benefits of readability and of masking the true path of the file within the
package.</p>
<p>With <a href="https://github.com/WICG/import-maps">import maps</a> now providing a standard for package resolution in browsers
and other JavaScript runtimes, using the extensionless style can result in
bloated import map definitions. Explicit file extensions can avoid this issue by
enabling the import map to utilize a <a href="https://github.com/WICG/import-maps#packages-via-trailing-slashes">packages folder mapping</a> to map multiple
subpaths where possible instead of a separate map entry per package subpath
export. This also mirrors the requirement of using <a href="esm.html#mandatory-file-extensions">the full specifier path</a>
in relative and absolute import specifiers.</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>&quot;.&quot;</code> export is the only export, the <a href="#exports"><code>&quot;exports&quot;</code></a> field provides sugar
for this case being the direct <a href="#exports"><code>&quot;exports&quot;</code></a> field value.</p>
<pre><code class="language-json">{
  &quot;exports&quot;: {
    &quot;.&quot;: &quot;./index.js&quot;
  }
}
</code></pre>
<p>can be written:</p>
<pre><code class="language-json">{
  &quot;exports&quot;: &quot;./index.js&quot;
}
</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>&quot;exports&quot;</code></a> field, there is a package <code>&quot;imports&quot;</code> field
to create private mappings that only apply to import specifiers from within the
package itself.</p>
<p>Entries in the <code>&quot;imports&quot;</code> field must always start with <code>#</code> to ensure they are
disambiguated from external 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">// package.json
{
  &quot;imports&quot;: {
    &quot;#dep&quot;: {
      &quot;node&quot;: &quot;dep-node-native&quot;,
      &quot;default&quot;: &quot;./dep-polyfill.js&quot;
    }
  },
  &quot;dependencies&quot;: {
    &quot;dep-node-native&quot;: &quot;^1.0.0&quot;
  }
}
</code></pre>
<p>where <code>import &#39;#dep&#39;</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>&quot;exports&quot;</code> field, the <code>&quot;imports&quot;</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">
<details class="changelog"><summary>History</summary>
<table>
<tr><th>Version</th><th>Changes</th></tr>
<tr><td>v16.10.0, v14.19.0</td>
<td><p>Support pattern trailers in &quot;imports&quot; field.</p>
</td></tr>
<tr><td>v16.9.0, v14.19.0</td>
<td><p>Support pattern trailers.</p>
</td></tr>
<tr><td>v14.13.0, v12.20.0</td>
<td><p><span>Added in: v14.13.0, v12.20.0</span></p>
</td></tr>
</table>
</details>
</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">// ./node_modules/es-module-package/package.json
{
  &quot;exports&quot;: {
    &quot;./features/*.js&quot;: &quot;./src/features/*.js&quot;
  },
  &quot;imports&quot;: {
    &quot;#internal/*.js&quot;: &quot;./src/internal/*.js&quot;
  }
}
</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">import featureX from &#39;es-module-package/features/x.js&#39;;
// Loads ./node_modules/es-module-package/src/features/x.js

import featureY from &#39;es-module-package/features/y/y.js&#39;;
// Loads ./node_modules/es-module-package/src/features/y/y.js

import internalZ from &#39;#internal/z.js&#39;;
// Loads ./node_modules/es-module-package/src/internal/z.js
</code></pre>
<p>This is a direct static matching and replacement without any special handling
for file extensions. Including the <code>&quot;*.js&quot;</code> on both sides of the mapping
restricts the exposed package exports to only JS files.</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">// ./node_modules/es-module-package/package.json
{
  &quot;exports&quot;: {
    &quot;./features/*.js&quot;: &quot;./src/features/*.js&quot;,
    &quot;./features/private-internal/*&quot;: null
  }
}
</code></pre>
<pre><code class="language-js">import featureInternal from &#39;es-module-package/features/private-internal/m.js&#39;;
// Throws: ERR_PACKAGE_PATH_NOT_EXPORTED

import featureX from &#39;es-module-package/features/x.js&#39;;
// Loads ./node_modules/es-module-package/src/features/x.js
</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>
<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>
</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">// package.json
{
  &quot;exports&quot;: {
    &quot;import&quot;: &quot;./index-module.js&quot;,
    &quot;require&quot;: &quot;./index-require.cjs&quot;
  },
  &quot;type&quot;: &quot;module&quot;
}
</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>&quot;node-addons&quot;</code> - similar to <code>&quot;node&quot;</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&#39;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>&quot;node&quot;</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>&quot;import&quot;</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>&quot;require&quot;</code>.</em></li>
<li><code>&quot;require&quot;</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&#39;t support them. <em>Always mutually exclusive with
<code>&quot;import&quot;</code>.</em></li>
<li><code>&quot;default&quot;</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>&quot;exports&quot;</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>&quot;import&quot;</code> and <code>&quot;require&quot;</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>&quot;node-addons&quot;</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>&quot;node-addons&quot;</code>, it&#39;s recommended to treat
<code>&quot;default&quot;</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">{
  &quot;exports&quot;: {
    &quot;.&quot;: &quot;./index.js&quot;,
    &quot;./feature.js&quot;: {
      &quot;node&quot;: &quot;./feature-node.js&quot;,
      &quot;default&quot;: &quot;./feature.js&quot;
    }
  }
}
</code></pre>
<p>Defines a package where <code>require(&#39;pkg/feature.js&#39;)</code> and
<code>import &#39;pkg/feature.js&#39;</code> could provide different implementations between
Node.js and other JS environments.</p>
<p>When using environment branches, always include a <code>&quot;default&quot;</code> condition where
possible. Providing a <code>&quot;default&quot;</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>&quot;node&quot;</code> and <code>&quot;default&quot;</code> condition branches is usually preferable to using
<code>&quot;node&quot;</code> and <code>&quot;browser&quot;</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">{
  &quot;exports&quot;: {
    &quot;node&quot;: {
      &quot;import&quot;: &quot;./feature-node.mjs&quot;,
      &quot;require&quot;: &quot;./feature-node.cjs&quot;
    },
    &quot;default&quot;: &quot;./feature.mjs&quot;
  }
}
</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 index.js
</code></pre>
<p>which would then resolve the <code>&quot;development&quot;</code> condition in package imports and
exports, while resolving the existing <code>&quot;node&quot;</code>, <code>&quot;node-addons&quot;</code>, <code>&quot;default&quot;</code>,
<code>&quot;import&quot;</code>, and <code>&quot;require&quot;</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>&quot;import&quot;</code>, <code>&quot;require&quot;</code>, <code>&quot;node&quot;</code>,
<code>&quot;node-addons&quot;</code> and <code>&quot;default&quot;</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>&quot;types&quot;</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>&quot;browser&quot;</code> - any web browser environment.</li>
<li><code>&quot;development&quot;</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>&quot;production&quot;</code>.</em></li>
<li><code>&quot;production&quot;</code> - can be used to define a production environment entry
point. <em>Must always be mutually exclusive with <code>&quot;development&quot;</code>.</em></li>
</ul>
<p>For other runtimes, platform-specific condition key definitions are maintained
by the <a href="https://wintercg.org/">WinterCG</a> in the <a href="https://runtime-keys.proposal.wintercg.org/">Runtime Keys</a> proposal specification.</p>
<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&#39;t otherwise be possible. For example,
this would not necessarily be the case for company-specific or
application-specific conditions.</li>
<li>The condition should be such that a Node.js user would expect it to be in
Node.js core documentation. The <code>&quot;types&quot;</code> condition is a good example: It
doesn&#39;t really belong in the <a href="https://runtime-keys.proposal.wintercg.org/">Runtime Keys</a> proposal but is a good fit
here in the Node.js docs.</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>
<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>
</table>
</details>
</div><p>Within a package, the values defined in the package&#39;s
<code>package.json</code> <a href="#exports"><code>&quot;exports&quot;</code></a> field can be referenced via the package&#39;s name.
For example, assuming the <code>package.json</code> is:</p>
<pre><code class="language-json">// package.json
{
  &quot;name&quot;: &quot;a-package&quot;,
  &quot;exports&quot;: {
    &quot;.&quot;: &quot;./index.mjs&quot;,
    &quot;./foo.js&quot;: &quot;./foo.js&quot;
  }
}
</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">// ./a-module.mjs
import { something } from &#39;a-package&#39;; // Imports &quot;something&quot; from ./index.mjs.
</code></pre>
<p>Self-referencing is available only if <code>package.json</code> has <a href="#exports"><code>&quot;exports&quot;</code></a>, and
will allow importing only what that <a href="#exports"><code>&quot;exports&quot;</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">// ./another-module.mjs

// Imports &quot;another&quot; from ./m.mjs. Fails because
// the &quot;package.json&quot; &quot;exports&quot; field
// does not provide an export named &quot;./m.mjs&quot;.
import { another } from &#39;a-package/m.mjs&#39;;
</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-cjs">// ./a-module.js
const { something } = require(&#39;a-package/foo.js&#39;); // Loads from ./foo.js.
</code></pre>
<p>Finally, self-referencing also works with scoped packages. For example, this
code will also work:</p>
<pre><code class="language-json">// package.json
{
  &quot;name&quot;: &quot;@my/package&quot;,
  &quot;exports&quot;: &quot;./index.js&quot;
}
</code></pre>
<pre><code class="language-cjs">// ./index.js
module.exports = 42;
</code></pre>
<pre><code class="language-cjs">// ./other.js
console.log(require(&#39;@my/package&#39;));
</code></pre>
<pre><code class="language-console">$ node other.js
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>&quot;main&quot;</code></a> specifying the
CommonJS entry point and <code>package.json</code> <code>&quot;module&quot;</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>&quot;module&quot;</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>&#39;pkg&#39;</code> and <code>&#39;pkg/es-module&#39;</code>, or both at the same specifier via <a href="#conditional-exports">Conditional
exports</a>). Unlike in the scenario where <code>&quot;module&quot;</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(&#39;pkg&#39;)</code> is not the same as the <code>pkgInstance</code>
created by <code>import pkgInstance from &#39;pkg&#39;</code> (or an alternative main path like
<code>&#39;pkg/module&#39;</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>&#39;pkg&#39;</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>&#39;pkg/feature&#39;</code>.)</li>
<li>The package provides named exports, e.g. <code>import { name } from &#39;pkg&#39;</code> rather
than <code>import pkg from &#39;pkg&#39;; 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">// ./node_modules/pkg/package.json
{
  &quot;type&quot;: &quot;module&quot;,
  &quot;exports&quot;: {
    &quot;import&quot;: &quot;./wrapper.mjs&quot;,
    &quot;require&quot;: &quot;./index.cjs&quot;
  }
}
</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>&quot;type&quot;: &quot;module&quot;</code> will cause such files
to be treated as ES modules, just as <code>&quot;type&quot;: &quot;commonjs&quot;</code> would cause them
to be treated as CommonJS.
See <a href="esm.html#enabling">Enabling</a>.</p>
<pre><code class="language-cjs">// ./node_modules/pkg/index.cjs
exports.name = &#39;value&#39;;
</code></pre>
<pre><code class="language-js">// ./node_modules/pkg/wrapper.mjs
import cjsModule from &#39;./index.cjs&#39;;
export const name = cjsModule.name;
</code></pre>
<p>In this example, the <code>name</code> from <code>import { name } from &#39;pkg&#39;</code> is the same
singleton as the <code>name</code> from <code>const { name } = require(&#39;pkg&#39;)</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 &#39;pkg&#39;</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">import cjsModule from &#39;./index.cjs&#39;;
export const name = cjsModule.name;
export default 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&#39;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>&quot;./module&quot;</code>, to point to an all-ES module-syntax
version of the package. This could be used via <code>import &#39;pkg/module&#39;</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&#39;t affect the ES module version (for example, because the package is
stateless):</p>
<pre><code class="language-json">// ./node_modules/pkg/package.json
{
  &quot;type&quot;: &quot;module&quot;,
  &quot;exports&quot;: {
    &quot;.&quot;: &quot;./index.cjs&quot;,
    &quot;./module&quot;: &quot;./wrapper.mjs&quot;
  }
}
</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">// ./node_modules/pkg/package.json
{
  &quot;type&quot;: &quot;module&quot;,
  &quot;exports&quot;: {
    &quot;import&quot;: &quot;./index.mjs&quot;,
    &quot;require&quot;: &quot;./index.cjs&quot;
  }
}
</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&#39;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&#39;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&#39;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&#39;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&#39;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">import Date from &#39;date&#39;;
const someDate = new Date();
// someDate contains state; Date does not
</code></pre>
<p>The <code>new</code> keyword isn&#39;t required; a package&#39;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-cjs">// ./node_modules/pkg/index.cjs
const state = require(&#39;./state.cjs&#39;);
module.exports.state = state;
</code></pre>
<pre><code class="language-js">// ./node_modules/pkg/index.mjs
import state from &#39;./state.cjs&#39;;
export {
  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&#39;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>&quot;./module&quot;</code>, to point to an all-ES module-syntax version of the package:</p>
<pre><code class="language-json">// ./node_modules/pkg/package.json
{
  &quot;type&quot;: &quot;module&quot;,
  &quot;exports&quot;: {
    &quot;.&quot;: &quot;./index.cjs&quot;,
    &quot;./module&quot;: &quot;./index.mjs&quot;
  }
}
</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>&quot;name&quot;</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>&quot;main&quot;</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>&quot;packageManager&quot;</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>&quot;type&quot;</code></a> - The package type determining whether to load <code>.js</code> files as
CommonJS or ES modules.</li>
<li><a href="#exports"><code>&quot;exports&quot;</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>&quot;imports&quot;</code></a> - Package imports, for use by modules within the package
itself.</li>
</ul>
<h4><code>&quot;name&quot;</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>
<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>
</table>
</details>
</div><ul>
<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<pre><code class="language-json">{
  &quot;name&quot;: &quot;package-name&quot;
}
</code></pre>
<p>The <code>&quot;name&quot;</code> field defines your package&#39;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>&quot;name&quot;</code> field can be used in addition to the <a href="#exports"><code>&quot;exports&quot;</code></a> field to
<a href="#self-referencing-a-package-using-its-name">self-reference</a> a package using its name.</p>
<h4><code>&quot;main&quot;</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">&lt;string&gt;</a></li>
</ul>
<pre><code class="language-json">{
  &quot;main&quot;: &quot;./index.js&quot;
}
</code></pre>
<p>The <code>&quot;main&quot;</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>&quot;exports&quot;</code></a> field, this will take precedence over the
<code>&quot;main&quot;</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-cjs">// This resolves to ./path/to/directory/index.js.
require(&#39;./path/to/directory&#39;);
</code></pre>
<h4><code>&quot;packageManager&quot;</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, v14.19.0</span>
</div><blockquote>
<p>Stability: 1 - Experimental</p>
</blockquote>
<ul>
<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<pre><code class="language-json">{
  &quot;packageManager&quot;: &quot;&lt;package manager name&gt;@&lt;version&gt;&quot;
}
</code></pre>
<p>The <code>&quot;packageManager&quot;</code> field defines which package manager is expected to be
used when working on the current project. It can be 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 other 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>&quot;type&quot;</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>
<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>
</table>
</details>
</div><ul>
<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>The <code>&quot;type&quot;</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>&quot;type&quot;</code> with a value of
<code>&quot;module&quot;</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&#39;s parent, and so on up
until a node_modules folder or the volume root is reached.</p>
<pre><code class="language-json">// package.json
{
  &quot;type&quot;: &quot;module&quot;
}
</code></pre>
<pre><code class="language-bash"># In same folder as preceding package.json
node my-app.js # Runs as ES module
</code></pre>
<p>If the nearest parent <code>package.json</code> lacks a <code>&quot;type&quot;</code> field, or contains
<code>&quot;type&quot;: &quot;commonjs&quot;</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>&quot;type&quot;: &quot;module&quot;</code>.</p>
<pre><code class="language-js">// my-app.js, part of the same example as above
import &#39;./startup.js&#39;; // Loaded as ES module because of package.json
</code></pre>
<p>Regardless of the value of the <code>&quot;type&quot;</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>&quot;exports&quot;</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>
<tr><th>Version</th><th>Changes</th></tr>
<tr><td>v14.13.0, v12.20.0</td>
<td><p>Add support for <code>&quot;exports&quot;</code> patterns.</p>
</td></tr>
<tr><td>v13.7.0, v12.17.0</td>
<td><p>Unflag conditional exports.</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. In 12.16.0, conditional exports are still behind <code>--experimental-modules</code>.</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>
</table>
</details>
</div><ul>
<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string[]&gt;</a></li>
</ul>
<pre><code class="language-json">{
  &quot;exports&quot;: &quot;./index.js&quot;
}
</code></pre>
<p>The <code>&quot;exports&quot;</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>&quot;main&quot;</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>&quot;exports&quot;</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>&quot;exports&quot;</code> must be relative file URLs starting with
<code>./</code>.</p>
<h4><code>&quot;imports&quot;</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">&lt;Object&gt;</a></li>
</ul>
<pre><code class="language-json">// package.json
{
  &quot;imports&quot;: {
    &quot;#dep&quot;: {
      &quot;node&quot;: &quot;dep-node-native&quot;,
      &quot;default&quot;: &quot;./dep-polyfill.js&quot;
    }
  },
  &quot;dependencies&quot;: {
    &quot;dep-node-native&quot;: &quot;^1.0.0&quot;
  }
}
</code></pre>
<p>Entries in the imports field must be strings starting with <code>#</code>.</p>
<p>Package imports 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>
</body>
</html>

Zerion Mini Shell 1.0