11cb0ef41Sopenharmony_ci<!DOCTYPE html>
21cb0ef41Sopenharmony_ci<html lang="en">
31cb0ef41Sopenharmony_ci<head>
41cb0ef41Sopenharmony_ci  <meta charset="utf-8">
51cb0ef41Sopenharmony_ci  <meta name="viewport" content="width=device-width">
61cb0ef41Sopenharmony_ci  <meta name="nodejs.org:node-version" content="v18.20.1">
71cb0ef41Sopenharmony_ci  <title>Modules: CommonJS modules | Node.js v18.20.1 Documentation</title>
81cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
91cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/style.css">
101cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/hljs.css">
111cb0ef41Sopenharmony_ci  <link rel="canonical" href="https://nodejs.org/api/modules.html">
121cb0ef41Sopenharmony_ci  <script async defer src="assets/api.js" type="text/javascript"></script>
131cb0ef41Sopenharmony_ci  
141cb0ef41Sopenharmony_ci</head>
151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-modules">
161cb0ef41Sopenharmony_ci  <div id="content" class="clearfix">
171cb0ef41Sopenharmony_ci    <div id="column2" class="interior">
181cb0ef41Sopenharmony_ci      <div id="intro" class="interior">
191cb0ef41Sopenharmony_ci        <a href="/" title="Go back to the home page">
201cb0ef41Sopenharmony_ci          Node.js
211cb0ef41Sopenharmony_ci        </a>
221cb0ef41Sopenharmony_ci      </div>
231cb0ef41Sopenharmony_ci      <ul>
241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
261cb0ef41Sopenharmony_ci</ul>
271cb0ef41Sopenharmony_ci<hr class="line">
281cb0ef41Sopenharmony_ci<ul>
291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li>
501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules active">Modules: CommonJS modules</a></li>
571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li>
701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li>
831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li>
841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
891cb0ef41Sopenharmony_ci</ul>
901cb0ef41Sopenharmony_ci<hr class="line">
911cb0ef41Sopenharmony_ci<ul>
921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
931cb0ef41Sopenharmony_ci</ul>
941cb0ef41Sopenharmony_ci    </div>
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci    <div id="column1" data-id="modules" class="interior">
971cb0ef41Sopenharmony_ci      <header class="header">
981cb0ef41Sopenharmony_ci        <div class="header-container">
991cb0ef41Sopenharmony_ci          <h1>Node.js v18.20.1 documentation</h1>
1001cb0ef41Sopenharmony_ci          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
1011cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
1021cb0ef41Sopenharmony_ci              <path fill="none" d="M0 0h24v24H0z" />
1031cb0ef41Sopenharmony_ci              <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"/>
1041cb0ef41Sopenharmony_ci              <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"/>
1051cb0ef41Sopenharmony_ci            </svg>
1061cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
1071cb0ef41Sopenharmony_ci              <path d="M0 0h24v24H0z" fill="none" />
1081cb0ef41Sopenharmony_ci              <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"/>
1091cb0ef41Sopenharmony_ci            </svg>
1101cb0ef41Sopenharmony_ci          </button>
1111cb0ef41Sopenharmony_ci        </div>
1121cb0ef41Sopenharmony_ci        <div id="gtoc">
1131cb0ef41Sopenharmony_ci          <ul>
1141cb0ef41Sopenharmony_ci            <li class="pinned-header">Node.js v18.20.1</li>
1151cb0ef41Sopenharmony_ci            
1161cb0ef41Sopenharmony_ci    <li class="picker-header">
1171cb0ef41Sopenharmony_ci      <a href="#">
1181cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
1191cb0ef41Sopenharmony_ci        Table of contents
1201cb0ef41Sopenharmony_ci      </a>
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci      <div class="picker"><div class="toc"><ul>
1231cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#modules-commonjs-modules">Modules: CommonJS modules</a></span>
1241cb0ef41Sopenharmony_ci<ul>
1251cb0ef41Sopenharmony_ci<li><a href="#enabling">Enabling</a></li>
1261cb0ef41Sopenharmony_ci<li><a href="#accessing-the-main-module">Accessing the main module</a></li>
1271cb0ef41Sopenharmony_ci<li><a href="#package-manager-tips">Package manager tips</a></li>
1281cb0ef41Sopenharmony_ci<li><a href="#the-mjs-extension">The <code>.mjs</code> extension</a></li>
1291cb0ef41Sopenharmony_ci<li><a href="#all-together">All together</a></li>
1301cb0ef41Sopenharmony_ci<li><a href="#caching">Caching</a>
1311cb0ef41Sopenharmony_ci<ul>
1321cb0ef41Sopenharmony_ci<li><a href="#module-caching-caveats">Module caching caveats</a></li>
1331cb0ef41Sopenharmony_ci</ul>
1341cb0ef41Sopenharmony_ci</li>
1351cb0ef41Sopenharmony_ci<li><a href="#core-modules">Core modules</a></li>
1361cb0ef41Sopenharmony_ci<li><a href="#cycles">Cycles</a></li>
1371cb0ef41Sopenharmony_ci<li><a href="#file-modules">File modules</a></li>
1381cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#folders-as-modules">Folders as modules</a></span></li>
1391cb0ef41Sopenharmony_ci<li><a href="#loading-from-node_modules-folders">Loading from <code>node_modules</code> folders</a></li>
1401cb0ef41Sopenharmony_ci<li><a href="#loading-from-the-global-folders">Loading from the global folders</a></li>
1411cb0ef41Sopenharmony_ci<li><a href="#the-module-wrapper">The module wrapper</a></li>
1421cb0ef41Sopenharmony_ci<li><a href="#the-module-scope">The module scope</a>
1431cb0ef41Sopenharmony_ci<ul>
1441cb0ef41Sopenharmony_ci<li><a href="#__dirname"><code>__dirname</code></a></li>
1451cb0ef41Sopenharmony_ci<li><a href="#__filename"><code>__filename</code></a></li>
1461cb0ef41Sopenharmony_ci<li><a href="#exports"><code>exports</code></a></li>
1471cb0ef41Sopenharmony_ci<li><a href="#module"><code>module</code></a></li>
1481cb0ef41Sopenharmony_ci<li><a href="#requireid"><code>require(id)</code></a>
1491cb0ef41Sopenharmony_ci<ul>
1501cb0ef41Sopenharmony_ci<li><a href="#requirecache"><code>require.cache</code></a></li>
1511cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#requireextensions"><code>require.extensions</code></a></span></li>
1521cb0ef41Sopenharmony_ci<li><a href="#requiremain"><code>require.main</code></a></li>
1531cb0ef41Sopenharmony_ci<li><a href="#requireresolverequest-options"><code>require.resolve(request[, options])</code></a>
1541cb0ef41Sopenharmony_ci<ul>
1551cb0ef41Sopenharmony_ci<li><a href="#requireresolvepathsrequest"><code>require.resolve.paths(request)</code></a></li>
1561cb0ef41Sopenharmony_ci</ul>
1571cb0ef41Sopenharmony_ci</li>
1581cb0ef41Sopenharmony_ci</ul>
1591cb0ef41Sopenharmony_ci</li>
1601cb0ef41Sopenharmony_ci</ul>
1611cb0ef41Sopenharmony_ci</li>
1621cb0ef41Sopenharmony_ci<li><a href="#the-module-object">The <code>module</code> object</a>
1631cb0ef41Sopenharmony_ci<ul>
1641cb0ef41Sopenharmony_ci<li><a href="#modulechildren"><code>module.children</code></a></li>
1651cb0ef41Sopenharmony_ci<li><a href="#moduleexports"><code>module.exports</code></a>
1661cb0ef41Sopenharmony_ci<ul>
1671cb0ef41Sopenharmony_ci<li><a href="#exports-shortcut"><code>exports</code> shortcut</a></li>
1681cb0ef41Sopenharmony_ci</ul>
1691cb0ef41Sopenharmony_ci</li>
1701cb0ef41Sopenharmony_ci<li><a href="#modulefilename"><code>module.filename</code></a></li>
1711cb0ef41Sopenharmony_ci<li><a href="#moduleid"><code>module.id</code></a></li>
1721cb0ef41Sopenharmony_ci<li><a href="#moduleispreloading"><code>module.isPreloading</code></a></li>
1731cb0ef41Sopenharmony_ci<li><a href="#moduleloaded"><code>module.loaded</code></a></li>
1741cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#moduleparent"><code>module.parent</code></a></span></li>
1751cb0ef41Sopenharmony_ci<li><a href="#modulepath"><code>module.path</code></a></li>
1761cb0ef41Sopenharmony_ci<li><a href="#modulepaths"><code>module.paths</code></a></li>
1771cb0ef41Sopenharmony_ci<li><a href="#modulerequireid"><code>module.require(id)</code></a></li>
1781cb0ef41Sopenharmony_ci</ul>
1791cb0ef41Sopenharmony_ci</li>
1801cb0ef41Sopenharmony_ci<li><a href="#the-module-object_1">The <code>Module</code> object</a></li>
1811cb0ef41Sopenharmony_ci<li><a href="#source-map-v3-support">Source map v3 support</a></li>
1821cb0ef41Sopenharmony_ci</ul>
1831cb0ef41Sopenharmony_ci</li>
1841cb0ef41Sopenharmony_ci</ul></div></div>
1851cb0ef41Sopenharmony_ci    </li>
1861cb0ef41Sopenharmony_ci  
1871cb0ef41Sopenharmony_ci            
1881cb0ef41Sopenharmony_ci    <li class="picker-header">
1891cb0ef41Sopenharmony_ci      <a href="#">
1901cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
1911cb0ef41Sopenharmony_ci        Index
1921cb0ef41Sopenharmony_ci      </a>
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci      <div class="picker"><ul>
1951cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
1961cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_ci      <li>
1991cb0ef41Sopenharmony_ci        <a href="index.html">Index</a>
2001cb0ef41Sopenharmony_ci      </li>
2011cb0ef41Sopenharmony_ci    </ul>
2021cb0ef41Sopenharmony_ci  
2031cb0ef41Sopenharmony_ci<hr class="line">
2041cb0ef41Sopenharmony_ci<ul>
2051cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
2061cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
2071cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
2081cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
2091cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
2101cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
2111cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
2121cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
2131cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
2141cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
2151cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
2161cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
2171cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
2181cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
2191cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
2201cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
2211cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
2221cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
2231cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
2241cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
2251cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li>
2261cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
2271cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
2281cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
2291cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
2301cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
2311cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
2321cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules active">Modules: CommonJS modules</a></li>
2331cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
2341cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
2351cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
2361cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
2371cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
2381cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
2391cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
2401cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
2411cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
2421cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
2431cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
2441cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
2451cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li>
2461cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
2471cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
2481cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
2491cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
2501cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
2511cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
2521cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
2531cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
2541cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
2551cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
2561cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
2571cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
2581cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li>
2591cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li>
2601cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
2611cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
2621cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
2631cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
2641cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
2651cb0ef41Sopenharmony_ci</ul>
2661cb0ef41Sopenharmony_ci<hr class="line">
2671cb0ef41Sopenharmony_ci<ul>
2681cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
2691cb0ef41Sopenharmony_ci</ul></div>
2701cb0ef41Sopenharmony_ci    </li>
2711cb0ef41Sopenharmony_ci  
2721cb0ef41Sopenharmony_ci            
2731cb0ef41Sopenharmony_ci    <li class="picker-header">
2741cb0ef41Sopenharmony_ci      <a href="#">
2751cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
2761cb0ef41Sopenharmony_ci        Other versions
2771cb0ef41Sopenharmony_ci      </a>
2781cb0ef41Sopenharmony_ci      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/modules.html">21.x</a></li>
2791cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/modules.html">20.x <b>LTS</b></a></li>
2801cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/modules.html">19.x</a></li>
2811cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/modules.html">18.x <b>LTS</b></a></li>
2821cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/modules.html">17.x</a></li>
2831cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/modules.html">16.x</a></li>
2841cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/modules.html">15.x</a></li>
2851cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/modules.html">14.x</a></li>
2861cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/modules.html">13.x</a></li>
2871cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/modules.html">12.x</a></li>
2881cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/modules.html">11.x</a></li>
2891cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/modules.html">10.x</a></li>
2901cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/modules.html">9.x</a></li>
2911cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/modules.html">8.x</a></li>
2921cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/modules.html">7.x</a></li>
2931cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/modules.html">6.x</a></li>
2941cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/modules.html">5.x</a></li>
2951cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/modules.html">4.x</a></li>
2961cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/modules.html">0.12.x</a></li>
2971cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/modules.html">0.10.x</a></li></ol></div>
2981cb0ef41Sopenharmony_ci    </li>
2991cb0ef41Sopenharmony_ci  
3001cb0ef41Sopenharmony_ci            <li class="picker-header">
3011cb0ef41Sopenharmony_ci              <a href="#">
3021cb0ef41Sopenharmony_ci                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
3031cb0ef41Sopenharmony_ci                Options
3041cb0ef41Sopenharmony_ci              </a>
3051cb0ef41Sopenharmony_ci        
3061cb0ef41Sopenharmony_ci              <div class="picker">
3071cb0ef41Sopenharmony_ci                <ul>
3081cb0ef41Sopenharmony_ci                  <li>
3091cb0ef41Sopenharmony_ci                    <a href="all.html">View on single page</a>
3101cb0ef41Sopenharmony_ci                  </li>
3111cb0ef41Sopenharmony_ci                  <li>
3121cb0ef41Sopenharmony_ci                    <a href="modules.json">View as JSON</a>
3131cb0ef41Sopenharmony_ci                  </li>
3141cb0ef41Sopenharmony_ci                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/modules.md">Edit on GitHub</a></li>    
3151cb0ef41Sopenharmony_ci                </ul>
3161cb0ef41Sopenharmony_ci              </div>
3171cb0ef41Sopenharmony_ci            </li>
3181cb0ef41Sopenharmony_ci          </ul>
3191cb0ef41Sopenharmony_ci        </div>
3201cb0ef41Sopenharmony_ci        <hr>
3211cb0ef41Sopenharmony_ci      </header>
3221cb0ef41Sopenharmony_ci
3231cb0ef41Sopenharmony_ci      <details id="toc" open><summary>Table of contents</summary><ul>
3241cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#modules-commonjs-modules">Modules: CommonJS modules</a></span>
3251cb0ef41Sopenharmony_ci<ul>
3261cb0ef41Sopenharmony_ci<li><a href="#enabling">Enabling</a></li>
3271cb0ef41Sopenharmony_ci<li><a href="#accessing-the-main-module">Accessing the main module</a></li>
3281cb0ef41Sopenharmony_ci<li><a href="#package-manager-tips">Package manager tips</a></li>
3291cb0ef41Sopenharmony_ci<li><a href="#the-mjs-extension">The <code>.mjs</code> extension</a></li>
3301cb0ef41Sopenharmony_ci<li><a href="#all-together">All together</a></li>
3311cb0ef41Sopenharmony_ci<li><a href="#caching">Caching</a>
3321cb0ef41Sopenharmony_ci<ul>
3331cb0ef41Sopenharmony_ci<li><a href="#module-caching-caveats">Module caching caveats</a></li>
3341cb0ef41Sopenharmony_ci</ul>
3351cb0ef41Sopenharmony_ci</li>
3361cb0ef41Sopenharmony_ci<li><a href="#core-modules">Core modules</a></li>
3371cb0ef41Sopenharmony_ci<li><a href="#cycles">Cycles</a></li>
3381cb0ef41Sopenharmony_ci<li><a href="#file-modules">File modules</a></li>
3391cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#folders-as-modules">Folders as modules</a></span></li>
3401cb0ef41Sopenharmony_ci<li><a href="#loading-from-node_modules-folders">Loading from <code>node_modules</code> folders</a></li>
3411cb0ef41Sopenharmony_ci<li><a href="#loading-from-the-global-folders">Loading from the global folders</a></li>
3421cb0ef41Sopenharmony_ci<li><a href="#the-module-wrapper">The module wrapper</a></li>
3431cb0ef41Sopenharmony_ci<li><a href="#the-module-scope">The module scope</a>
3441cb0ef41Sopenharmony_ci<ul>
3451cb0ef41Sopenharmony_ci<li><a href="#__dirname"><code>__dirname</code></a></li>
3461cb0ef41Sopenharmony_ci<li><a href="#__filename"><code>__filename</code></a></li>
3471cb0ef41Sopenharmony_ci<li><a href="#exports"><code>exports</code></a></li>
3481cb0ef41Sopenharmony_ci<li><a href="#module"><code>module</code></a></li>
3491cb0ef41Sopenharmony_ci<li><a href="#requireid"><code>require(id)</code></a>
3501cb0ef41Sopenharmony_ci<ul>
3511cb0ef41Sopenharmony_ci<li><a href="#requirecache"><code>require.cache</code></a></li>
3521cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#requireextensions"><code>require.extensions</code></a></span></li>
3531cb0ef41Sopenharmony_ci<li><a href="#requiremain"><code>require.main</code></a></li>
3541cb0ef41Sopenharmony_ci<li><a href="#requireresolverequest-options"><code>require.resolve(request[, options])</code></a>
3551cb0ef41Sopenharmony_ci<ul>
3561cb0ef41Sopenharmony_ci<li><a href="#requireresolvepathsrequest"><code>require.resolve.paths(request)</code></a></li>
3571cb0ef41Sopenharmony_ci</ul>
3581cb0ef41Sopenharmony_ci</li>
3591cb0ef41Sopenharmony_ci</ul>
3601cb0ef41Sopenharmony_ci</li>
3611cb0ef41Sopenharmony_ci</ul>
3621cb0ef41Sopenharmony_ci</li>
3631cb0ef41Sopenharmony_ci<li><a href="#the-module-object">The <code>module</code> object</a>
3641cb0ef41Sopenharmony_ci<ul>
3651cb0ef41Sopenharmony_ci<li><a href="#modulechildren"><code>module.children</code></a></li>
3661cb0ef41Sopenharmony_ci<li><a href="#moduleexports"><code>module.exports</code></a>
3671cb0ef41Sopenharmony_ci<ul>
3681cb0ef41Sopenharmony_ci<li><a href="#exports-shortcut"><code>exports</code> shortcut</a></li>
3691cb0ef41Sopenharmony_ci</ul>
3701cb0ef41Sopenharmony_ci</li>
3711cb0ef41Sopenharmony_ci<li><a href="#modulefilename"><code>module.filename</code></a></li>
3721cb0ef41Sopenharmony_ci<li><a href="#moduleid"><code>module.id</code></a></li>
3731cb0ef41Sopenharmony_ci<li><a href="#moduleispreloading"><code>module.isPreloading</code></a></li>
3741cb0ef41Sopenharmony_ci<li><a href="#moduleloaded"><code>module.loaded</code></a></li>
3751cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#moduleparent"><code>module.parent</code></a></span></li>
3761cb0ef41Sopenharmony_ci<li><a href="#modulepath"><code>module.path</code></a></li>
3771cb0ef41Sopenharmony_ci<li><a href="#modulepaths"><code>module.paths</code></a></li>
3781cb0ef41Sopenharmony_ci<li><a href="#modulerequireid"><code>module.require(id)</code></a></li>
3791cb0ef41Sopenharmony_ci</ul>
3801cb0ef41Sopenharmony_ci</li>
3811cb0ef41Sopenharmony_ci<li><a href="#the-module-object_1">The <code>Module</code> object</a></li>
3821cb0ef41Sopenharmony_ci<li><a href="#source-map-v3-support">Source map v3 support</a></li>
3831cb0ef41Sopenharmony_ci</ul>
3841cb0ef41Sopenharmony_ci</li>
3851cb0ef41Sopenharmony_ci</ul></details>
3861cb0ef41Sopenharmony_ci
3871cb0ef41Sopenharmony_ci      <div id="apicontent">
3881cb0ef41Sopenharmony_ci        <h2>Modules: CommonJS modules<span><a class="mark" href="#modules-commonjs-modules" id="modules-commonjs-modules">#</a></span><a aria-hidden="true" class="legacy" id="modules_modules_commonjs_modules"></a></h2>
3891cb0ef41Sopenharmony_ci
3901cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
3911cb0ef41Sopenharmony_ci
3921cb0ef41Sopenharmony_ci<p>CommonJS modules are the original way to package JavaScript code for Node.js.
3931cb0ef41Sopenharmony_ciNode.js also supports the <a href="esm.html">ECMAScript modules</a> standard used by browsers
3941cb0ef41Sopenharmony_ciand other JavaScript runtimes.</p>
3951cb0ef41Sopenharmony_ci<p>In Node.js, each file is treated as a separate module. For
3961cb0ef41Sopenharmony_ciexample, consider a file named <code>foo.js</code>:</p>
3971cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> circle = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./circle.js'</span>);
3981cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`The area of a circle of radius 4 is <span class="hljs-subst">${circle.area(<span class="hljs-number">4</span>)}</span>`</span>);</code> <button class="copy-button">copy</button></pre>
3991cb0ef41Sopenharmony_ci<p>On the first line, <code>foo.js</code> loads the module <code>circle.js</code> that is in the same
4001cb0ef41Sopenharmony_cidirectory as <code>foo.js</code>.</p>
4011cb0ef41Sopenharmony_ci<p>Here are the contents of <code>circle.js</code>:</p>
4021cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-variable constant_">PI</span> } = <span class="hljs-title class_">Math</span>;
4031cb0ef41Sopenharmony_ci
4041cb0ef41Sopenharmony_ci<span class="hljs-built_in">exports</span>.<span class="hljs-property">area</span> = <span class="hljs-function">(<span class="hljs-params">r</span>) =></span> <span class="hljs-variable constant_">PI</span> * r ** <span class="hljs-number">2</span>;
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_ci<span class="hljs-built_in">exports</span>.<span class="hljs-property">circumference</span> = <span class="hljs-function">(<span class="hljs-params">r</span>) =></span> <span class="hljs-number">2</span> * <span class="hljs-variable constant_">PI</span> * r;</code> <button class="copy-button">copy</button></pre>
4071cb0ef41Sopenharmony_ci<p>The module <code>circle.js</code> has exported the functions <code>area()</code> and
4081cb0ef41Sopenharmony_ci<code>circumference()</code>. Functions and objects are added to the root of a module
4091cb0ef41Sopenharmony_ciby specifying additional properties on the special <code>exports</code> object.</p>
4101cb0ef41Sopenharmony_ci<p>Variables local to the module will be private, because the module is wrapped
4111cb0ef41Sopenharmony_ciin a function by Node.js (see <a href="#the-module-wrapper">module wrapper</a>).
4121cb0ef41Sopenharmony_ciIn this example, the variable <code>PI</code> is private to <code>circle.js</code>.</p>
4131cb0ef41Sopenharmony_ci<p>The <code>module.exports</code> property can be assigned a new value (such as a function
4141cb0ef41Sopenharmony_cior object).</p>
4151cb0ef41Sopenharmony_ci<p>Below, <code>bar.js</code> makes use of the <code>square</code> module, which exports a Square class:</p>
4161cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">Square</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./square.js'</span>);
4171cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> mySquare = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Square</span>(<span class="hljs-number">2</span>);
4181cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`The area of mySquare is <span class="hljs-subst">${mySquare.area()}</span>`</span>);</code> <button class="copy-button">copy</button></pre>
4191cb0ef41Sopenharmony_ci<p>The <code>square</code> module is defined in <code>square.js</code>:</p>
4201cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Assigning to exports will not modify module, must use module.exports</span>
4211cb0ef41Sopenharmony_ci<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-keyword">class</span> <span class="hljs-title class_">Square</span> {
4221cb0ef41Sopenharmony_ci  <span class="hljs-title function_">constructor</span>(<span class="hljs-params">width</span>) {
4231cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">this</span>.<span class="hljs-property">width</span> = width;
4241cb0ef41Sopenharmony_ci  }
4251cb0ef41Sopenharmony_ci
4261cb0ef41Sopenharmony_ci  <span class="hljs-title function_">area</span>(<span class="hljs-params"></span>) {
4271cb0ef41Sopenharmony_ci    <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">width</span> ** <span class="hljs-number">2</span>;
4281cb0ef41Sopenharmony_ci  }
4291cb0ef41Sopenharmony_ci};</code> <button class="copy-button">copy</button></pre>
4301cb0ef41Sopenharmony_ci<p>The CommonJS module system is implemented in the <a href="module.html"><code>module</code> core module</a>.</p>
4311cb0ef41Sopenharmony_ci<section><h3>Enabling<span><a class="mark" href="#enabling" id="enabling">#</a></span><a aria-hidden="true" class="legacy" id="modules_enabling"></a></h3>
4321cb0ef41Sopenharmony_ci
4331cb0ef41Sopenharmony_ci<p>Node.js has two module systems: CommonJS modules and <a href="esm.html">ECMAScript modules</a>.</p>
4341cb0ef41Sopenharmony_ci<p>By default, Node.js will treat the following as CommonJS modules:</p>
4351cb0ef41Sopenharmony_ci<ul>
4361cb0ef41Sopenharmony_ci<li>
4371cb0ef41Sopenharmony_ci<p>Files with a <code>.cjs</code> extension;</p>
4381cb0ef41Sopenharmony_ci</li>
4391cb0ef41Sopenharmony_ci<li>
4401cb0ef41Sopenharmony_ci<p>Files with a <code>.js</code> extension when the nearest parent <code>package.json</code> file
4411cb0ef41Sopenharmony_cicontains a top-level field <a href="packages.html#type"><code>"type"</code></a> with a value of <code>"commonjs"</code>.</p>
4421cb0ef41Sopenharmony_ci</li>
4431cb0ef41Sopenharmony_ci<li>
4441cb0ef41Sopenharmony_ci<p>Files with a <code>.js</code> extension when the nearest parent <code>package.json</code> file
4451cb0ef41Sopenharmony_cidoesn't contain a top-level field <a href="packages.html#type"><code>"type"</code></a>. Package authors should include
4461cb0ef41Sopenharmony_cithe <a href="packages.html#type"><code>"type"</code></a> field, even in packages where all sources are CommonJS. Being
4471cb0ef41Sopenharmony_ciexplicit about the <code>type</code> of the package will make things easier for build
4481cb0ef41Sopenharmony_citools and loaders to determine how the files in the package should be
4491cb0ef41Sopenharmony_ciinterpreted.</p>
4501cb0ef41Sopenharmony_ci</li>
4511cb0ef41Sopenharmony_ci<li>
4521cb0ef41Sopenharmony_ci<p>Files with an extension that is not <code>.mjs</code>, <code>.cjs</code>, <code>.json</code>, <code>.node</code>, or <code>.js</code>
4531cb0ef41Sopenharmony_ci(when the nearest parent <code>package.json</code> file contains a top-level field
4541cb0ef41Sopenharmony_ci<a href="packages.html#type"><code>"type"</code></a> with a value of <code>"module"</code>, those files will be recognized as
4551cb0ef41Sopenharmony_ciCommonJS modules only if they are being included via <code>require()</code>, not when
4561cb0ef41Sopenharmony_ciused as the command-line entry point of the program).</p>
4571cb0ef41Sopenharmony_ci</li>
4581cb0ef41Sopenharmony_ci</ul>
4591cb0ef41Sopenharmony_ci<p>See <a href="packages.html#determining-module-system">Determining module system</a> for more details.</p>
4601cb0ef41Sopenharmony_ci<p>Calling <code>require()</code> always use the CommonJS module loader. Calling <code>import()</code>
4611cb0ef41Sopenharmony_cialways use the ECMAScript module loader.</p>
4621cb0ef41Sopenharmony_ci</section><section><h3>Accessing the main module<span><a class="mark" href="#accessing-the-main-module" id="accessing-the-main-module">#</a></span><a aria-hidden="true" class="legacy" id="modules_accessing_the_main_module"></a></h3>
4631cb0ef41Sopenharmony_ci
4641cb0ef41Sopenharmony_ci<p>When a file is run directly from Node.js, <code>require.main</code> is set to its
4651cb0ef41Sopenharmony_ci<code>module</code>. That means that it is possible to determine whether a file has been
4661cb0ef41Sopenharmony_cirun directly by testing <code>require.main === module</code>.</p>
4671cb0ef41Sopenharmony_ci<p>For a file <code>foo.js</code>, this will be <code>true</code> if run via <code>node foo.js</code>, but
4681cb0ef41Sopenharmony_ci<code>false</code> if run by <code>require('./foo')</code>.</p>
4691cb0ef41Sopenharmony_ci<p>When the entry point is not a CommonJS module, <code>require.main</code> is <code>undefined</code>,
4701cb0ef41Sopenharmony_ciand the main module is out of reach.</p>
4711cb0ef41Sopenharmony_ci</section><section><h3>Package manager tips<span><a class="mark" href="#package-manager-tips" id="package-manager-tips">#</a></span><a aria-hidden="true" class="legacy" id="modules_package_manager_tips"></a></h3>
4721cb0ef41Sopenharmony_ci
4731cb0ef41Sopenharmony_ci<p>The semantics of the Node.js <code>require()</code> function were designed to be general
4741cb0ef41Sopenharmony_cienough to support reasonable directory structures. Package manager programs
4751cb0ef41Sopenharmony_cisuch as <code>dpkg</code>, <code>rpm</code>, and <code>npm</code> will hopefully find it possible to build
4761cb0ef41Sopenharmony_cinative packages from Node.js modules without modification.</p>
4771cb0ef41Sopenharmony_ci<p>Below we give a suggested directory structure that could work:</p>
4781cb0ef41Sopenharmony_ci<p>Let's say that we wanted to have the folder at
4791cb0ef41Sopenharmony_ci<code>/usr/lib/node/&#x3C;some-package>/&#x3C;some-version></code> hold the contents of a
4801cb0ef41Sopenharmony_cispecific version of a package.</p>
4811cb0ef41Sopenharmony_ci<p>Packages can depend on one another. In order to install package <code>foo</code>, it
4821cb0ef41Sopenharmony_cimay be necessary to install a specific version of package <code>bar</code>. The <code>bar</code>
4831cb0ef41Sopenharmony_cipackage may itself have dependencies, and in some cases, these may even collide
4841cb0ef41Sopenharmony_cior form cyclic dependencies.</p>
4851cb0ef41Sopenharmony_ci<p>Because Node.js looks up the <code>realpath</code> of any modules it loads (that is, it
4861cb0ef41Sopenharmony_ciresolves symlinks) and then <a href="#loading-from-node_modules-folders">looks for their dependencies in <code>node_modules</code> folders</a>,
4871cb0ef41Sopenharmony_cithis situation can be resolved with the following architecture:</p>
4881cb0ef41Sopenharmony_ci<ul>
4891cb0ef41Sopenharmony_ci<li><code>/usr/lib/node/foo/1.2.3/</code>: Contents of the <code>foo</code> package, version 1.2.3.</li>
4901cb0ef41Sopenharmony_ci<li><code>/usr/lib/node/bar/4.3.2/</code>: Contents of the <code>bar</code> package that <code>foo</code> depends
4911cb0ef41Sopenharmony_cion.</li>
4921cb0ef41Sopenharmony_ci<li><code>/usr/lib/node/foo/1.2.3/node_modules/bar</code>: Symbolic link to
4931cb0ef41Sopenharmony_ci<code>/usr/lib/node/bar/4.3.2/</code>.</li>
4941cb0ef41Sopenharmony_ci<li><code>/usr/lib/node/bar/4.3.2/node_modules/*</code>: Symbolic links to the packages that
4951cb0ef41Sopenharmony_ci<code>bar</code> depends on.</li>
4961cb0ef41Sopenharmony_ci</ul>
4971cb0ef41Sopenharmony_ci<p>Thus, even if a cycle is encountered, or if there are dependency
4981cb0ef41Sopenharmony_ciconflicts, every module will be able to get a version of its dependency
4991cb0ef41Sopenharmony_cithat it can use.</p>
5001cb0ef41Sopenharmony_ci<p>When the code in the <code>foo</code> package does <code>require('bar')</code>, it will get the
5011cb0ef41Sopenharmony_civersion that is symlinked into <code>/usr/lib/node/foo/1.2.3/node_modules/bar</code>.
5021cb0ef41Sopenharmony_ciThen, when the code in the <code>bar</code> package calls <code>require('quux')</code>, it'll get
5031cb0ef41Sopenharmony_cithe version that is symlinked into
5041cb0ef41Sopenharmony_ci<code>/usr/lib/node/bar/4.3.2/node_modules/quux</code>.</p>
5051cb0ef41Sopenharmony_ci<p>Furthermore, to make the module lookup process even more optimal, rather
5061cb0ef41Sopenharmony_cithan putting packages directly in <code>/usr/lib/node</code>, we could put them in
5071cb0ef41Sopenharmony_ci<code>/usr/lib/node_modules/&#x3C;name>/&#x3C;version></code>. Then Node.js will not bother
5081cb0ef41Sopenharmony_cilooking for missing dependencies in <code>/usr/node_modules</code> or <code>/node_modules</code>.</p>
5091cb0ef41Sopenharmony_ci<p>In order to make modules available to the Node.js REPL, it might be useful to
5101cb0ef41Sopenharmony_cialso add the <code>/usr/lib/node_modules</code> folder to the <code>$NODE_PATH</code> environment
5111cb0ef41Sopenharmony_civariable. Since the module lookups using <code>node_modules</code> folders are all
5121cb0ef41Sopenharmony_cirelative, and based on the real path of the files making the calls to
5131cb0ef41Sopenharmony_ci<code>require()</code>, the packages themselves can be anywhere.</p>
5141cb0ef41Sopenharmony_ci</section><section><h3>The <code>.mjs</code> extension<span><a class="mark" href="#the-mjs-extension" id="the-mjs-extension">#</a></span><a aria-hidden="true" class="legacy" id="modules_the_mjs_extension"></a></h3>
5151cb0ef41Sopenharmony_ci<p>Due to the synchronous nature of <code>require()</code>, it is not possible to use it to
5161cb0ef41Sopenharmony_ciload ECMAScript module files. Attempting to do so will throw a
5171cb0ef41Sopenharmony_ci<a href="errors.html#err_require_esm"><code>ERR_REQUIRE_ESM</code></a> error. Use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import"><code>import()</code></a> instead.</p>
5181cb0ef41Sopenharmony_ci<p>The <code>.mjs</code> extension is reserved for <a href="esm.html">ECMAScript Modules</a> which cannot be
5191cb0ef41Sopenharmony_ciloaded via <code>require()</code>. See <a href="packages.html#determining-module-system">Determining module system</a> section for more info
5201cb0ef41Sopenharmony_ciregarding which files are parsed as ECMAScript modules.</p>
5211cb0ef41Sopenharmony_ci</section><section><h3>All together<span><a class="mark" href="#all-together" id="all-together">#</a></span><a aria-hidden="true" class="legacy" id="modules_all_together"></a></h3>
5221cb0ef41Sopenharmony_ci
5231cb0ef41Sopenharmony_ci<p>To get the exact filename that will be loaded when <code>require()</code> is called, use
5241cb0ef41Sopenharmony_cithe <code>require.resolve()</code> function.</p>
5251cb0ef41Sopenharmony_ci<p>Putting together all of the above, here is the high-level algorithm
5261cb0ef41Sopenharmony_ciin pseudocode of what <code>require()</code> does:</p>
5271cb0ef41Sopenharmony_ci<pre>require(X) from module at path Y
5281cb0ef41Sopenharmony_ci1. If X is a core module,
5291cb0ef41Sopenharmony_ci   a. return the core module
5301cb0ef41Sopenharmony_ci   b. STOP
5311cb0ef41Sopenharmony_ci2. If X begins with '/'
5321cb0ef41Sopenharmony_ci   a. set Y to be the file system root
5331cb0ef41Sopenharmony_ci3. If X begins with './' or '/' or '../'
5341cb0ef41Sopenharmony_ci   a. LOAD_AS_FILE(Y + X)
5351cb0ef41Sopenharmony_ci   b. LOAD_AS_DIRECTORY(Y + X)
5361cb0ef41Sopenharmony_ci   c. THROW "not found"
5371cb0ef41Sopenharmony_ci4. If X begins with '#'
5381cb0ef41Sopenharmony_ci   a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))
5391cb0ef41Sopenharmony_ci5. LOAD_PACKAGE_SELF(X, dirname(Y))
5401cb0ef41Sopenharmony_ci6. LOAD_NODE_MODULES(X, dirname(Y))
5411cb0ef41Sopenharmony_ci7. THROW "not found"
5421cb0ef41Sopenharmony_ci
5431cb0ef41Sopenharmony_ciLOAD_AS_FILE(X)
5441cb0ef41Sopenharmony_ci1. If X is a file, load X as its file extension format. STOP
5451cb0ef41Sopenharmony_ci2. If X.js is a file, load X.js as JavaScript text. STOP
5461cb0ef41Sopenharmony_ci3. If X.json is a file, parse X.json to a JavaScript Object. STOP
5471cb0ef41Sopenharmony_ci4. If X.node is a file, load X.node as binary addon. STOP
5481cb0ef41Sopenharmony_ci
5491cb0ef41Sopenharmony_ciLOAD_INDEX(X)
5501cb0ef41Sopenharmony_ci1. If X/index.js is a file, load X/index.js as JavaScript text. STOP
5511cb0ef41Sopenharmony_ci2. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
5521cb0ef41Sopenharmony_ci3. If X/index.node is a file, load X/index.node as binary addon. STOP
5531cb0ef41Sopenharmony_ci
5541cb0ef41Sopenharmony_ciLOAD_AS_DIRECTORY(X)
5551cb0ef41Sopenharmony_ci1. If X/package.json is a file,
5561cb0ef41Sopenharmony_ci   a. Parse X/package.json, and look for "main" field.
5571cb0ef41Sopenharmony_ci   b. If "main" is a falsy value, GOTO 2.
5581cb0ef41Sopenharmony_ci   c. let M = X + (json main field)
5591cb0ef41Sopenharmony_ci   d. LOAD_AS_FILE(M)
5601cb0ef41Sopenharmony_ci   e. LOAD_INDEX(M)
5611cb0ef41Sopenharmony_ci   f. LOAD_INDEX(X) DEPRECATED
5621cb0ef41Sopenharmony_ci   g. THROW "not found"
5631cb0ef41Sopenharmony_ci2. LOAD_INDEX(X)
5641cb0ef41Sopenharmony_ci
5651cb0ef41Sopenharmony_ciLOAD_NODE_MODULES(X, START)
5661cb0ef41Sopenharmony_ci1. let DIRS = NODE_MODULES_PATHS(START)
5671cb0ef41Sopenharmony_ci2. for each DIR in DIRS:
5681cb0ef41Sopenharmony_ci   a. LOAD_PACKAGE_EXPORTS(X, DIR)
5691cb0ef41Sopenharmony_ci   b. LOAD_AS_FILE(DIR/X)
5701cb0ef41Sopenharmony_ci   c. LOAD_AS_DIRECTORY(DIR/X)
5711cb0ef41Sopenharmony_ci
5721cb0ef41Sopenharmony_ciNODE_MODULES_PATHS(START)
5731cb0ef41Sopenharmony_ci1. let PARTS = path split(START)
5741cb0ef41Sopenharmony_ci2. let I = count of PARTS - 1
5751cb0ef41Sopenharmony_ci3. let DIRS = []
5761cb0ef41Sopenharmony_ci4. while I >= 0,
5771cb0ef41Sopenharmony_ci   a. if PARTS[I] = "node_modules" CONTINUE
5781cb0ef41Sopenharmony_ci   b. DIR = path join(PARTS[0 .. I] + "node_modules")
5791cb0ef41Sopenharmony_ci   c. DIRS = DIR + DIRS
5801cb0ef41Sopenharmony_ci   d. let I = I - 1
5811cb0ef41Sopenharmony_ci5. return DIRS + GLOBAL_FOLDERS
5821cb0ef41Sopenharmony_ci
5831cb0ef41Sopenharmony_ciLOAD_PACKAGE_IMPORTS(X, DIR)
5841cb0ef41Sopenharmony_ci1. Find the closest package scope SCOPE to DIR.
5851cb0ef41Sopenharmony_ci2. If no scope was found, return.
5861cb0ef41Sopenharmony_ci3. If the SCOPE/package.json "imports" is null or undefined, return.
5871cb0ef41Sopenharmony_ci4. let MATCH = PACKAGE_IMPORTS_RESOLVE(X, pathToFileURL(SCOPE),
5881cb0ef41Sopenharmony_ci  ["node", "require"]) <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
5891cb0ef41Sopenharmony_ci5. RESOLVE_ESM_MATCH(MATCH).
5901cb0ef41Sopenharmony_ci
5911cb0ef41Sopenharmony_ciLOAD_PACKAGE_EXPORTS(X, DIR)
5921cb0ef41Sopenharmony_ci1. Try to interpret X as a combination of NAME and SUBPATH where the name
5931cb0ef41Sopenharmony_ci   may have a @scope/ prefix and the subpath begins with a slash (`/`).
5941cb0ef41Sopenharmony_ci2. If X does not match this pattern or DIR/NAME/package.json is not a file,
5951cb0ef41Sopenharmony_ci   return.
5961cb0ef41Sopenharmony_ci3. Parse DIR/NAME/package.json, and look for "exports" field.
5971cb0ef41Sopenharmony_ci4. If "exports" is null or undefined, return.
5981cb0ef41Sopenharmony_ci5. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(DIR/NAME), "." + SUBPATH,
5991cb0ef41Sopenharmony_ci   `package.json` "exports", ["node", "require"]) <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
6001cb0ef41Sopenharmony_ci6. RESOLVE_ESM_MATCH(MATCH)
6011cb0ef41Sopenharmony_ci
6021cb0ef41Sopenharmony_ciLOAD_PACKAGE_SELF(X, DIR)
6031cb0ef41Sopenharmony_ci1. Find the closest package scope SCOPE to DIR.
6041cb0ef41Sopenharmony_ci2. If no scope was found, return.
6051cb0ef41Sopenharmony_ci3. If the SCOPE/package.json "exports" is null or undefined, return.
6061cb0ef41Sopenharmony_ci4. If the SCOPE/package.json "name" is not the first segment of X, return.
6071cb0ef41Sopenharmony_ci5. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(SCOPE),
6081cb0ef41Sopenharmony_ci   "." + X.slice("name".length), `package.json` "exports", ["node", "require"])
6091cb0ef41Sopenharmony_ci   <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
6101cb0ef41Sopenharmony_ci6. RESOLVE_ESM_MATCH(MATCH)
6111cb0ef41Sopenharmony_ci
6121cb0ef41Sopenharmony_ciRESOLVE_ESM_MATCH(MATCH)
6131cb0ef41Sopenharmony_ci1. let RESOLVED_PATH = fileURLToPath(MATCH)
6141cb0ef41Sopenharmony_ci2. If the file at RESOLVED_PATH exists, load RESOLVED_PATH as its extension
6151cb0ef41Sopenharmony_ci   format. STOP
6161cb0ef41Sopenharmony_ci3. THROW "not found"
6171cb0ef41Sopenharmony_ci</pre>
6181cb0ef41Sopenharmony_ci</section><section><h3>Caching<span><a class="mark" href="#caching" id="caching">#</a></span><a aria-hidden="true" class="legacy" id="modules_caching"></a></h3>
6191cb0ef41Sopenharmony_ci
6201cb0ef41Sopenharmony_ci<p>Modules are cached after the first time they are loaded. This means (among other
6211cb0ef41Sopenharmony_cithings) that every call to <code>require('foo')</code> will get exactly the same object
6221cb0ef41Sopenharmony_cireturned, if it would resolve to the same file.</p>
6231cb0ef41Sopenharmony_ci<p>Provided <code>require.cache</code> is not modified, multiple calls to <code>require('foo')</code>
6241cb0ef41Sopenharmony_ciwill not cause the module code to be executed multiple times. This is an
6251cb0ef41Sopenharmony_ciimportant feature. With it, "partially done" objects can be returned, thus
6261cb0ef41Sopenharmony_ciallowing transitive dependencies to be loaded even when they would cause cycles.</p>
6271cb0ef41Sopenharmony_ci<p>To have a module execute code multiple times, export a function, and call that
6281cb0ef41Sopenharmony_cifunction.</p>
6291cb0ef41Sopenharmony_ci<h4>Module caching caveats<span><a class="mark" href="#module-caching-caveats" id="module-caching-caveats">#</a></span><a aria-hidden="true" class="legacy" id="modules_module_caching_caveats"></a></h4>
6301cb0ef41Sopenharmony_ci
6311cb0ef41Sopenharmony_ci<p>Modules are cached based on their resolved filename. Since modules may resolve
6321cb0ef41Sopenharmony_cito a different filename based on the location of the calling module (loading
6331cb0ef41Sopenharmony_cifrom <code>node_modules</code> folders), it is not a <em>guarantee</em> that <code>require('foo')</code> will
6341cb0ef41Sopenharmony_cialways return the exact same object, if it would resolve to different files.</p>
6351cb0ef41Sopenharmony_ci<p>Additionally, on case-insensitive file systems or operating systems, different
6361cb0ef41Sopenharmony_ciresolved filenames can point to the same file, but the cache will still treat
6371cb0ef41Sopenharmony_cithem as different modules and will reload the file multiple times. For example,
6381cb0ef41Sopenharmony_ci<code>require('./foo')</code> and <code>require('./FOO')</code> return two different objects,
6391cb0ef41Sopenharmony_ciirrespective of whether or not <code>./foo</code> and <code>./FOO</code> are the same file.</p>
6401cb0ef41Sopenharmony_ci</section><section><h3>Core modules<span><a class="mark" href="#core-modules" id="core-modules">#</a></span><a aria-hidden="true" class="legacy" id="modules_core_modules"></a></h3>
6411cb0ef41Sopenharmony_ci
6421cb0ef41Sopenharmony_ci<div class="api_metadata">
6431cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
6441cb0ef41Sopenharmony_ci<table>
6451cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
6461cb0ef41Sopenharmony_ci<tr><td>v16.0.0, v14.18.0</td>
6471cb0ef41Sopenharmony_ci<td><p>Added <code>node:</code> import support to <code>require(...)</code>.</p></td></tr>
6481cb0ef41Sopenharmony_ci</tbody></table>
6491cb0ef41Sopenharmony_ci</details>
6501cb0ef41Sopenharmony_ci</div>
6511cb0ef41Sopenharmony_ci<p>Node.js has several modules compiled into the binary. These modules are
6521cb0ef41Sopenharmony_cidescribed in greater detail elsewhere in this documentation.</p>
6531cb0ef41Sopenharmony_ci<p>The core modules are defined within the Node.js source and are located in the
6541cb0ef41Sopenharmony_ci<code>lib/</code> folder.</p>
6551cb0ef41Sopenharmony_ci<p>Core modules can be identified using the <code>node:</code> prefix, in which case
6561cb0ef41Sopenharmony_ciit bypasses the <code>require</code> cache. For instance, <code>require('node:http')</code> will
6571cb0ef41Sopenharmony_cialways return the built in HTTP module, even if there is <code>require.cache</code> entry
6581cb0ef41Sopenharmony_ciby that name.</p>
6591cb0ef41Sopenharmony_ci<p>Some core modules are always preferentially loaded if their identifier is
6601cb0ef41Sopenharmony_cipassed to <code>require()</code>. For instance, <code>require('http')</code> will always
6611cb0ef41Sopenharmony_cireturn the built-in HTTP module, even if there is a file by that name. The list
6621cb0ef41Sopenharmony_ciof core modules that can be loaded without using the <code>node:</code> prefix is exposed
6631cb0ef41Sopenharmony_cias <a href="module.html#modulebuiltinmodules"><code>module.builtinModules</code></a>.</p>
6641cb0ef41Sopenharmony_ci</section><section><h3>Cycles<span><a class="mark" href="#cycles" id="cycles">#</a></span><a aria-hidden="true" class="legacy" id="modules_cycles"></a></h3>
6651cb0ef41Sopenharmony_ci
6661cb0ef41Sopenharmony_ci<p>When there are circular <code>require()</code> calls, a module might not have finished
6671cb0ef41Sopenharmony_ciexecuting when it is returned.</p>
6681cb0ef41Sopenharmony_ci<p>Consider this situation:</p>
6691cb0ef41Sopenharmony_ci<p><code>a.js</code>:</p>
6701cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'a starting'</span>);
6711cb0ef41Sopenharmony_ci<span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="hljs-literal">false</span>;
6721cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> b = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./b.js'</span>);
6731cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'in a, b.done = %j'</span>, b.<span class="hljs-property">done</span>);
6741cb0ef41Sopenharmony_ci<span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="hljs-literal">true</span>;
6751cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'a done'</span>);</code> <button class="copy-button">copy</button></pre>
6761cb0ef41Sopenharmony_ci<p><code>b.js</code>:</p>
6771cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'b starting'</span>);
6781cb0ef41Sopenharmony_ci<span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="hljs-literal">false</span>;
6791cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> a = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./a.js'</span>);
6801cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'in b, a.done = %j'</span>, a.<span class="hljs-property">done</span>);
6811cb0ef41Sopenharmony_ci<span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="hljs-literal">true</span>;
6821cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'b done'</span>);</code> <button class="copy-button">copy</button></pre>
6831cb0ef41Sopenharmony_ci<p><code>main.js</code>:</p>
6841cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'main starting'</span>);
6851cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> a = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./a.js'</span>);
6861cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> b = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./b.js'</span>);
6871cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'in main, a.done = %j, b.done = %j'</span>, a.<span class="hljs-property">done</span>, b.<span class="hljs-property">done</span>);</code> <button class="copy-button">copy</button></pre>
6881cb0ef41Sopenharmony_ci<p>When <code>main.js</code> loads <code>a.js</code>, then <code>a.js</code> in turn loads <code>b.js</code>. At that
6891cb0ef41Sopenharmony_cipoint, <code>b.js</code> tries to load <code>a.js</code>. In order to prevent an infinite
6901cb0ef41Sopenharmony_ciloop, an <strong>unfinished copy</strong> of the <code>a.js</code> exports object is returned to the
6911cb0ef41Sopenharmony_ci<code>b.js</code> module. <code>b.js</code> then finishes loading, and its <code>exports</code> object is
6921cb0ef41Sopenharmony_ciprovided to the <code>a.js</code> module.</p>
6931cb0ef41Sopenharmony_ci<p>By the time <code>main.js</code> has loaded both modules, they're both finished.
6941cb0ef41Sopenharmony_ciThe output of this program would thus be:</p>
6951cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node main.js</span>
6961cb0ef41Sopenharmony_cimain starting
6971cb0ef41Sopenharmony_cia starting
6981cb0ef41Sopenharmony_cib starting
6991cb0ef41Sopenharmony_ciin b, a.done = false
7001cb0ef41Sopenharmony_cib done
7011cb0ef41Sopenharmony_ciin a, b.done = true
7021cb0ef41Sopenharmony_cia done
7031cb0ef41Sopenharmony_ciin main, a.done = true, b.done = true</code> <button class="copy-button">copy</button></pre>
7041cb0ef41Sopenharmony_ci<p>Careful planning is required to allow cyclic module dependencies to work
7051cb0ef41Sopenharmony_cicorrectly within an application.</p>
7061cb0ef41Sopenharmony_ci</section><section><h3>File modules<span><a class="mark" href="#file-modules" id="file-modules">#</a></span><a aria-hidden="true" class="legacy" id="modules_file_modules"></a></h3>
7071cb0ef41Sopenharmony_ci
7081cb0ef41Sopenharmony_ci<p>If the exact filename is not found, then Node.js will attempt to load the
7091cb0ef41Sopenharmony_cirequired filename with the added extensions: <code>.js</code>, <code>.json</code>, and finally
7101cb0ef41Sopenharmony_ci<code>.node</code>. When loading a file that has a different extension (e.g. <code>.cjs</code>), its
7111cb0ef41Sopenharmony_cifull name must be passed to <code>require()</code>, including its file extension (e.g.
7121cb0ef41Sopenharmony_ci<code>require('./file.cjs')</code>).</p>
7131cb0ef41Sopenharmony_ci<p><code>.json</code> files are parsed as JSON text files, <code>.node</code> files are interpreted as
7141cb0ef41Sopenharmony_cicompiled addon modules loaded with <code>process.dlopen()</code>. Files using any other
7151cb0ef41Sopenharmony_ciextension (or no extension at all) are parsed as JavaScript text files. Refer to
7161cb0ef41Sopenharmony_cithe <a href="packages.html#determining-module-system">Determining module system</a> section to understand what parse goal will be
7171cb0ef41Sopenharmony_ciused.</p>
7181cb0ef41Sopenharmony_ci<p>A required module prefixed with <code>'/'</code> is an absolute path to the file. For
7191cb0ef41Sopenharmony_ciexample, <code>require('/home/marco/foo.js')</code> will load the file at
7201cb0ef41Sopenharmony_ci<code>/home/marco/foo.js</code>.</p>
7211cb0ef41Sopenharmony_ci<p>A required module prefixed with <code>'./'</code> is relative to the file calling
7221cb0ef41Sopenharmony_ci<code>require()</code>. That is, <code>circle.js</code> must be in the same directory as <code>foo.js</code> for
7231cb0ef41Sopenharmony_ci<code>require('./circle')</code> to find it.</p>
7241cb0ef41Sopenharmony_ci<p>Without a leading <code>'/'</code>, <code>'./'</code>, or <code>'../'</code> to indicate a file, the module must
7251cb0ef41Sopenharmony_cieither be a core module or is loaded from a <code>node_modules</code> folder.</p>
7261cb0ef41Sopenharmony_ci<p>If the given path does not exist, <code>require()</code> will throw a
7271cb0ef41Sopenharmony_ci<a href="errors.html#module_not_found"><code>MODULE_NOT_FOUND</code></a> error.</p>
7281cb0ef41Sopenharmony_ci</section><section><h3>Folders as modules<span><a class="mark" href="#folders-as-modules" id="folders-as-modules">#</a></span><a aria-hidden="true" class="legacy" id="modules_folders_as_modules"></a></h3>
7291cb0ef41Sopenharmony_ci
7301cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use <a href="packages.html#subpath-exports">subpath exports</a> or <a href="packages.html#subpath-imports">subpath imports</a> instead.</div><p></p>
7311cb0ef41Sopenharmony_ci<p>There are three ways in which a folder may be passed to <code>require()</code> as
7321cb0ef41Sopenharmony_cian argument.</p>
7331cb0ef41Sopenharmony_ci<p>The first is to create a <a href="packages.html#nodejs-packagejson-field-definitions"><code>package.json</code></a> file in the root of the folder,
7341cb0ef41Sopenharmony_ciwhich specifies a <code>main</code> module. An example <a href="packages.html#nodejs-packagejson-field-definitions"><code>package.json</code></a> file might
7351cb0ef41Sopenharmony_cilook like this:</p>
7361cb0ef41Sopenharmony_ci<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">"some-library"</span><span class="hljs-punctuation">,</span>
7371cb0ef41Sopenharmony_ci  <span class="hljs-attr">"main"</span> <span class="hljs-punctuation">:</span> <span class="hljs-string">"./lib/some-library.js"</span> <span class="hljs-punctuation">}</span></code> <button class="copy-button">copy</button></pre>
7381cb0ef41Sopenharmony_ci<p>If this was in a folder at <code>./some-library</code>, then
7391cb0ef41Sopenharmony_ci<code>require('./some-library')</code> would attempt to load
7401cb0ef41Sopenharmony_ci<code>./some-library/lib/some-library.js</code>.</p>
7411cb0ef41Sopenharmony_ci<p>If there is no <a href="packages.html#nodejs-packagejson-field-definitions"><code>package.json</code></a> file present in the directory, or if the
7421cb0ef41Sopenharmony_ci<a href="packages.html#main"><code>"main"</code></a> entry is missing or cannot be resolved, then Node.js
7431cb0ef41Sopenharmony_ciwill attempt to load an <code>index.js</code> or <code>index.node</code> file out of that
7441cb0ef41Sopenharmony_cidirectory. For example, if there was no <a href="packages.html#nodejs-packagejson-field-definitions"><code>package.json</code></a> file in the previous
7451cb0ef41Sopenharmony_ciexample, then <code>require('./some-library')</code> would attempt to load:</p>
7461cb0ef41Sopenharmony_ci<ul>
7471cb0ef41Sopenharmony_ci<li><code>./some-library/index.js</code></li>
7481cb0ef41Sopenharmony_ci<li><code>./some-library/index.node</code></li>
7491cb0ef41Sopenharmony_ci</ul>
7501cb0ef41Sopenharmony_ci<p>If these attempts fail, then Node.js will report the entire module as missing
7511cb0ef41Sopenharmony_ciwith the default error:</p>
7521cb0ef41Sopenharmony_ci<pre><code class="language-console">Error: Cannot find module 'some-library'</code> <button class="copy-button">copy</button></pre>
7531cb0ef41Sopenharmony_ci<p>In all three above cases, an <code>import('./some-library')</code> call would result in a
7541cb0ef41Sopenharmony_ci<a href="errors.html#err_unsupported_dir_import"><code>ERR_UNSUPPORTED_DIR_IMPORT</code></a> error. Using package <a href="packages.html#subpath-exports">subpath exports</a> or
7551cb0ef41Sopenharmony_ci<a href="packages.html#subpath-imports">subpath imports</a> can provide the same containment organization benefits as
7561cb0ef41Sopenharmony_cifolders as modules, and work for both <code>require</code> and <code>import</code>.</p>
7571cb0ef41Sopenharmony_ci</section><section><h3>Loading from <code>node_modules</code> folders<span><a class="mark" href="#loading-from-node_modules-folders" id="loading-from-node_modules-folders">#</a></span><a aria-hidden="true" class="legacy" id="modules_loading_from_node_modules_folders"></a></h3>
7581cb0ef41Sopenharmony_ci
7591cb0ef41Sopenharmony_ci<p>If the module identifier passed to <code>require()</code> is not a
7601cb0ef41Sopenharmony_ci<a href="#core-modules">core</a> module, and does not begin with <code>'/'</code>, <code>'../'</code>, or
7611cb0ef41Sopenharmony_ci<code>'./'</code>, then Node.js starts at the directory of the current module, and
7621cb0ef41Sopenharmony_ciadds <code>/node_modules</code>, and attempts to load the module from that location.
7631cb0ef41Sopenharmony_ciNode.js will not append <code>node_modules</code> to a path already ending in
7641cb0ef41Sopenharmony_ci<code>node_modules</code>.</p>
7651cb0ef41Sopenharmony_ci<p>If it is not found there, then it moves to the parent directory, and so
7661cb0ef41Sopenharmony_cion, until the root of the file system is reached.</p>
7671cb0ef41Sopenharmony_ci<p>For example, if the file at <code>'/home/ry/projects/foo.js'</code> called
7681cb0ef41Sopenharmony_ci<code>require('bar.js')</code>, then Node.js would look in the following locations, in
7691cb0ef41Sopenharmony_cithis order:</p>
7701cb0ef41Sopenharmony_ci<ul>
7711cb0ef41Sopenharmony_ci<li><code>/home/ry/projects/node_modules/bar.js</code></li>
7721cb0ef41Sopenharmony_ci<li><code>/home/ry/node_modules/bar.js</code></li>
7731cb0ef41Sopenharmony_ci<li><code>/home/node_modules/bar.js</code></li>
7741cb0ef41Sopenharmony_ci<li><code>/node_modules/bar.js</code></li>
7751cb0ef41Sopenharmony_ci</ul>
7761cb0ef41Sopenharmony_ci<p>This allows programs to localize their dependencies, so that they do not
7771cb0ef41Sopenharmony_ciclash.</p>
7781cb0ef41Sopenharmony_ci<p>It is possible to require specific files or sub modules distributed with a
7791cb0ef41Sopenharmony_cimodule by including a path suffix after the module name. For instance
7801cb0ef41Sopenharmony_ci<code>require('example-module/path/to/file')</code> would resolve <code>path/to/file</code>
7811cb0ef41Sopenharmony_cirelative to where <code>example-module</code> is located. The suffixed path follows the
7821cb0ef41Sopenharmony_cisame module resolution semantics.</p>
7831cb0ef41Sopenharmony_ci</section><section><h3>Loading from the global folders<span><a class="mark" href="#loading-from-the-global-folders" id="loading-from-the-global-folders">#</a></span><a aria-hidden="true" class="legacy" id="modules_loading_from_the_global_folders"></a></h3>
7841cb0ef41Sopenharmony_ci
7851cb0ef41Sopenharmony_ci<p>If the <code>NODE_PATH</code> environment variable is set to a colon-delimited list
7861cb0ef41Sopenharmony_ciof absolute paths, then Node.js will search those paths for modules if they
7871cb0ef41Sopenharmony_ciare not found elsewhere.</p>
7881cb0ef41Sopenharmony_ci<p>On Windows, <code>NODE_PATH</code> is delimited by semicolons (<code>;</code>) instead of colons.</p>
7891cb0ef41Sopenharmony_ci<p><code>NODE_PATH</code> was originally created to support loading modules from
7901cb0ef41Sopenharmony_civarying paths before the current <a href="#all-together">module resolution</a> algorithm was defined.</p>
7911cb0ef41Sopenharmony_ci<p><code>NODE_PATH</code> is still supported, but is less necessary now that the Node.js
7921cb0ef41Sopenharmony_ciecosystem has settled on a convention for locating dependent modules.
7931cb0ef41Sopenharmony_ciSometimes deployments that rely on <code>NODE_PATH</code> show surprising behavior
7941cb0ef41Sopenharmony_ciwhen people are unaware that <code>NODE_PATH</code> must be set. Sometimes a
7951cb0ef41Sopenharmony_cimodule's dependencies change, causing a different version (or even a
7961cb0ef41Sopenharmony_cidifferent module) to be loaded as the <code>NODE_PATH</code> is searched.</p>
7971cb0ef41Sopenharmony_ci<p>Additionally, Node.js will search in the following list of GLOBAL_FOLDERS:</p>
7981cb0ef41Sopenharmony_ci<ul>
7991cb0ef41Sopenharmony_ci<li>1: <code>$HOME/.node_modules</code></li>
8001cb0ef41Sopenharmony_ci<li>2: <code>$HOME/.node_libraries</code></li>
8011cb0ef41Sopenharmony_ci<li>3: <code>$PREFIX/lib/node</code></li>
8021cb0ef41Sopenharmony_ci</ul>
8031cb0ef41Sopenharmony_ci<p>Where <code>$HOME</code> is the user's home directory, and <code>$PREFIX</code> is the Node.js
8041cb0ef41Sopenharmony_ciconfigured <code>node_prefix</code>.</p>
8051cb0ef41Sopenharmony_ci<p>These are mostly for historic reasons.</p>
8061cb0ef41Sopenharmony_ci<p>It is strongly encouraged to place dependencies in the local <code>node_modules</code>
8071cb0ef41Sopenharmony_cifolder. These will be loaded faster, and more reliably.</p>
8081cb0ef41Sopenharmony_ci</section><section><h3>The module wrapper<span><a class="mark" href="#the-module-wrapper" id="the-module-wrapper">#</a></span><a aria-hidden="true" class="legacy" id="modules_the_module_wrapper"></a></h3>
8091cb0ef41Sopenharmony_ci
8101cb0ef41Sopenharmony_ci<p>Before a module's code is executed, Node.js will wrap it with a function
8111cb0ef41Sopenharmony_ciwrapper that looks like the following:</p>
8121cb0ef41Sopenharmony_ci<pre><code class="language-js">(<span class="hljs-keyword">function</span>(<span class="hljs-params"><span class="hljs-built_in">exports</span>, <span class="hljs-built_in">require</span>, <span class="hljs-variable language_">module</span>, __filename, __dirname</span>) {
8131cb0ef41Sopenharmony_ci<span class="hljs-comment">// Module code actually lives in here</span>
8141cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
8151cb0ef41Sopenharmony_ci<p>By doing this, Node.js achieves a few things:</p>
8161cb0ef41Sopenharmony_ci<ul>
8171cb0ef41Sopenharmony_ci<li>It keeps top-level variables (defined with <code>var</code>, <code>const</code>, or <code>let</code>) scoped to
8181cb0ef41Sopenharmony_cithe module rather than the global object.</li>
8191cb0ef41Sopenharmony_ci<li>It helps to provide some global-looking variables that are actually specific
8201cb0ef41Sopenharmony_cito the module, such as:
8211cb0ef41Sopenharmony_ci<ul>
8221cb0ef41Sopenharmony_ci<li>The <code>module</code> and <code>exports</code> objects that the implementor can use to export
8231cb0ef41Sopenharmony_civalues from the module.</li>
8241cb0ef41Sopenharmony_ci<li>The convenience variables <code>__filename</code> and <code>__dirname</code>, containing the
8251cb0ef41Sopenharmony_cimodule's absolute filename and directory path.</li>
8261cb0ef41Sopenharmony_ci</ul>
8271cb0ef41Sopenharmony_ci</li>
8281cb0ef41Sopenharmony_ci</ul>
8291cb0ef41Sopenharmony_ci</section><section><h3>The module scope<span><a class="mark" href="#the-module-scope" id="the-module-scope">#</a></span><a aria-hidden="true" class="legacy" id="modules_the_module_scope"></a></h3>
8301cb0ef41Sopenharmony_ci<h4><code>__dirname</code><span><a class="mark" href="#__dirname" id="__dirname">#</a></span><a aria-hidden="true" class="legacy" id="modules_dirname"></a></h4>
8311cb0ef41Sopenharmony_ci<div class="api_metadata">
8321cb0ef41Sopenharmony_ci<span>Added in: v0.1.27</span>
8331cb0ef41Sopenharmony_ci</div>
8341cb0ef41Sopenharmony_ci
8351cb0ef41Sopenharmony_ci<ul>
8361cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
8371cb0ef41Sopenharmony_ci</ul>
8381cb0ef41Sopenharmony_ci<p>The directory name of the current module. This is the same as the
8391cb0ef41Sopenharmony_ci<a href="path.html#pathdirnamepath"><code>path.dirname()</code></a> of the <a href="#__filename"><code>__filename</code></a>.</p>
8401cb0ef41Sopenharmony_ci<p>Example: running <code>node example.js</code> from <code>/Users/mjr</code></p>
8411cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(__dirname);
8421cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: /Users/mjr</span>
8431cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(path.<span class="hljs-title function_">dirname</span>(__filename));
8441cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: /Users/mjr</span></code> <button class="copy-button">copy</button></pre>
8451cb0ef41Sopenharmony_ci<h4><code>__filename</code><span><a class="mark" href="#__filename" id="__filename">#</a></span><a aria-hidden="true" class="legacy" id="modules_filename"></a></h4>
8461cb0ef41Sopenharmony_ci<div class="api_metadata">
8471cb0ef41Sopenharmony_ci<span>Added in: v0.0.1</span>
8481cb0ef41Sopenharmony_ci</div>
8491cb0ef41Sopenharmony_ci
8501cb0ef41Sopenharmony_ci<ul>
8511cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
8521cb0ef41Sopenharmony_ci</ul>
8531cb0ef41Sopenharmony_ci<p>The file name of the current module. This is the current module file's absolute
8541cb0ef41Sopenharmony_cipath with symlinks resolved.</p>
8551cb0ef41Sopenharmony_ci<p>For a main program this is not necessarily the same as the file name used in the
8561cb0ef41Sopenharmony_cicommand line.</p>
8571cb0ef41Sopenharmony_ci<p>See <a href="#__dirname"><code>__dirname</code></a> for the directory name of the current module.</p>
8581cb0ef41Sopenharmony_ci<p>Examples:</p>
8591cb0ef41Sopenharmony_ci<p>Running <code>node example.js</code> from <code>/Users/mjr</code></p>
8601cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(__filename);
8611cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: /Users/mjr/example.js</span>
8621cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(__dirname);
8631cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: /Users/mjr</span></code> <button class="copy-button">copy</button></pre>
8641cb0ef41Sopenharmony_ci<p>Given two modules: <code>a</code> and <code>b</code>, where <code>b</code> is a dependency of
8651cb0ef41Sopenharmony_ci<code>a</code> and there is a directory structure of:</p>
8661cb0ef41Sopenharmony_ci<ul>
8671cb0ef41Sopenharmony_ci<li><code>/Users/mjr/app/a.js</code></li>
8681cb0ef41Sopenharmony_ci<li><code>/Users/mjr/app/node_modules/b/b.js</code></li>
8691cb0ef41Sopenharmony_ci</ul>
8701cb0ef41Sopenharmony_ci<p>References to <code>__filename</code> within <code>b.js</code> will return
8711cb0ef41Sopenharmony_ci<code>/Users/mjr/app/node_modules/b/b.js</code> while references to <code>__filename</code> within
8721cb0ef41Sopenharmony_ci<code>a.js</code> will return <code>/Users/mjr/app/a.js</code>.</p>
8731cb0ef41Sopenharmony_ci<h4><code>exports</code><span><a class="mark" href="#exports" id="exports">#</a></span><a aria-hidden="true" class="legacy" id="modules_exports"></a></h4>
8741cb0ef41Sopenharmony_ci<div class="api_metadata">
8751cb0ef41Sopenharmony_ci<span>Added in: v0.1.12</span>
8761cb0ef41Sopenharmony_ci</div>
8771cb0ef41Sopenharmony_ci
8781cb0ef41Sopenharmony_ci<ul>
8791cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
8801cb0ef41Sopenharmony_ci</ul>
8811cb0ef41Sopenharmony_ci<p>A reference to the <code>module.exports</code> that is shorter to type.
8821cb0ef41Sopenharmony_ciSee the section about the <a href="#exports-shortcut">exports shortcut</a> for details on when to use
8831cb0ef41Sopenharmony_ci<code>exports</code> and when to use <code>module.exports</code>.</p>
8841cb0ef41Sopenharmony_ci<h4><code>module</code><span><a class="mark" href="#module" id="module">#</a></span><a aria-hidden="true" class="legacy" id="modules_module"></a></h4>
8851cb0ef41Sopenharmony_ci<div class="api_metadata">
8861cb0ef41Sopenharmony_ci<span>Added in: v0.1.16</span>
8871cb0ef41Sopenharmony_ci</div>
8881cb0ef41Sopenharmony_ci
8891cb0ef41Sopenharmony_ci<ul>
8901cb0ef41Sopenharmony_ci<li><a href="modules.html#the-module-object" class="type">&#x3C;module></a></li>
8911cb0ef41Sopenharmony_ci</ul>
8921cb0ef41Sopenharmony_ci<p>A reference to the current module, see the section about the
8931cb0ef41Sopenharmony_ci<a href="#the-module-object"><code>module</code> object</a>. In particular, <code>module.exports</code> is used for defining what
8941cb0ef41Sopenharmony_cia module exports and makes available through <code>require()</code>.</p>
8951cb0ef41Sopenharmony_ci<h4><code>require(id)</code><span><a class="mark" href="#requireid" id="requireid">#</a></span><a aria-hidden="true" class="legacy" id="modules_require_id"></a></h4>
8961cb0ef41Sopenharmony_ci<div class="api_metadata">
8971cb0ef41Sopenharmony_ci<span>Added in: v0.1.13</span>
8981cb0ef41Sopenharmony_ci</div>
8991cb0ef41Sopenharmony_ci
9001cb0ef41Sopenharmony_ci<ul>
9011cb0ef41Sopenharmony_ci<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> module name or path</li>
9021cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> exported module content</li>
9031cb0ef41Sopenharmony_ci</ul>
9041cb0ef41Sopenharmony_ci<p>Used to import modules, <code>JSON</code>, and local files. Modules can be imported
9051cb0ef41Sopenharmony_cifrom <code>node_modules</code>. Local modules and JSON files can be imported using
9061cb0ef41Sopenharmony_cia relative path (e.g. <code>./</code>, <code>./foo</code>, <code>./bar/baz</code>, <code>../foo</code>) that will be
9071cb0ef41Sopenharmony_ciresolved against the directory named by <a href="#__dirname"><code>__dirname</code></a> (if defined) or
9081cb0ef41Sopenharmony_cithe current working directory. The relative paths of POSIX style are resolved
9091cb0ef41Sopenharmony_ciin an OS independent fashion, meaning that the examples above will work on
9101cb0ef41Sopenharmony_ciWindows in the same way they would on Unix systems.</p>
9111cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Importing a local module with a path relative to the `__dirname` or current</span>
9121cb0ef41Sopenharmony_ci<span class="hljs-comment">// working directory. (On Windows, this would resolve to .\path\myLocalModule.)</span>
9131cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myLocalModule = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./path/myLocalModule'</span>);
9141cb0ef41Sopenharmony_ci
9151cb0ef41Sopenharmony_ci<span class="hljs-comment">// Importing a JSON file:</span>
9161cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> jsonData = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./path/filename.json'</span>);
9171cb0ef41Sopenharmony_ci
9181cb0ef41Sopenharmony_ci<span class="hljs-comment">// Importing a module from node_modules or Node.js built-in module:</span>
9191cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>);</code> <button class="copy-button">copy</button></pre>
9201cb0ef41Sopenharmony_ci<h5><code>require.cache</code><span><a class="mark" href="#requirecache" id="requirecache">#</a></span><a aria-hidden="true" class="legacy" id="modules_require_cache"></a></h5>
9211cb0ef41Sopenharmony_ci<div class="api_metadata">
9221cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span>
9231cb0ef41Sopenharmony_ci</div>
9241cb0ef41Sopenharmony_ci<ul>
9251cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
9261cb0ef41Sopenharmony_ci</ul>
9271cb0ef41Sopenharmony_ci<p>Modules are cached in this object when they are required. By deleting a key
9281cb0ef41Sopenharmony_civalue from this object, the next <code>require</code> will reload the module.
9291cb0ef41Sopenharmony_ciThis does not apply to <a href="addons.html">native addons</a>, for which reloading will result in an
9301cb0ef41Sopenharmony_cierror.</p>
9311cb0ef41Sopenharmony_ci<p>Adding or replacing entries is also possible. This cache is checked before
9321cb0ef41Sopenharmony_cibuilt-in modules and if a name matching a built-in module is added to the cache,
9331cb0ef41Sopenharmony_cionly <code>node:</code>-prefixed require calls are going to receive the built-in module.
9341cb0ef41Sopenharmony_ciUse with care!</p>
9351cb0ef41Sopenharmony_ci<!-- eslint-disable node-core/no-duplicate-requires -->
9361cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>);
9371cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> realFs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
9381cb0ef41Sopenharmony_ci
9391cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> fakeFs = {};
9401cb0ef41Sopenharmony_ci<span class="hljs-built_in">require</span>.<span class="hljs-property">cache</span>.<span class="hljs-property">fs</span> = { <span class="hljs-attr">exports</span>: fakeFs };
9411cb0ef41Sopenharmony_ci
9421cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>), fakeFs);
9431cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>), realFs);</code> <button class="copy-button">copy</button></pre>
9441cb0ef41Sopenharmony_ci<h5><code>require.extensions</code><span><a class="mark" href="#requireextensions" id="requireextensions">#</a></span><a aria-hidden="true" class="legacy" id="modules_require_extensions"></a></h5>
9451cb0ef41Sopenharmony_ci<div class="api_metadata">
9461cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span><span>Deprecated since: v0.10.6</span>
9471cb0ef41Sopenharmony_ci</div>
9481cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated</div><p></p>
9491cb0ef41Sopenharmony_ci<ul>
9501cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
9511cb0ef41Sopenharmony_ci</ul>
9521cb0ef41Sopenharmony_ci<p>Instruct <code>require</code> on how to handle certain file extensions.</p>
9531cb0ef41Sopenharmony_ci<p>Process files with the extension <code>.sjs</code> as <code>.js</code>:</p>
9541cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-built_in">require</span>.<span class="hljs-property">extensions</span>[<span class="hljs-string">'.sjs'</span>] = <span class="hljs-built_in">require</span>.<span class="hljs-property">extensions</span>[<span class="hljs-string">'.js'</span>];</code> <button class="copy-button">copy</button></pre>
9551cb0ef41Sopenharmony_ci<p><strong>Deprecated.</strong> In the past, this list has been used to load non-JavaScript
9561cb0ef41Sopenharmony_cimodules into Node.js by compiling them on-demand. However, in practice, there
9571cb0ef41Sopenharmony_ciare much better ways to do this, such as loading modules via some other Node.js
9581cb0ef41Sopenharmony_ciprogram, or compiling them to JavaScript ahead of time.</p>
9591cb0ef41Sopenharmony_ci<p>Avoid using <code>require.extensions</code>. Use could cause subtle bugs and resolving the
9601cb0ef41Sopenharmony_ciextensions gets slower with each registered extension.</p>
9611cb0ef41Sopenharmony_ci<h5><code>require.main</code><span><a class="mark" href="#requiremain" id="requiremain">#</a></span><a aria-hidden="true" class="legacy" id="modules_require_main"></a></h5>
9621cb0ef41Sopenharmony_ci<div class="api_metadata">
9631cb0ef41Sopenharmony_ci<span>Added in: v0.1.17</span>
9641cb0ef41Sopenharmony_ci</div>
9651cb0ef41Sopenharmony_ci<ul>
9661cb0ef41Sopenharmony_ci<li><a href="modules.html#the-module-object" class="type">&#x3C;module></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
9671cb0ef41Sopenharmony_ci</ul>
9681cb0ef41Sopenharmony_ci<p>The <code>Module</code> object representing the entry script loaded when the Node.js
9691cb0ef41Sopenharmony_ciprocess launched, or <code>undefined</code> if the entry point of the program is not a
9701cb0ef41Sopenharmony_ciCommonJS module.
9711cb0ef41Sopenharmony_ciSee <a href="#accessing-the-main-module">"Accessing the main module"</a>.</p>
9721cb0ef41Sopenharmony_ci<p>In <code>entry.js</code> script:</p>
9731cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-built_in">require</span>.<span class="hljs-property">main</span>);</code> <button class="copy-button">copy</button></pre>
9741cb0ef41Sopenharmony_ci<pre><code class="language-bash">node entry.js</code> <button class="copy-button">copy</button></pre>
9751cb0ef41Sopenharmony_ci<!-- eslint-skip -->
9761cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-title class_">Module</span> {
9771cb0ef41Sopenharmony_ci  <span class="hljs-attr">id</span>: <span class="hljs-string">'.'</span>,
9781cb0ef41Sopenharmony_ci  <span class="hljs-attr">path</span>: <span class="hljs-string">'/absolute/path/to'</span>,
9791cb0ef41Sopenharmony_ci  <span class="hljs-attr">exports</span>: {},
9801cb0ef41Sopenharmony_ci  <span class="hljs-attr">filename</span>: <span class="hljs-string">'/absolute/path/to/entry.js'</span>,
9811cb0ef41Sopenharmony_ci  <span class="hljs-attr">loaded</span>: <span class="hljs-literal">false</span>,
9821cb0ef41Sopenharmony_ci  <span class="hljs-attr">children</span>: [],
9831cb0ef41Sopenharmony_ci  <span class="hljs-attr">paths</span>:
9841cb0ef41Sopenharmony_ci   [ <span class="hljs-string">'/absolute/path/to/node_modules'</span>,
9851cb0ef41Sopenharmony_ci     <span class="hljs-string">'/absolute/path/node_modules'</span>,
9861cb0ef41Sopenharmony_ci     <span class="hljs-string">'/absolute/node_modules'</span>,
9871cb0ef41Sopenharmony_ci     <span class="hljs-string">'/node_modules'</span> ] }</code> <button class="copy-button">copy</button></pre>
9881cb0ef41Sopenharmony_ci<h5><code>require.resolve(request[, options])</code><span><a class="mark" href="#requireresolverequest-options" id="requireresolverequest-options">#</a></span><a aria-hidden="true" class="legacy" id="modules_require_resolve_request_options"></a></h5>
9891cb0ef41Sopenharmony_ci<div class="api_metadata">
9901cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
9911cb0ef41Sopenharmony_ci<table>
9921cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
9931cb0ef41Sopenharmony_ci<tr><td>v8.9.0</td>
9941cb0ef41Sopenharmony_ci<td><p>The <code>paths</code> option is now supported.</p></td></tr>
9951cb0ef41Sopenharmony_ci<tr><td>v0.3.0</td>
9961cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.0</span></p></td></tr>
9971cb0ef41Sopenharmony_ci</tbody></table>
9981cb0ef41Sopenharmony_ci</details>
9991cb0ef41Sopenharmony_ci</div>
10001cb0ef41Sopenharmony_ci<ul>
10011cb0ef41Sopenharmony_ci<li><code>request</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The module path to resolve.</li>
10021cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
10031cb0ef41Sopenharmony_ci<ul>
10041cb0ef41Sopenharmony_ci<li><code>paths</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> Paths to resolve module location from. If present, these
10051cb0ef41Sopenharmony_cipaths are used instead of the default resolution paths, with the exception
10061cb0ef41Sopenharmony_ciof <a href="#loading-from-the-global-folders">GLOBAL_FOLDERS</a> like <code>$HOME/.node_modules</code>, which are
10071cb0ef41Sopenharmony_cialways included. Each of these paths is used as a starting point for
10081cb0ef41Sopenharmony_cithe module resolution algorithm, meaning that the <code>node_modules</code> hierarchy
10091cb0ef41Sopenharmony_ciis checked from this location.</li>
10101cb0ef41Sopenharmony_ci</ul>
10111cb0ef41Sopenharmony_ci</li>
10121cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
10131cb0ef41Sopenharmony_ci</ul>
10141cb0ef41Sopenharmony_ci<p>Use the internal <code>require()</code> machinery to look up the location of a module,
10151cb0ef41Sopenharmony_cibut rather than loading the module, just return the resolved filename.</p>
10161cb0ef41Sopenharmony_ci<p>If the module can not be found, a <code>MODULE_NOT_FOUND</code> error is thrown.</p>
10171cb0ef41Sopenharmony_ci<h6><code>require.resolve.paths(request)</code><span><a class="mark" href="#requireresolvepathsrequest" id="requireresolvepathsrequest">#</a></span><a aria-hidden="true" class="legacy" id="modules_require_resolve_paths_request"></a></h6>
10181cb0ef41Sopenharmony_ci<div class="api_metadata">
10191cb0ef41Sopenharmony_ci<span>Added in: v8.9.0</span>
10201cb0ef41Sopenharmony_ci</div>
10211cb0ef41Sopenharmony_ci<ul>
10221cb0ef41Sopenharmony_ci<li><code>request</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The module path whose lookup paths are being retrieved.</li>
10231cb0ef41Sopenharmony_ci<li>Returns: <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#Null_type" class="type">&#x3C;null></a></li>
10241cb0ef41Sopenharmony_ci</ul>
10251cb0ef41Sopenharmony_ci<p>Returns an array containing the paths searched during resolution of <code>request</code> or
10261cb0ef41Sopenharmony_ci<code>null</code> if the <code>request</code> string references a core module, for example <code>http</code> or
10271cb0ef41Sopenharmony_ci<code>fs</code>.</p>
10281cb0ef41Sopenharmony_ci</section><section><h3>The <code>module</code> object<span><a class="mark" href="#the-module-object" id="the-module-object">#</a></span><a aria-hidden="true" class="legacy" id="modules_the_module_object"></a></h3>
10291cb0ef41Sopenharmony_ci<div class="api_metadata">
10301cb0ef41Sopenharmony_ci<span>Added in: v0.1.16</span>
10311cb0ef41Sopenharmony_ci</div>
10321cb0ef41Sopenharmony_ci
10331cb0ef41Sopenharmony_ci
10341cb0ef41Sopenharmony_ci<ul>
10351cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
10361cb0ef41Sopenharmony_ci</ul>
10371cb0ef41Sopenharmony_ci<p>In each module, the <code>module</code> free variable is a reference to the object
10381cb0ef41Sopenharmony_cirepresenting the current module. For convenience, <code>module.exports</code> is
10391cb0ef41Sopenharmony_cialso accessible via the <code>exports</code> module-global. <code>module</code> is not actually
10401cb0ef41Sopenharmony_cia global but rather local to each module.</p>
10411cb0ef41Sopenharmony_ci<h4><code>module.children</code><span><a class="mark" href="#modulechildren" id="modulechildren">#</a></span><a aria-hidden="true" class="legacy" id="modules_module_children"></a></h4>
10421cb0ef41Sopenharmony_ci<div class="api_metadata">
10431cb0ef41Sopenharmony_ci<span>Added in: v0.1.16</span>
10441cb0ef41Sopenharmony_ci</div>
10451cb0ef41Sopenharmony_ci<ul>
10461cb0ef41Sopenharmony_ci<li><a href="modules.html#the-module-object" class="type">&#x3C;module[]></a></li>
10471cb0ef41Sopenharmony_ci</ul>
10481cb0ef41Sopenharmony_ci<p>The module objects required for the first time by this one.</p>
10491cb0ef41Sopenharmony_ci<h4><code>module.exports</code><span><a class="mark" href="#moduleexports" id="moduleexports">#</a></span><a aria-hidden="true" class="legacy" id="modules_module_exports"></a></h4>
10501cb0ef41Sopenharmony_ci<div class="api_metadata">
10511cb0ef41Sopenharmony_ci<span>Added in: v0.1.16</span>
10521cb0ef41Sopenharmony_ci</div>
10531cb0ef41Sopenharmony_ci<ul>
10541cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
10551cb0ef41Sopenharmony_ci</ul>
10561cb0ef41Sopenharmony_ci<p>The <code>module.exports</code> object is created by the <code>Module</code> system. Sometimes this is
10571cb0ef41Sopenharmony_cinot acceptable; many want their module to be an instance of some class. To do
10581cb0ef41Sopenharmony_cithis, assign the desired export object to <code>module.exports</code>. Assigning
10591cb0ef41Sopenharmony_cithe desired object to <code>exports</code> will simply rebind the local <code>exports</code> variable,
10601cb0ef41Sopenharmony_ciwhich is probably not what is desired.</p>
10611cb0ef41Sopenharmony_ci<p>For example, suppose we were making a module called <code>a.js</code>:</p>
10621cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>);
10631cb0ef41Sopenharmony_ci
10641cb0ef41Sopenharmony_ci<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
10651cb0ef41Sopenharmony_ci
10661cb0ef41Sopenharmony_ci<span class="hljs-comment">// Do some work, and after some time emit</span>
10671cb0ef41Sopenharmony_ci<span class="hljs-comment">// the 'ready' event from the module itself.</span>
10681cb0ef41Sopenharmony_ci<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
10691cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ready'</span>);
10701cb0ef41Sopenharmony_ci}, <span class="hljs-number">1000</span>);</code> <button class="copy-button">copy</button></pre>
10711cb0ef41Sopenharmony_ci<p>Then in another file we could do:</p>
10721cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> a = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./a'</span>);
10731cb0ef41Sopenharmony_cia.<span class="hljs-title function_">on</span>(<span class="hljs-string">'ready'</span>, <span class="hljs-function">() =></span> {
10741cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'module "a" is ready'</span>);
10751cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
10761cb0ef41Sopenharmony_ci<p>Assignment to <code>module.exports</code> must be done immediately. It cannot be
10771cb0ef41Sopenharmony_cidone in any callbacks. This does not work:</p>
10781cb0ef41Sopenharmony_ci<p><code>x.js</code>:</p>
10791cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
10801cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = { <span class="hljs-attr">a</span>: <span class="hljs-string">'hello'</span> };
10811cb0ef41Sopenharmony_ci}, <span class="hljs-number">0</span>);</code> <button class="copy-button">copy</button></pre>
10821cb0ef41Sopenharmony_ci<p><code>y.js</code>:</p>
10831cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> x = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./x'</span>);
10841cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(x.<span class="hljs-property">a</span>);</code> <button class="copy-button">copy</button></pre>
10851cb0ef41Sopenharmony_ci<h5><code>exports</code> shortcut<span><a class="mark" href="#exports-shortcut" id="exports-shortcut">#</a></span><a aria-hidden="true" class="legacy" id="modules_exports_shortcut"></a></h5>
10861cb0ef41Sopenharmony_ci<div class="api_metadata">
10871cb0ef41Sopenharmony_ci<span>Added in: v0.1.16</span>
10881cb0ef41Sopenharmony_ci</div>
10891cb0ef41Sopenharmony_ci<p>The <code>exports</code> variable is available within a module's file-level scope, and is
10901cb0ef41Sopenharmony_ciassigned the value of <code>module.exports</code> before the module is evaluated.</p>
10911cb0ef41Sopenharmony_ci<p>It allows a shortcut, so that <code>module.exports.f = ...</code> can be written more
10921cb0ef41Sopenharmony_cisuccinctly as <code>exports.f = ...</code>. However, be aware that like any variable, if a
10931cb0ef41Sopenharmony_cinew value is assigned to <code>exports</code>, it is no longer bound to <code>module.exports</code>:</p>
10941cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<span class="hljs-property">hello</span> = <span class="hljs-literal">true</span>; <span class="hljs-comment">// Exported from require of module</span>
10951cb0ef41Sopenharmony_ci<span class="hljs-built_in">exports</span> = { <span class="hljs-attr">hello</span>: <span class="hljs-literal">false</span> };  <span class="hljs-comment">// Not exported, only available in the module</span></code> <button class="copy-button">copy</button></pre>
10961cb0ef41Sopenharmony_ci<p>When the <code>module.exports</code> property is being completely replaced by a new
10971cb0ef41Sopenharmony_ciobject, it is common to also reassign <code>exports</code>:</p>
10981cb0ef41Sopenharmony_ci<!-- eslint-disable func-name-matching -->
10991cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-built_in">exports</span> = <span class="hljs-keyword">function</span> <span class="hljs-title function_">Constructor</span>(<span class="hljs-params"></span>) {
11001cb0ef41Sopenharmony_ci  <span class="hljs-comment">// ... etc.</span>
11011cb0ef41Sopenharmony_ci};</code> <button class="copy-button">copy</button></pre>
11021cb0ef41Sopenharmony_ci<p>To illustrate the behavior, imagine this hypothetical implementation of
11031cb0ef41Sopenharmony_ci<code>require()</code>, which is quite similar to what is actually done by <code>require()</code>:</p>
11041cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">require</span>(<span class="hljs-params"><span class="hljs-comment">/* ... */</span></span>) {
11051cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = { <span class="hljs-attr">exports</span>: {} };
11061cb0ef41Sopenharmony_ci  (<span class="hljs-function">(<span class="hljs-params"><span class="hljs-variable language_">module</span>, <span class="hljs-built_in">exports</span></span>) =></span> {
11071cb0ef41Sopenharmony_ci    <span class="hljs-comment">// Module code here. In this example, define a function.</span>
11081cb0ef41Sopenharmony_ci    <span class="hljs-keyword">function</span> <span class="hljs-title function_">someFunc</span>(<span class="hljs-params"></span>) {}
11091cb0ef41Sopenharmony_ci    <span class="hljs-built_in">exports</span> = someFunc;
11101cb0ef41Sopenharmony_ci    <span class="hljs-comment">// At this point, exports is no longer a shortcut to module.exports, and</span>
11111cb0ef41Sopenharmony_ci    <span class="hljs-comment">// this module will still export an empty default object.</span>
11121cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = someFunc;
11131cb0ef41Sopenharmony_ci    <span class="hljs-comment">// At this point, the module will now export someFunc, instead of the</span>
11141cb0ef41Sopenharmony_ci    <span class="hljs-comment">// default object.</span>
11151cb0ef41Sopenharmony_ci  })(<span class="hljs-variable language_">module</span>, <span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span>);
11161cb0ef41Sopenharmony_ci  <span class="hljs-keyword">return</span> <span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span>;
11171cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
11181cb0ef41Sopenharmony_ci<h4><code>module.filename</code><span><a class="mark" href="#modulefilename" id="modulefilename">#</a></span><a aria-hidden="true" class="legacy" id="modules_module_filename"></a></h4>
11191cb0ef41Sopenharmony_ci<div class="api_metadata">
11201cb0ef41Sopenharmony_ci<span>Added in: v0.1.16</span>
11211cb0ef41Sopenharmony_ci</div>
11221cb0ef41Sopenharmony_ci<ul>
11231cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
11241cb0ef41Sopenharmony_ci</ul>
11251cb0ef41Sopenharmony_ci<p>The fully resolved filename of the module.</p>
11261cb0ef41Sopenharmony_ci<h4><code>module.id</code><span><a class="mark" href="#moduleid" id="moduleid">#</a></span><a aria-hidden="true" class="legacy" id="modules_module_id"></a></h4>
11271cb0ef41Sopenharmony_ci<div class="api_metadata">
11281cb0ef41Sopenharmony_ci<span>Added in: v0.1.16</span>
11291cb0ef41Sopenharmony_ci</div>
11301cb0ef41Sopenharmony_ci<ul>
11311cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
11321cb0ef41Sopenharmony_ci</ul>
11331cb0ef41Sopenharmony_ci<p>The identifier for the module. Typically this is the fully resolved
11341cb0ef41Sopenharmony_cifilename.</p>
11351cb0ef41Sopenharmony_ci<h4><code>module.isPreloading</code><span><a class="mark" href="#moduleispreloading" id="moduleispreloading">#</a></span><a aria-hidden="true" class="legacy" id="modules_module_ispreloading"></a></h4>
11361cb0ef41Sopenharmony_ci<div class="api_metadata">
11371cb0ef41Sopenharmony_ci<span>Added in: v15.4.0, v14.17.0</span>
11381cb0ef41Sopenharmony_ci</div>
11391cb0ef41Sopenharmony_ci<ul>
11401cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if the module is running during the Node.js preload
11411cb0ef41Sopenharmony_ciphase.</li>
11421cb0ef41Sopenharmony_ci</ul>
11431cb0ef41Sopenharmony_ci<h4><code>module.loaded</code><span><a class="mark" href="#moduleloaded" id="moduleloaded">#</a></span><a aria-hidden="true" class="legacy" id="modules_module_loaded"></a></h4>
11441cb0ef41Sopenharmony_ci<div class="api_metadata">
11451cb0ef41Sopenharmony_ci<span>Added in: v0.1.16</span>
11461cb0ef41Sopenharmony_ci</div>
11471cb0ef41Sopenharmony_ci<ul>
11481cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
11491cb0ef41Sopenharmony_ci</ul>
11501cb0ef41Sopenharmony_ci<p>Whether or not the module is done loading, or is in the process of
11511cb0ef41Sopenharmony_ciloading.</p>
11521cb0ef41Sopenharmony_ci<h4><code>module.parent</code><span><a class="mark" href="#moduleparent" id="moduleparent">#</a></span><a aria-hidden="true" class="legacy" id="modules_module_parent"></a></h4>
11531cb0ef41Sopenharmony_ci<div class="api_metadata">
11541cb0ef41Sopenharmony_ci<span>Added in: v0.1.16</span><span>Deprecated since: v14.6.0, v12.19.0</span>
11551cb0ef41Sopenharmony_ci</div>
11561cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Please use <a href="#requiremain"><code>require.main</code></a> and
11571cb0ef41Sopenharmony_ci<a href="#modulechildren"><code>module.children</code></a> instead.</div><p></p>
11581cb0ef41Sopenharmony_ci<ul>
11591cb0ef41Sopenharmony_ci<li><a href="modules.html#the-module-object" class="type">&#x3C;module></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
11601cb0ef41Sopenharmony_ci</ul>
11611cb0ef41Sopenharmony_ci<p>The module that first required this one, or <code>null</code> if the current module is the
11621cb0ef41Sopenharmony_cientry point of the current process, or <code>undefined</code> if the module was loaded by
11631cb0ef41Sopenharmony_cisomething that is not a CommonJS module (E.G.: REPL or <code>import</code>).</p>
11641cb0ef41Sopenharmony_ci<h4><code>module.path</code><span><a class="mark" href="#modulepath" id="modulepath">#</a></span><a aria-hidden="true" class="legacy" id="modules_module_path"></a></h4>
11651cb0ef41Sopenharmony_ci<div class="api_metadata">
11661cb0ef41Sopenharmony_ci<span>Added in: v11.14.0</span>
11671cb0ef41Sopenharmony_ci</div>
11681cb0ef41Sopenharmony_ci<ul>
11691cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
11701cb0ef41Sopenharmony_ci</ul>
11711cb0ef41Sopenharmony_ci<p>The directory name of the module. This is usually the same as the
11721cb0ef41Sopenharmony_ci<a href="path.html#pathdirnamepath"><code>path.dirname()</code></a> of the <a href="#moduleid"><code>module.id</code></a>.</p>
11731cb0ef41Sopenharmony_ci<h4><code>module.paths</code><span><a class="mark" href="#modulepaths" id="modulepaths">#</a></span><a aria-hidden="true" class="legacy" id="modules_module_paths"></a></h4>
11741cb0ef41Sopenharmony_ci<div class="api_metadata">
11751cb0ef41Sopenharmony_ci<span>Added in: v0.4.0</span>
11761cb0ef41Sopenharmony_ci</div>
11771cb0ef41Sopenharmony_ci<ul>
11781cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
11791cb0ef41Sopenharmony_ci</ul>
11801cb0ef41Sopenharmony_ci<p>The search paths for the module.</p>
11811cb0ef41Sopenharmony_ci<h4><code>module.require(id)</code><span><a class="mark" href="#modulerequireid" id="modulerequireid">#</a></span><a aria-hidden="true" class="legacy" id="modules_module_require_id"></a></h4>
11821cb0ef41Sopenharmony_ci<div class="api_metadata">
11831cb0ef41Sopenharmony_ci<span>Added in: v0.5.1</span>
11841cb0ef41Sopenharmony_ci</div>
11851cb0ef41Sopenharmony_ci<ul>
11861cb0ef41Sopenharmony_ci<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
11871cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> exported module content</li>
11881cb0ef41Sopenharmony_ci</ul>
11891cb0ef41Sopenharmony_ci<p>The <code>module.require()</code> method provides a way to load a module as if
11901cb0ef41Sopenharmony_ci<code>require()</code> was called from the original module.</p>
11911cb0ef41Sopenharmony_ci<p>In order to do this, it is necessary to get a reference to the <code>module</code> object.
11921cb0ef41Sopenharmony_ciSince <code>require()</code> returns the <code>module.exports</code>, and the <code>module</code> is typically
11931cb0ef41Sopenharmony_ci<em>only</em> available within a specific module's code, it must be explicitly exported
11941cb0ef41Sopenharmony_ciin order to be used.</p>
11951cb0ef41Sopenharmony_ci</section><section><h3>The <code>Module</code> object<span><a class="mark" href="#the-module-object_1" id="the-module-object_1">#</a></span><a aria-hidden="true" class="legacy" id="modules_the_module_object_1"></a></h3>
11961cb0ef41Sopenharmony_ci<p>This section was moved to
11971cb0ef41Sopenharmony_ci<a href="module.html#the-module-object">Modules: <code>module</code> core module</a>.</p>
11981cb0ef41Sopenharmony_ci<!-- Anchors to make sure old links find a target -->
11991cb0ef41Sopenharmony_ci<ul>
12001cb0ef41Sopenharmony_ci<li><a id="modules_module_builtinmodules" href="module.html#modulebuiltinmodules"><code>module.builtinModules</code></a></li>
12011cb0ef41Sopenharmony_ci<li><a id="modules_module_createrequire_filename" href="module.html#modulecreaterequirefilename"><code>module.createRequire(filename)</code></a></li>
12021cb0ef41Sopenharmony_ci<li><a id="modules_module_syncbuiltinesmexports" href="module.html#modulesyncbuiltinesmexports"><code>module.syncBuiltinESMExports()</code></a></li>
12031cb0ef41Sopenharmony_ci</ul>
12041cb0ef41Sopenharmony_ci</section><section><h3>Source map v3 support<span><a class="mark" href="#source-map-v3-support" id="source-map-v3-support">#</a></span><a aria-hidden="true" class="legacy" id="modules_source_map_v3_support"></a></h3>
12051cb0ef41Sopenharmony_ci<p>This section was moved to
12061cb0ef41Sopenharmony_ci<a href="module.html#source-map-v3-support">Modules: <code>module</code> core module</a>.</p>
12071cb0ef41Sopenharmony_ci<!-- Anchors to make sure old links find a target -->
12081cb0ef41Sopenharmony_ci<ul>
12091cb0ef41Sopenharmony_ci<li><a id="modules_module_findsourcemap_path_error" href="module.html#modulefindsourcemappath"><code>module.findSourceMap(path)</code></a></li>
12101cb0ef41Sopenharmony_ci<li><a id="modules_class_module_sourcemap" href="module.html#class-modulesourcemap">Class: <code>module.SourceMap</code></a>
12111cb0ef41Sopenharmony_ci<ul>
12121cb0ef41Sopenharmony_ci<li><a id="modules_new_sourcemap_payload" href="module.html#new-sourcemappayload"><code>new SourceMap(payload)</code></a></li>
12131cb0ef41Sopenharmony_ci<li><a id="modules_sourcemap_payload" href="module.html#sourcemappayload"><code>sourceMap.payload</code></a></li>
12141cb0ef41Sopenharmony_ci<li><a id="modules_sourcemap_findentry_linenumber_columnnumber" href="module.html#sourcemapfindentrylinenumber-columnnumber"><code>sourceMap.findEntry(lineNumber, columnNumber)</code></a></li>
12151cb0ef41Sopenharmony_ci</ul>
12161cb0ef41Sopenharmony_ci</li>
12171cb0ef41Sopenharmony_ci</ul></section>
12181cb0ef41Sopenharmony_ci        <!-- API END -->
12191cb0ef41Sopenharmony_ci      </div>
12201cb0ef41Sopenharmony_ci    </div>
12211cb0ef41Sopenharmony_ci  </div>
12221cb0ef41Sopenharmony_ci</body>
12231cb0ef41Sopenharmony_ci</html>
1224