{
  "type": "module",
  "source": "doc/api/modules.md",
  "modules": [
    {
      "textRaw": "Modules: CommonJS modules",
      "name": "modules:_commonjs_modules",
      "introduced_in": "v0.10.0",
      "type": "module",
      "stability": 2,
      "stabilityText": "Stable",
      "desc": "<p>CommonJS modules are the original way to package JavaScript code for Node.js.\nNode.js also supports the <a href=\"esm.html\">ECMAScript modules</a> standard used by browsers\nand other JavaScript runtimes.</p>\n<p>In Node.js, each file is treated as a separate module. For\nexample, consider a file named <code>foo.js</code>:</p>\n<pre><code class=\"language-js\">const circle = require('./circle.js');\nconsole.log(`The area of a circle of radius 4 is ${circle.area(4)}`);\n</code></pre>\n<p>On the first line, <code>foo.js</code> loads the module <code>circle.js</code> that is in the same\ndirectory as <code>foo.js</code>.</p>\n<p>Here are the contents of <code>circle.js</code>:</p>\n<pre><code class=\"language-js\">const { PI } = Math;\n\nexports.area = (r) => PI * r ** 2;\n\nexports.circumference = (r) => 2 * PI * r;\n</code></pre>\n<p>The module <code>circle.js</code> has exported the functions <code>area()</code> and\n<code>circumference()</code>. Functions and objects are added to the root of a module\nby specifying additional properties on the special <code>exports</code> object.</p>\n<p>Variables local to the module will be private, because the module is wrapped\nin a function by Node.js (see <a href=\"#the-module-wrapper\">module wrapper</a>).\nIn this example, the variable <code>PI</code> is private to <code>circle.js</code>.</p>\n<p>The <code>module.exports</code> property can be assigned a new value (such as a function\nor object).</p>\n<p>In the following code, <code>bar.js</code> makes use of the <code>square</code> module, which exports\na Square class:</p>\n<pre><code class=\"language-js\">const Square = require('./square.js');\nconst mySquare = new Square(2);\nconsole.log(`The area of mySquare is ${mySquare.area()}`);\n</code></pre>\n<p>The <code>square</code> module is defined in <code>square.js</code>:</p>\n<pre><code class=\"language-js\">// Assigning to exports will not modify module, must use module.exports\nmodule.exports = class Square {\n  constructor(width) {\n    this.width = width;\n  }\n\n  area() {\n    return this.width ** 2;\n  }\n};\n</code></pre>\n<p>The CommonJS module system is implemented in the <a href=\"module.html\"><code>module</code> core module</a>.</p>",
      "miscs": [
        {
          "textRaw": "Enabling",
          "name": "Enabling",
          "type": "misc",
          "desc": "<p>Node.js has two module systems: CommonJS modules and <a href=\"esm.html\">ECMAScript modules</a>.</p>\n<p>By default, Node.js will treat the following as CommonJS modules:</p>\n<ul>\n<li>\n<p>Files with a <code>.cjs</code> extension.</p>\n</li>\n<li>\n<p>Files with a <code>.js</code> extension or without an extension, when the nearest parent\n<code>package.json</code> file contains a top-level field <a href=\"packages.html#type\"><code>\"type\"</code></a> with a value of\n<code>\"commonjs\"</code>.</p>\n</li>\n<li>\n<p>Files with a <code>.js</code> extension or without an extension, when the nearest parent\n<code>package.json</code> file doesn't contain a top-level field <a href=\"packages.html#type\"><code>\"type\"</code></a> or there is\nno <code>package.json</code> in any parent folder; unless the file contains syntax that\nerrors unless it is evaluated as an ES module. Package authors should include\nthe <a href=\"packages.html#type\"><code>\"type\"</code></a> field, even in packages where all sources are CommonJS. Being\nexplicit about the <code>type</code> of the package will make things easier for build\ntools and loaders to determine how the files in the package should be\ninterpreted.</p>\n</li>\n<li>\n<p>Files with an extension that is not <code>.mjs</code>, <code>.cjs</code>, <code>.json</code>, <code>.node</code>, or <code>.js</code>,\nwhen the nearest parent <code>package.json</code> file contains a top-level field\n<a href=\"packages.html#type\"><code>\"type\"</code></a> with a value of <code>\"module\"</code>.</p>\n</li>\n</ul>\n<p>See <a href=\"packages.html#determining-module-system\">Determining module system</a> for more details.</p>\n<p>Calling <code>require()</code> always use the CommonJS module loader. Calling <code>import()</code>\nalways use the ECMAScript module loader.</p>"
        },
        {
          "textRaw": "Accessing the main module",
          "name": "Accessing the main module",
          "type": "misc",
          "desc": "<p>When a file is run directly from Node.js, <code>require.main</code> is set to its\n<code>module</code>. That means that it is possible to determine whether a file has been\nrun directly by testing <code>require.main === module</code>.</p>\n<p>For a file <code>foo.js</code>, this will be <code>true</code> if run via <code>node foo.js</code>, but\n<code>false</code> if run by <code>require('./foo')</code>.</p>\n<p>When the entry point is not a CommonJS module, <code>require.main</code> is <code>undefined</code>,\nand the main module is out of reach.</p>"
        },
        {
          "textRaw": "Package manager tips",
          "name": "Package manager tips",
          "type": "misc",
          "desc": "<p>The semantics of the Node.js <code>require()</code> function were designed to be general\nenough to support reasonable directory structures. Package manager programs\nsuch as <code>dpkg</code>, <code>rpm</code>, and <code>npm</code> will hopefully find it possible to build\nnative packages from Node.js modules without modification.</p>\n<p>In the following, we give a suggested directory structure that could work:</p>\n<p>Let's say that we wanted to have the folder at\n<code>/usr/lib/node/&#x3C;some-package>/&#x3C;some-version></code> hold the contents of a\nspecific version of a package.</p>\n<p>Packages can depend on one another. In order to install package <code>foo</code>, it\nmay be necessary to install a specific version of package <code>bar</code>. The <code>bar</code>\npackage may itself have dependencies, and in some cases, these may even collide\nor form cyclic dependencies.</p>\n<p>Because Node.js looks up the <code>realpath</code> of any modules it loads (that is, it\nresolves symlinks) and then <a href=\"#loading-from-node_modules-folders\">looks for their dependencies in <code>node_modules</code> folders</a>,\nthis situation can be resolved with the following architecture:</p>\n<ul>\n<li><code>/usr/lib/node/foo/1.2.3/</code>: Contents of the <code>foo</code> package, version 1.2.3.</li>\n<li><code>/usr/lib/node/bar/4.3.2/</code>: Contents of the <code>bar</code> package that <code>foo</code> depends\non.</li>\n<li><code>/usr/lib/node/foo/1.2.3/node_modules/bar</code>: Symbolic link to\n<code>/usr/lib/node/bar/4.3.2/</code>.</li>\n<li><code>/usr/lib/node/bar/4.3.2/node_modules/*</code>: Symbolic links to the packages that\n<code>bar</code> depends on.</li>\n</ul>\n<p>Thus, even if a cycle is encountered, or if there are dependency\nconflicts, every module will be able to get a version of its dependency\nthat it can use.</p>\n<p>When the code in the <code>foo</code> package does <code>require('bar')</code>, it will get the\nversion that is symlinked into <code>/usr/lib/node/foo/1.2.3/node_modules/bar</code>.\nThen, when the code in the <code>bar</code> package calls <code>require('quux')</code>, it'll get\nthe version that is symlinked into\n<code>/usr/lib/node/bar/4.3.2/node_modules/quux</code>.</p>\n<p>Furthermore, to make the module lookup process even more optimal, rather\nthan putting packages directly in <code>/usr/lib/node</code>, we could put them in\n<code>/usr/lib/node_modules/&#x3C;name>/&#x3C;version></code>. Then Node.js will not bother\nlooking for missing dependencies in <code>/usr/node_modules</code> or <code>/node_modules</code>.</p>\n<p>In order to make modules available to the Node.js REPL, it might be useful to\nalso add the <code>/usr/lib/node_modules</code> folder to the <code>$NODE_PATH</code> environment\nvariable. Since the module lookups using <code>node_modules</code> folders are all\nrelative, and based on the real path of the files making the calls to\n<code>require()</code>, the packages themselves can be anywhere.</p>"
        },
        {
          "textRaw": "All together",
          "name": "All together",
          "type": "misc",
          "desc": "<p>To get the exact filename that will be loaded when <code>require()</code> is called, use\nthe <code>require.resolve()</code> function.</p>\n<p>Putting together all of the above, here is the high-level algorithm\nin pseudocode of what <code>require()</code> does:</p>\n<pre><code class=\"language-text\">require(X) from module at path Y\n1. If X is a core module,\n   a. return the core module\n   b. STOP\n2. If X begins with '/'\n   a. set Y to the file system root\n3. If X is equal to '.', or X begins with './', '/' or '../'\n   a. LOAD_AS_FILE(Y + X)\n   b. LOAD_AS_DIRECTORY(Y + X)\n   c. THROW \"not found\"\n4. If X begins with '#'\n   a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))\n5. LOAD_PACKAGE_SELF(X, dirname(Y))\n6. LOAD_NODE_MODULES(X, dirname(Y))\n7. THROW \"not found\"\n\nMAYBE_DETECT_AND_LOAD(X)\n1. If X parses as a CommonJS module, load X as a CommonJS module. STOP.\n2. Else, if the source code of X can be parsed as ECMAScript module using\n  &#x3C;a href=\"esm.md#resolver-algorithm-specification\">DETECT_MODULE_SYNTAX defined in\n  the ESM resolver&#x3C;/a>,\n  a. Load X as an ECMAScript module. STOP.\n3. THROW the SyntaxError from attempting to parse X as CommonJS in 1. STOP.\n\nLOAD_AS_FILE(X)\n1. If X is a file, load X as its file extension format. STOP\n2. If X.js is a file,\n    a. Find the closest package scope SCOPE to X.\n    b. If no scope was found\n      1. MAYBE_DETECT_AND_LOAD(X.js)\n    c. If the SCOPE/package.json contains \"type\" field,\n      1. If the \"type\" field is \"module\", load X.js as an ECMAScript module. STOP.\n      2. If the \"type\" field is \"commonjs\", load X.js as a CommonJS module. STOP.\n    d. MAYBE_DETECT_AND_LOAD(X.js)\n3. If X.json is a file, load X.json to a JavaScript Object. STOP\n4. If X.node is a file, load X.node as binary addon. STOP\n\nLOAD_INDEX(X)\n1. If X/index.js is a file\n    a. Find the closest package scope SCOPE to X.\n    b. If no scope was found, load X/index.js as a CommonJS module. STOP.\n    c. If the SCOPE/package.json contains \"type\" field,\n      1. If the \"type\" field is \"module\", load X/index.js as an ECMAScript module. STOP.\n      2. Else, load X/index.js as a CommonJS module. STOP.\n2. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP\n3. If X/index.node is a file, load X/index.node as binary addon. STOP\n\nLOAD_AS_DIRECTORY(X)\n1. If X/package.json is a file,\n   a. Parse X/package.json, and look for \"main\" field.\n   b. If \"main\" is a falsy value, GOTO 2.\n   c. let M = X + (json main field)\n   d. LOAD_AS_FILE(M)\n   e. LOAD_INDEX(M)\n   f. LOAD_INDEX(X) DEPRECATED\n   g. THROW \"not found\"\n2. LOAD_INDEX(X)\n\nLOAD_NODE_MODULES(X, START)\n1. let DIRS = NODE_MODULES_PATHS(START)\n2. for each DIR in DIRS:\n   a. LOAD_PACKAGE_EXPORTS(X, DIR)\n   b. LOAD_AS_FILE(DIR/X)\n   c. LOAD_AS_DIRECTORY(DIR/X)\n\nNODE_MODULES_PATHS(START)\n1. let PARTS = path split(START)\n2. let I = count of PARTS - 1\n3. let DIRS = []\n4. while I >= 0,\n   a. if PARTS[I] = \"node_modules\", GOTO d.\n   b. DIR = path join(PARTS[0 .. I] + \"node_modules\")\n   c. DIRS = DIRS + DIR\n   d. let I = I - 1\n5. return DIRS + GLOBAL_FOLDERS\n\nLOAD_PACKAGE_IMPORTS(X, DIR)\n1. Find the closest package scope SCOPE to DIR.\n2. If no scope was found, return.\n3. If the SCOPE/package.json \"imports\" is null or undefined, return.\n4. If `--no-require-module` is not enabled\n  a. let CONDITIONS = [\"node\", \"require\", \"module-sync\"]\n  b. Else, let CONDITIONS = [\"node\", \"require\"]\n5. let MATCH = PACKAGE_IMPORTS_RESOLVE(X, pathToFileURL(SCOPE),\n  CONDITIONS) &#x3C;a href=\"esm.md#resolver-algorithm-specification\">defined in the ESM resolver&#x3C;/a>.\n6. RESOLVE_ESM_MATCH(MATCH).\n\nLOAD_PACKAGE_EXPORTS(X, DIR)\n1. Try to interpret X as a combination of NAME and SUBPATH where the name\n   may have a @scope/ prefix and the subpath begins with a slash (`/`).\n2. If X does not match this pattern or DIR/NAME/package.json is not a file,\n   return.\n3. Parse DIR/NAME/package.json, and look for \"exports\" field.\n4. If \"exports\" is null or undefined, return.\n5. If `--no-require-module` is not enabled\n  a. let CONDITIONS = [\"node\", \"require\", \"module-sync\"]\n  b. Else, let CONDITIONS = [\"node\", \"require\"]\n6. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(DIR/NAME), \".\" + SUBPATH,\n   `package.json` \"exports\", CONDITIONS) &#x3C;a href=\"esm.md#resolver-algorithm-specification\">defined in the ESM resolver&#x3C;/a>.\n7. RESOLVE_ESM_MATCH(MATCH)\n\nLOAD_PACKAGE_SELF(X, DIR)\n1. Find the closest package scope SCOPE to DIR.\n2. If no scope was found, return.\n3. If the SCOPE/package.json \"exports\" is null or undefined, return.\n4. If the SCOPE/package.json \"name\" is not the first segment of X, return.\n5. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(SCOPE),\n   \".\" + X.slice(\"name\".length), `package.json` \"exports\", [\"node\", \"require\"])\n   &#x3C;a href=\"esm.md#resolver-algorithm-specification\">defined in the ESM resolver&#x3C;/a>.\n6. RESOLVE_ESM_MATCH(MATCH)\n\nRESOLVE_ESM_MATCH(MATCH)\n1. let RESOLVED_PATH = fileURLToPath(MATCH)\n2. If the file at RESOLVED_PATH exists, load RESOLVED_PATH as its extension\n   format. STOP\n3. THROW \"not found\"\n</code></pre>"
        },
        {
          "textRaw": "Caching",
          "name": "Caching",
          "type": "misc",
          "desc": "<p>Modules are cached after the first time they are loaded. This means (among other\nthings) that every call to <code>require('foo')</code> will get exactly the same object\nreturned, if it would resolve to the same file.</p>\n<p>Provided <code>require.cache</code> is not modified, multiple calls to <code>require('foo')</code>\nwill not cause the module code to be executed multiple times. This is an\nimportant feature. With it, \"partially done\" objects can be returned, thus\nallowing transitive dependencies to be loaded even when they would cause cycles.</p>\n<p>To have a module execute code multiple times, export a function, and call that\nfunction.</p>",
          "miscs": [
            {
              "textRaw": "Module caching caveats",
              "name": "Module caching caveats",
              "type": "misc",
              "desc": "<p>Modules are cached based on their resolved filename. Since modules may resolve\nto a different filename based on the location of the calling module (loading\nfrom <code>node_modules</code> folders), it is not a <em>guarantee</em> that <code>require('foo')</code> will\nalways return the exact same object, if it would resolve to different files.</p>\n<p>Additionally, on case-insensitive file systems or operating systems, different\nresolved filenames can point to the same file, but the cache will still treat\nthem as different modules and will reload the file multiple times. For example,\n<code>require('./foo')</code> and <code>require('./FOO')</code> return two different objects,\nirrespective of whether or not <code>./foo</code> and <code>./FOO</code> are the same file.</p>"
            }
          ]
        },
        {
          "textRaw": "Built-in modules",
          "name": "Built-in modules",
          "type": "misc",
          "meta": {
            "changes": [
              {
                "version": [
                  "v16.0.0",
                  "v14.18.0"
                ],
                "pr-url": "https://github.com/nodejs/node/pull/37246",
                "description": "Added `node:` import support to `require(...)`."
              }
            ]
          },
          "desc": "<p>Node.js has several modules compiled into the binary. These modules are\ndescribed in greater detail elsewhere in this documentation.</p>\n<p>The built-in modules are defined within the Node.js source and are located in the\n<code>lib/</code> folder.</p>\n<p>Built-in modules can be identified using the <code>node:</code> prefix, in which case\nit bypasses the <code>require</code> cache. For instance, <code>require('node:http')</code> will\nalways return the built in HTTP module, even if there is <code>require.cache</code> entry\nby that name.</p>\n<p>Some built-in modules are always preferentially loaded if their identifier is\npassed to <code>require()</code>. For instance, <code>require('http')</code> will always\nreturn the built-in HTTP module, even if there is a file by that name.</p>\n<p>The list of all the built-in modules can be retrieved from <a href=\"module.html#modulebuiltinmodules\"><code>module.builtinModules</code></a>.\nThe modules being all listed without the <code>node:</code> prefix, except those that mandate such\nprefix (as explained in the next section).</p>",
          "miscs": [
            {
              "textRaw": "Built-in modules with mandatory `node:` prefix",
              "name": "built-in_modules_with_mandatory_`node:`_prefix",
              "type": "misc",
              "desc": "<p>When being loaded by <code>require()</code>, some built-in modules must be requested with the\n<code>node:</code> prefix. This requirement exists to prevent newly introduced built-in\nmodules from having a conflict with user land packages that already have\ntaken the name. Currently the built-in modules that requires the <code>node:</code> prefix are:</p>\n<ul>\n<li><a href=\"ffi.html\"><code>node:ffi</code></a></li>\n<li><a href=\"single-executable-applications.html#single-executable-application-api\"><code>node:sea</code></a></li>\n<li><a href=\"sqlite.html\"><code>node:sqlite</code></a></li>\n<li><a href=\"test.html\"><code>node:test</code></a></li>\n<li><a href=\"test.html#test-reporters\"><code>node:test/reporters</code></a></li>\n</ul>\n<p>The list of these modules is exposed in <a href=\"module.html#modulebuiltinmodules\"><code>module.builtinModules</code></a>, including the prefix.</p>",
              "displayName": "Built-in modules with mandatory `node:` prefix"
            }
          ]
        },
        {
          "textRaw": "Cycles",
          "name": "Cycles",
          "type": "misc",
          "desc": "<p>When there are circular <code>require()</code> calls, a module might not have finished\nexecuting when it is returned.</p>\n<p>Consider this situation:</p>\n<p><code>a.js</code>:</p>\n<pre><code class=\"language-js\">console.log('a starting');\nexports.done = false;\nconst b = require('./b.js');\nconsole.log('in a, b.done = %j', b.done);\nexports.done = true;\nconsole.log('a done');\n</code></pre>\n<p><code>b.js</code>:</p>\n<pre><code class=\"language-js\">console.log('b starting');\nexports.done = false;\nconst a = require('./a.js');\nconsole.log('in b, a.done = %j', a.done);\nexports.done = true;\nconsole.log('b done');\n</code></pre>\n<p><code>main.js</code>:</p>\n<pre><code class=\"language-js\">console.log('main starting');\nconst a = require('./a.js');\nconst b = require('./b.js');\nconsole.log('in main, a.done = %j, b.done = %j', a.done, b.done);\n</code></pre>\n<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\npoint, <code>b.js</code> tries to load <code>a.js</code>. In order to prevent an infinite\nloop, an <strong>unfinished copy</strong> of the <code>a.js</code> exports object is returned to the\n<code>b.js</code> module. <code>b.js</code> then finishes loading, and its <code>exports</code> object is\nprovided to the <code>a.js</code> module.</p>\n<p>By the time <code>main.js</code> has loaded both modules, they're both finished.\nThe output of this program would thus be:</p>\n<pre><code class=\"language-console\">$ node main.js\nmain starting\na starting\nb starting\nin b, a.done = false\nb done\nin a, b.done = true\na done\nin main, a.done = true, b.done = true\n</code></pre>\n<p>Careful planning is required to allow cyclic module dependencies to work\ncorrectly within an application.</p>"
        },
        {
          "textRaw": "File modules",
          "name": "File modules",
          "type": "misc",
          "desc": "<p>If the exact filename is not found, then Node.js will attempt to load the\nrequired filename with the added extensions: <code>.js</code>, <code>.json</code>, and finally\n<code>.node</code>. When loading a file that has a different extension (e.g. <code>.cjs</code>), its\nfull name must be passed to <code>require()</code>, including its file extension (e.g.\n<code>require('./file.cjs')</code>).</p>\n<p><code>.json</code> files are parsed as JSON text files, <code>.node</code> files are interpreted as\ncompiled addon modules loaded with <code>process.dlopen()</code>. Files using any other\nextension (or no extension at all) are parsed as JavaScript text files. Refer to\nthe <a href=\"packages.html#determining-module-system\">Determining module system</a> section to understand what parse goal will be\nused.</p>\n<p>A required module prefixed with <code>'/'</code> is an absolute path to the file. For\nexample, <code>require('/home/marco/foo.js')</code> will load the file at\n<code>/home/marco/foo.js</code>.</p>\n<p>A required module prefixed with <code>'./'</code> is relative to the file calling\n<code>require()</code>. That is, <code>circle.js</code> must be in the same directory as <code>foo.js</code> for\n<code>require('./circle')</code> to find it.</p>\n<p>Without a leading <code>'/'</code>, <code>'./'</code>, or <code>'../'</code> to indicate a file, the module must\neither be a core module or is loaded from a <code>node_modules</code> folder.</p>\n<p>If the given path does not exist, <code>require()</code> will throw a\n<a href=\"errors.html#module_not_found\"><code>MODULE_NOT_FOUND</code></a> error.</p>"
        },
        {
          "textRaw": "Folders as modules",
          "name": "Folders as modules",
          "type": "misc",
          "stability": 3,
          "stabilityText": "Legacy: Use subpath exports or subpath imports instead.",
          "desc": "<p>There are three ways in which a folder may be passed to <code>require()</code> as\nan argument.</p>\n<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,\nwhich specifies a <code>main</code> module. An example <a href=\"packages.html#nodejs-packagejson-field-definitions\"><code>package.json</code></a> file might\nlook like this:</p>\n<pre><code class=\"language-json\">{ \"name\" : \"some-library\",\n  \"main\" : \"./lib/some-library.js\" }\n</code></pre>\n<p>If this was in a folder at <code>./some-library</code>, then\n<code>require('./some-library')</code> would attempt to load\n<code>./some-library/lib/some-library.js</code>.</p>\n<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\n<a href=\"packages.html#main\"><code>\"main\"</code></a> entry is missing or cannot be resolved, then Node.js\nwill attempt to load an <code>index.js</code> or <code>index.node</code> file out of that\ndirectory. For example, if there was no <a href=\"packages.html#nodejs-packagejson-field-definitions\"><code>package.json</code></a> file in the previous\nexample, then <code>require('./some-library')</code> would attempt to load:</p>\n<ul>\n<li><code>./some-library/index.js</code></li>\n<li><code>./some-library/index.node</code></li>\n</ul>\n<p>If these attempts fail, then Node.js will report the entire module as missing\nwith the default error:</p>\n<pre><code class=\"language-console\">Error: Cannot find module 'some-library'\n</code></pre>\n<p>In all three above cases, an <code>import('./some-library')</code> call would result in a\n<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\n<a href=\"packages.html#subpath-imports\">subpath imports</a> can provide the same containment organization benefits as\nfolders as modules, and work for both <code>require</code> and <code>import</code>.</p>"
        },
        {
          "textRaw": "Loading from `node_modules` folders",
          "name": "Loading from `node_modules` folders",
          "type": "misc",
          "desc": "<p>If the module identifier passed to <code>require()</code> is not a\n<a href=\"#built-in-modules\">built-in</a> module, and does not begin with <code>'/'</code>, <code>'../'</code>, or\n<code>'./'</code>, then Node.js starts at the directory of the current module, and\nadds <code>/node_modules</code>, and attempts to load the module from that location.\nNode.js will not append <code>node_modules</code> to a path already ending in\n<code>node_modules</code>.</p>\n<p>If it is not found there, then it moves to the parent directory, and so\non, until the root of the file system is reached.</p>\n<p>For example, if the file at <code>'/home/ry/projects/foo.js'</code> called\n<code>require('bar.js')</code>, then Node.js would look in the following locations, in\nthis order:</p>\n<ul>\n<li><code>/home/ry/projects/node_modules/bar.js</code></li>\n<li><code>/home/ry/node_modules/bar.js</code></li>\n<li><code>/home/node_modules/bar.js</code></li>\n<li><code>/node_modules/bar.js</code></li>\n</ul>\n<p>This allows programs to localize their dependencies, so that they do not\nclash.</p>\n<p>It is possible to require specific files or sub modules distributed with a\nmodule by including a path suffix after the module name. For instance\n<code>require('example-module/path/to/file')</code> would resolve <code>path/to/file</code>\nrelative to where <code>example-module</code> is located. The suffixed path follows the\nsame module resolution semantics.</p>"
        },
        {
          "textRaw": "Loading from the global folders",
          "name": "Loading from the global folders",
          "type": "misc",
          "desc": "<p>If the <code>NODE_PATH</code> environment variable is set to a colon-delimited list\nof absolute paths, then Node.js will search those paths for modules if they\nare not found elsewhere.</p>\n<p>On Windows, <code>NODE_PATH</code> is delimited by semicolons (<code>;</code>) instead of colons.</p>\n<p><code>NODE_PATH</code> was originally created to support loading modules from\nvarying paths before the current <a href=\"#all-together\">module resolution</a> algorithm was defined.</p>\n<p><code>NODE_PATH</code> is still supported, but is less necessary now that the Node.js\necosystem has settled on a convention for locating dependent modules.\nSometimes deployments that rely on <code>NODE_PATH</code> show surprising behavior\nwhen people are unaware that <code>NODE_PATH</code> must be set. Sometimes a\nmodule's dependencies change, causing a different version (or even a\ndifferent module) to be loaded as the <code>NODE_PATH</code> is searched.</p>\n<p>Additionally, Node.js will search in the following list of GLOBAL_FOLDERS:</p>\n<ul>\n<li>1: <code>$HOME/.node_modules</code></li>\n<li>2: <code>$HOME/.node_libraries</code></li>\n<li>3: <code>$PREFIX/lib/node</code></li>\n</ul>\n<p>Where <code>$HOME</code> is the user's home directory, and <code>$PREFIX</code> is the Node.js\nconfigured <code>node_prefix</code>.</p>\n<p>These are mostly for historic reasons.</p>\n<p>It is strongly encouraged to place dependencies in the local <code>node_modules</code>\nfolder. These will be loaded faster, and more reliably.</p>"
        },
        {
          "textRaw": "The module wrapper",
          "name": "The module wrapper",
          "type": "misc",
          "desc": "<p>Before a module's code is executed, Node.js will wrap it with a function\nwrapper that looks like the following:</p>\n<pre><code class=\"language-js\">(function(exports, require, module, __filename, __dirname) {\n// Module code actually lives in here\n});\n</code></pre>\n<p>By doing this, Node.js achieves a few things:</p>\n<ul>\n<li>It keeps top-level variables (defined with <code>var</code>, <code>const</code>, or <code>let</code>) scoped to\nthe module rather than the global object.</li>\n<li>It helps to provide some global-looking variables that are actually specific\nto the module, such as:\n<ul>\n<li>The <code>module</code> and <code>exports</code> objects that the implementor can use to export\nvalues from the module.</li>\n<li>The convenience variables <code>__filename</code> and <code>__dirname</code>, containing the\nmodule's absolute filename and directory path.</li>\n</ul>\n</li>\n</ul>"
        }
      ],
      "modules": [
        {
          "textRaw": "Loading ECMAScript modules using `require()`",
          "name": "loading_ecmascript_modules_using_`require()`",
          "type": "module",
          "meta": {
            "added": [
              "v22.0.0",
              "v20.17.0"
            ],
            "changes": [
              {
                "version": [
                  "v25.4.0"
                ],
                "pr-url": "https://github.com/nodejs/node/pull/60959",
                "description": "This feature is no longer experimental."
              },
              {
                "version": [
                  "v23.5.0",
                  "v22.13.0",
                  "v20.19.0"
                ],
                "pr-url": "https://github.com/nodejs/node/pull/56194",
                "description": "This feature no longer emits an experimental warning by default, though the warning can still be emitted by --trace-require-module."
              },
              {
                "version": [
                  "v23.0.0",
                  "v22.12.0",
                  "v20.19.0"
                ],
                "pr-url": "https://github.com/nodejs/node/pull/55085",
                "description": "This feature is no longer behind the `--experimental-require-module` CLI flag."
              },
              {
                "version": [
                  "v23.0.0",
                  "v22.12.0"
                ],
                "pr-url": "https://github.com/nodejs/node/pull/54563",
                "description": "Support `'module.exports'` interop export in `require(esm)`."
              }
            ]
          },
          "desc": "<p>The <code>.mjs</code> extension is reserved for <a href=\"esm.html\">ECMAScript Modules</a>.\nSee <a href=\"packages.html#determining-module-system\">Determining module system</a> section for more info\nregarding which files are parsed as ECMAScript modules.</p>\n<p><code>require()</code> only supports loading ECMAScript modules that meet the following requirements:</p>\n<ul>\n<li>The module is fully synchronous (contains no top-level <code>await</code>); and</li>\n<li>One of these conditions are met:\n<ol>\n<li>The file has a <code>.mjs</code> extension.</li>\n<li>The file has a <code>.js</code> extension, and the closest <code>package.json</code> contains <code>\"type\": \"module\"</code></li>\n<li>The file has a <code>.js</code> extension, the closest <code>package.json</code> does not contain\n<code>\"type\": \"commonjs\"</code>, and the module contains ES module syntax.</li>\n</ol>\n</li>\n</ul>\n<p>If the ES Module being loaded meets the requirements, <code>require()</code> can load it and\nreturn the <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import#module_namespace_object\">module namespace object</a>. In this case it is similar to dynamic\n<code>import()</code> but is run synchronously and returns the name space object\ndirectly.</p>\n<p>With the following ES Modules:</p>\n<pre><code class=\"language-mjs\">// distance.mjs\nexport function distance(a, b) { return Math.sqrt((b.x - a.x) ** 2 + (b.y - a.y) ** 2); }\n</code></pre>\n<pre><code class=\"language-mjs\">// point.mjs\nexport default class Point {\n  constructor(x, y) { this.x = x; this.y = y; }\n}\n</code></pre>\n<p>A CommonJS module can load them with <code>require()</code>:</p>\n<pre><code class=\"language-cjs\">const distance = require('./distance.mjs');\nconsole.log(distance);\n// [Module: null prototype] {\n//   distance: [Function: distance]\n// }\n\nconst point = require('./point.mjs');\nconsole.log(point);\n// [Module: null prototype] {\n//   default: [class Point],\n//   __esModule: true,\n// }\n</code></pre>\n<p>For interoperability with existing tools that convert ES Modules into CommonJS,\nwhich could then load real ES Modules through <code>require()</code>, the returned namespace\nwould contain a <code>__esModule: true</code> property if it has a <code>default</code> export so that\nconsuming code generated by tools can recognize the default exports in real\nES Modules. If the namespace already defines <code>__esModule</code>, this would not be added.\nThis property is experimental and can change in the future. It should only be used\nby tools converting ES modules into CommonJS modules, following existing ecosystem\nconventions. Code authored directly in CommonJS should avoid depending on it.</p>\n<p>The result returned by <code>require()</code> is the <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import#module_namespace_object\">module namespace object</a>, which places\nthe default export in the <code>.default</code> property, similar to the results returned by <code>import()</code>.\nTo customize what should be returned by <code>require(esm)</code> directly, the ES Module can export the\ndesired value using the string name <code>\"module.exports\"</code>.</p>\n<pre><code class=\"language-mjs\">// point.mjs\nexport default class Point {\n  constructor(x, y) { this.x = x; this.y = y; }\n}\n\n// `distance` is lost to CommonJS consumers of this module, unless it's\n// added to `Point` as a static property.\nexport function distance(a, b) { return Math.sqrt((b.x - a.x) ** 2 + (b.y - a.y) ** 2); }\nexport { Point as 'module.exports' };\n</code></pre>\n<pre><code class=\"language-cjs\">const Point = require('./point.mjs');\nconsole.log(Point); // [class Point]\n\n// Named exports are lost when 'module.exports' is used\nconst { distance } = require('./point.mjs');\nconsole.log(distance); // undefined\n</code></pre>\n<p>Notice in the example above, when the <code>module.exports</code> export name is used, named exports\nwill be lost to CommonJS consumers. To allow  CommonJS consumers to continue accessing\nnamed exports, the module can make sure that the default export is an object with the\nnamed exports attached to it as properties. For example with the example above,\n<code>distance</code> can be attached to the default export, the <code>Point</code> class, as a static method.</p>\n<pre><code class=\"language-mjs\">export function distance(a, b) { return Math.sqrt((b.x - a.x) ** 2 + (b.y - a.y) ** 2); }\n\nexport default class Point {\n  constructor(x, y) { this.x = x; this.y = y; }\n  static distance = distance;\n}\n\nexport { Point as 'module.exports' };\n</code></pre>\n<pre><code class=\"language-cjs\">const Point = require('./point.mjs');\nconsole.log(Point); // [class Point]\n\nconst { distance } = require('./point.mjs');\nconsole.log(distance); // [Function: distance]\n</code></pre>\n<p>If the module being <code>require()</code>'d contains top-level <code>await</code>, or the module\ngraph it <code>import</code>s contains top-level <code>await</code>,\n<a href=\"errors.html#err_require_async_module\"><code>ERR_REQUIRE_ASYNC_MODULE</code></a> will be thrown. In this case, users should\nload the asynchronous module using <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import\"><code>import()</code></a>.</p>\n<p>If <code>--experimental-print-required-tla</code> is enabled, instead of throwing\n<code>ERR_REQUIRE_ASYNC_MODULE</code> before evaluation, Node.js will evaluate the\nmodule, try to locate the top-level awaits, and print their location to\nhelp users fix them.</p>\n<p>If support for loading ES modules using <code>require()</code> results in unexpected\nbreakage, it can be disabled using <code>--no-require-module</code>.\nTo print where this feature is used, use <a href=\"cli.html#--trace-require-modulemode\"><code>--trace-require-module</code></a>.</p>\n<p>This feature can be detected by checking if\n<a href=\"process.html#processfeaturesrequire_module\"><code>process.features.require_module</code></a> is <code>true</code>.</p>",
          "displayName": "Loading ECMAScript modules using `require()`"
        },
        {
          "textRaw": "The module scope",
          "name": "the_module_scope",
          "type": "module",
          "modules": [
            {
              "textRaw": "`__dirname`",
              "name": "`__dirname`",
              "type": "module",
              "meta": {
                "added": [
                  "v0.1.27"
                ],
                "changes": []
              },
              "desc": "<ul>\n<li>Type: <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type\"><code>&#x3C;string></code></a></li>\n</ul>\n<p>The directory name of the current module. This is the same as the\n<a href=\"path.html#pathdirnamepath\"><code>path.dirname()</code></a> of the <a href=\"#__filename\"><code>__filename</code></a>.</p>\n<p>Example: running <code>node example.js</code> from <code>/Users/mjr</code></p>\n<pre><code class=\"language-js\">console.log(__dirname);\n// Prints: /Users/mjr\nconsole.log(path.dirname(__filename));\n// Prints: /Users/mjr\n</code></pre>",
              "displayName": "`__dirname`"
            },
            {
              "textRaw": "`__filename`",
              "name": "`__filename`",
              "type": "module",
              "meta": {
                "added": [
                  "v0.0.1"
                ],
                "changes": []
              },
              "desc": "<ul>\n<li>Type: <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type\"><code>&#x3C;string></code></a></li>\n</ul>\n<p>The file name of the current module. This is the current module file's absolute\npath with symlinks resolved.</p>\n<p>For a main program this is not necessarily the same as the file name used in the\ncommand line.</p>\n<p>See <a href=\"#__dirname\"><code>__dirname</code></a> for the directory name of the current module.</p>\n<p>Examples:</p>\n<p>Running <code>node example.js</code> from <code>/Users/mjr</code></p>\n<pre><code class=\"language-js\">console.log(__filename);\n// Prints: /Users/mjr/example.js\nconsole.log(__dirname);\n// Prints: /Users/mjr\n</code></pre>\n<p>Given two modules: <code>a</code> and <code>b</code>, where <code>b</code> is a dependency of\n<code>a</code> and there is a directory structure of:</p>\n<ul>\n<li><code>/Users/mjr/app/a.js</code></li>\n<li><code>/Users/mjr/app/node_modules/b/b.js</code></li>\n</ul>\n<p>References to <code>__filename</code> within <code>b.js</code> will return\n<code>/Users/mjr/app/node_modules/b/b.js</code> while references to <code>__filename</code> within\n<code>a.js</code> will return <code>/Users/mjr/app/a.js</code>.</p>",
              "displayName": "`__filename`"
            },
            {
              "textRaw": "`exports`",
              "name": "`exports`",
              "type": "module",
              "meta": {
                "added": [
                  "v0.1.12"
                ],
                "changes": []
              },
              "desc": "<ul>\n<li>Type: <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object\"><code>&#x3C;Object></code></a></li>\n</ul>\n<p>A reference to the <code>module.exports</code> that is shorter to type.\nSee the section about the <a href=\"#exports-shortcut\">exports shortcut</a> for details on when to use\n<code>exports</code> and when to use <code>module.exports</code>.</p>",
              "displayName": "`exports`"
            },
            {
              "textRaw": "`module`",
              "name": "`module`",
              "type": "module",
              "meta": {
                "added": [
                  "v0.1.16"
                ],
                "changes": []
              },
              "desc": "<ul>\n<li>Type: <a href=\"modules.html#the-module-object\"><code>&#x3C;module></code></a></li>\n</ul>\n<p>A reference to the current module, see the section about the\n<a href=\"#the-module-object\"><code>module</code> object</a>. In particular, <code>module.exports</code> is used for defining what\na module exports and makes available through <code>require()</code>.</p>",
              "displayName": "`module`"
            }
          ],
          "methods": [
            {
              "textRaw": "`require(id)`",
              "name": "require",
              "type": "method",
              "meta": {
                "added": [
                  "v0.1.13"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`id` {string} module name or path",
                      "name": "id",
                      "type": "string",
                      "desc": "module name or path"
                    }
                  ],
                  "return": {
                    "textRaw": "Returns: {any} exported module content",
                    "name": "return",
                    "type": "any",
                    "desc": "exported module content"
                  }
                }
              ],
              "desc": "<p>Used to import modules, <code>JSON</code>, and local files. Modules can be imported\nfrom <code>node_modules</code>. Local modules and JSON files can be imported using\na relative path (e.g. <code>./</code>, <code>./foo</code>, <code>./bar/baz</code>, <code>../foo</code>) that will be\nresolved against the directory named by <a href=\"#__dirname\"><code>__dirname</code></a> (if defined) or\nthe current working directory. The relative paths of POSIX style are resolved\nin an OS independent fashion, meaning that the examples above will work on\nWindows in the same way they would on Unix systems.</p>\n<pre><code class=\"language-js\">// Importing a local module with a path relative to the `__dirname` or current\n// working directory. (On Windows, this would resolve to .\\path\\myLocalModule.)\nconst myLocalModule = require('./path/myLocalModule');\n\n// Importing a JSON file:\nconst jsonData = require('./path/filename.json');\n\n// Importing a module from node_modules or Node.js built-in module:\nconst crypto = require('node:crypto');\n</code></pre>",
              "properties": [
                {
                  "textRaw": "Type: {Object}",
                  "name": "cache",
                  "type": "Object",
                  "meta": {
                    "added": [
                      "v0.3.0"
                    ],
                    "changes": []
                  },
                  "desc": "<p>Modules are cached in this object when they are required. By deleting a key\nvalue from this object, the next <code>require</code> will reload the module.\nThis does not apply to <a href=\"addons.html\">native addons</a>, for which reloading will result in an\nerror.</p>\n<p>Adding or replacing entries is also possible. This cache is checked before\nbuilt-in modules and if a name matching a built-in module is added to the cache,\nonly <code>node:</code>-prefixed require calls are going to receive the built-in module.\nUse with care!</p>\n<pre><code class=\"language-js\">const assert = require('node:assert');\nconst realFs = require('node:fs');\n\nconst fakeFs = {};\nrequire.cache.fs = { exports: fakeFs };\n\nassert.strictEqual(require('fs'), fakeFs);\nassert.strictEqual(require('node:fs'), realFs);\n</code></pre>"
                },
                {
                  "textRaw": "Type: {Object}",
                  "name": "extensions",
                  "type": "Object",
                  "meta": {
                    "added": [
                      "v0.3.0"
                    ],
                    "changes": [],
                    "deprecated": [
                      "v0.10.6"
                    ]
                  },
                  "stability": 0,
                  "stabilityText": "Deprecated",
                  "desc": "<p>Instruct <code>require</code> on how to handle certain file extensions.</p>\n<p>Process files with the extension <code>.sjs</code> as <code>.js</code>:</p>\n<pre><code class=\"language-js\">require.extensions['.sjs'] = require.extensions['.js'];\n</code></pre>\n<p><strong>Deprecated.</strong> In the past, this list has been used to load non-JavaScript\nmodules into Node.js by compiling them on-demand. However, in practice, there\nare much better ways to do this, such as loading modules via some other Node.js\nprogram, or compiling them to JavaScript ahead of time.</p>\n<p>Avoid using <code>require.extensions</code>. Use could cause subtle bugs and resolving the\nextensions gets slower with each registered extension.</p>"
                },
                {
                  "textRaw": "Type: {module|undefined}",
                  "name": "main",
                  "type": "module|undefined",
                  "meta": {
                    "added": [
                      "v0.1.17"
                    ],
                    "changes": []
                  },
                  "desc": "<p>The <code>Module</code> object representing the entry script loaded when the Node.js\nprocess launched, or <code>undefined</code> if the entry point of the program is not a\nCommonJS module.\nSee <a href=\"#accessing-the-main-module\">\"Accessing the main module\"</a>.</p>\n<p>In <code>entry.js</code> script:</p>\n<pre><code class=\"language-js\">console.log(require.main);\n</code></pre>\n<pre><code class=\"language-bash\">node entry.js\n</code></pre>\n<pre><code class=\"language-js\">Module {\n  id: '.',\n  path: '/absolute/path/to',\n  exports: {},\n  filename: '/absolute/path/to/entry.js',\n  loaded: false,\n  children: [],\n  paths:\n   [ '/absolute/path/to/node_modules',\n     '/absolute/path/node_modules',\n     '/absolute/node_modules',\n     '/node_modules' ] }\n</code></pre>"
                }
              ],
              "methods": [
                {
                  "textRaw": "`require.resolve(request[, options])`",
                  "name": "resolve",
                  "type": "method",
                  "meta": {
                    "added": [
                      "v0.3.0"
                    ],
                    "changes": [
                      {
                        "version": "v8.9.0",
                        "pr-url": "https://github.com/nodejs/node/pull/16397",
                        "description": "The `paths` option is now supported."
                      }
                    ]
                  },
                  "signatures": [
                    {
                      "params": [
                        {
                          "textRaw": "`request` {string} The module path to resolve.",
                          "name": "request",
                          "type": "string",
                          "desc": "The module path to resolve."
                        },
                        {
                          "textRaw": "`options` {Object}",
                          "name": "options",
                          "type": "Object",
                          "options": [
                            {
                              "textRaw": "`paths` {string[]} Paths to resolve module location from. If present, these paths are used instead of the default resolution paths, with the exception of GLOBAL_FOLDERS like `$HOME/.node_modules`, which are always included. Each of these paths is used as a starting point for the module resolution algorithm, meaning that the `node_modules` hierarchy is checked from this location.",
                              "name": "paths",
                              "type": "string[]",
                              "desc": "Paths to resolve module location from. If present, these paths are used instead of the default resolution paths, with the exception of GLOBAL_FOLDERS like `$HOME/.node_modules`, which are always included. Each of these paths is used as a starting point for the module resolution algorithm, meaning that the `node_modules` hierarchy is checked from this location."
                            }
                          ],
                          "optional": true
                        }
                      ],
                      "return": {
                        "textRaw": "Returns: {string}",
                        "name": "return",
                        "type": "string"
                      }
                    }
                  ],
                  "desc": "<p>Use the internal <code>require()</code> machinery to look up the location of a module,\nbut rather than loading the module, just return the resolved filename.</p>\n<p>If the module can not be found, a <code>MODULE_NOT_FOUND</code> error is thrown.</p>",
                  "methods": [
                    {
                      "textRaw": "`require.resolve.paths(request)`",
                      "name": "paths",
                      "type": "method",
                      "meta": {
                        "added": [
                          "v8.9.0"
                        ],
                        "changes": []
                      },
                      "signatures": [
                        {
                          "params": [
                            {
                              "textRaw": "`request` {string} The module path whose lookup paths are being retrieved.",
                              "name": "request",
                              "type": "string",
                              "desc": "The module path whose lookup paths are being retrieved."
                            }
                          ],
                          "return": {
                            "textRaw": "Returns: {string[]|null}",
                            "name": "return",
                            "type": "string[]|null"
                          }
                        }
                      ],
                      "desc": "<p>Returns an array containing the paths searched during resolution of <code>request</code> or\n<code>null</code> if the <code>request</code> string references a core module, for example <code>http</code> or\n<code>fs</code>.</p>"
                    }
                  ]
                }
              ]
            }
          ],
          "displayName": "The module scope"
        },
        {
          "textRaw": "The `module` object",
          "name": "the_`module`_object",
          "type": "module",
          "meta": {
            "added": [
              "v0.1.16"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li>Type: <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object\"><code>&#x3C;Object></code></a></li>\n</ul>\n<p>In each module, the <code>module</code> free variable is a reference to the object\nrepresenting the current module. For convenience, <code>module.exports</code> is\nalso accessible via the <code>exports</code> module-global. <code>module</code> is not actually\na global but rather local to each module.</p>",
          "properties": [
            {
              "textRaw": "Type: {module[]}",
              "name": "children",
              "type": "module[]",
              "meta": {
                "added": [
                  "v0.1.16"
                ],
                "changes": []
              },
              "desc": "<p>The module objects required for the first time by this one.</p>"
            },
            {
              "textRaw": "Type: {Object}",
              "name": "exports",
              "type": "Object",
              "meta": {
                "added": [
                  "v0.1.16"
                ],
                "changes": []
              },
              "desc": "<p>The <code>module.exports</code> object is created by the <code>Module</code> system. Sometimes this is\nnot acceptable; many want their module to be an instance of some class. To do\nthis, assign the desired export object to <code>module.exports</code>. Assigning\nthe desired object to <code>exports</code> will simply rebind the local <code>exports</code> variable,\nwhich is probably not what is desired.</p>\n<p>For example, suppose we were making a module called <code>a.js</code>:</p>\n<pre><code class=\"language-js\">const EventEmitter = require('node:events');\n\nmodule.exports = new EventEmitter();\n\n// Do some work, and after some time emit\n// the 'ready' event from the module itself.\nsetTimeout(() => {\n  module.exports.emit('ready');\n}, 1000);\n</code></pre>\n<p>Then in another file we could do:</p>\n<pre><code class=\"language-js\">const a = require('./a');\na.on('ready', () => {\n  console.log('module \"a\" is ready');\n});\n</code></pre>\n<p>Assignment to <code>module.exports</code> must be done immediately. It cannot be\ndone in any callbacks. This does not work:</p>\n<p><code>x.js</code>:</p>\n<pre><code class=\"language-js\">setTimeout(() => {\n  module.exports = { a: 'hello' };\n}, 0);\n</code></pre>\n<p><code>y.js</code>:</p>\n<pre><code class=\"language-js\">const x = require('./x');\nconsole.log(x.a);\n</code></pre>",
              "modules": [
                {
                  "textRaw": "`exports` shortcut",
                  "name": "`exports`_shortcut",
                  "type": "module",
                  "meta": {
                    "added": [
                      "v0.1.16"
                    ],
                    "changes": []
                  },
                  "desc": "<p>The <code>exports</code> variable is available within a module's file-level scope, and is\nassigned the value of <code>module.exports</code> before the module is evaluated.</p>\n<p>It allows a shortcut, so that <code>module.exports.f = ...</code> can be written more\nsuccinctly as <code>exports.f = ...</code>. However, be aware that like any variable, if a\nnew value is assigned to <code>exports</code>, it is no longer bound to <code>module.exports</code>:</p>\n<pre><code class=\"language-js\">module.exports.hello = true; // Exported from require of module\nexports = { hello: false };  // Not exported, only available in the module\n</code></pre>\n<p>When the <code>module.exports</code> property is being completely replaced by a new\nobject, it is common to also reassign <code>exports</code>:</p>\n<pre><code class=\"language-js\">module.exports = exports = function Constructor() {\n  // ... etc.\n};\n</code></pre>\n<p>To illustrate the behavior, imagine this hypothetical implementation of\n<code>require()</code>, which is quite similar to what is actually done by <code>require()</code>:</p>\n<pre><code class=\"language-js\">function require(/* ... */) {\n  const module = { exports: {} };\n  ((module, exports) => {\n    // Module code here. In this example, define a function.\n    function someFunc() {}\n    exports = someFunc;\n    // At this point, exports is no longer a shortcut to module.exports, and\n    // this module will still export an empty default object.\n    module.exports = someFunc;\n    // At this point, the module will now export someFunc, instead of the\n    // default object.\n  })(module, module.exports);\n  return module.exports;\n}\n</code></pre>",
                  "displayName": "`exports` shortcut"
                }
              ]
            },
            {
              "textRaw": "Type: {string}",
              "name": "filename",
              "type": "string",
              "meta": {
                "added": [
                  "v0.1.16"
                ],
                "changes": []
              },
              "desc": "<p>The fully resolved filename of the module.</p>"
            },
            {
              "textRaw": "Type: {string}",
              "name": "id",
              "type": "string",
              "meta": {
                "added": [
                  "v0.1.16"
                ],
                "changes": []
              },
              "desc": "<p>The identifier for the module. Typically this is the fully resolved\nfilename.</p>"
            },
            {
              "textRaw": "Type: {boolean} `true` if the module is running during the Node.js preload phase.",
              "name": "isPreloading",
              "type": "boolean",
              "meta": {
                "added": [
                  "v15.4.0",
                  "v14.17.0"
                ],
                "changes": []
              },
              "desc": "`true` if the module is running during the Node.js preload phase."
            },
            {
              "textRaw": "Type: {boolean}",
              "name": "loaded",
              "type": "boolean",
              "meta": {
                "added": [
                  "v0.1.16"
                ],
                "changes": []
              },
              "desc": "<p>Whether or not the module is done loading, or is in the process of\nloading.</p>"
            },
            {
              "textRaw": "Type: {module|null|undefined}",
              "name": "parent",
              "type": "module|null|undefined",
              "meta": {
                "added": [
                  "v0.1.16"
                ],
                "changes": [],
                "deprecated": [
                  "v14.6.0",
                  "v12.19.0"
                ]
              },
              "stability": 0,
              "stabilityText": "Deprecated: Please use `require.main` and `module.children` instead.",
              "desc": "<p>The module that first required this one, or <code>null</code> if the current module is the\nentry point of the current process, or <code>undefined</code> if the module was loaded by\nsomething that is not a CommonJS module (E.G.: REPL or <code>import</code>).</p>"
            },
            {
              "textRaw": "Type: {string}",
              "name": "path",
              "type": "string",
              "meta": {
                "added": [
                  "v11.14.0"
                ],
                "changes": []
              },
              "desc": "<p>The directory name of the module. This is usually the same as the\n<a href=\"path.html#pathdirnamepath\"><code>path.dirname()</code></a> of the <a href=\"#moduleid\"><code>module.id</code></a>.</p>"
            },
            {
              "textRaw": "Type: {string[]}",
              "name": "paths",
              "type": "string[]",
              "meta": {
                "added": [
                  "v0.4.0"
                ],
                "changes": []
              },
              "desc": "<p>The search paths for the module.</p>"
            }
          ],
          "methods": [
            {
              "textRaw": "`module.require(id)`",
              "name": "require",
              "type": "method",
              "meta": {
                "added": [
                  "v0.5.1"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`id` {string}",
                      "name": "id",
                      "type": "string"
                    }
                  ],
                  "return": {
                    "textRaw": "Returns: {any} exported module content",
                    "name": "return",
                    "type": "any",
                    "desc": "exported module content"
                  }
                }
              ],
              "desc": "<p>The <code>module.require()</code> method provides a way to load a module as if\n<code>require()</code> was called from the original module.</p>\n<p>In order to do this, it is necessary to get a reference to the <code>module</code> object.\nSince <code>require()</code> returns the <code>module.exports</code>, and the <code>module</code> is typically\n<em>only</em> available within a specific module's code, it must be explicitly exported\nin order to be used.</p>"
            }
          ],
          "displayName": "The `module` object"
        },
        {
          "textRaw": "The `Module` object",
          "name": "the_`module`_object",
          "type": "module",
          "desc": "<p>This section was moved to\n<a href=\"module.html#the-module-object\">Modules: <code>module</code> core module</a>.</p>\n<ul>\n<li><a id=\"modules_module_builtinmodules\" href=\"module.html#modulebuiltinmodules\"><code>module.builtinModules</code></a></li>\n<li><a id=\"modules_module_createrequire_filename\" href=\"module.html#modulecreaterequirefilename\"><code>module.createRequire(filename)</code></a></li>\n<li><a id=\"modules_module_syncbuiltinesmexports\" href=\"module.html#modulesyncbuiltinesmexports\"><code>module.syncBuiltinESMExports()</code></a></li>\n</ul>",
          "displayName": "The `Module` object"
        },
        {
          "textRaw": "Source map v3 support",
          "name": "source_map_v3_support",
          "type": "module",
          "desc": "<p>This section was moved to\n<a href=\"module.html#source-map-support\">Modules: <code>module</code> core module</a>.</p>\n<ul>\n<li><a id=\"modules_module_findsourcemap_path_error\" href=\"module.html#modulefindsourcemappath\"><code>module.findSourceMap(path)</code></a></li>\n<li><a id=\"modules_class_module_sourcemap\" href=\"module.html#class-modulesourcemap\">Class: <code>module.SourceMap</code></a>\n<ul>\n<li><a id=\"modules_new_sourcemap_payload\" href=\"module.html#new-sourcemappayload--linelengths-\"><code>new SourceMap(payload)</code></a></li>\n<li><a id=\"modules_sourcemap_payload\" href=\"module.html#sourcemappayload\"><code>sourceMap.payload</code></a></li>\n<li><a id=\"modules_sourcemap_findentry_linenumber_columnnumber\" href=\"module.html#sourcemapfindentrylineoffset-columnoffset\"><code>sourceMap.findEntry(lineNumber, columnNumber)</code></a></li>\n</ul>\n</li>\n</ul>",
          "displayName": "Source map v3 support"
        }
      ],
      "meta": {
        "changes": [
          {
            "version": [
              "v16.0.0",
              "v14.18.0"
            ],
            "pr-url": "https://github.com/nodejs/node/pull/37246",
            "description": "Added `node:` import support to `require(...)`."
          }
        ]
      },
      "displayName": "Modules: CommonJS modules"
    }
  ]
}