xref: /third_party/node/doc/api/modules.md (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci# Modules: CommonJS modules
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci> Stability: 2 - Stable
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!--name=module-->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciCommonJS modules are the original way to package JavaScript code for Node.js.
101cb0ef41Sopenharmony_ciNode.js also supports the [ECMAScript modules][] standard used by browsers
111cb0ef41Sopenharmony_ciand other JavaScript runtimes.
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciIn Node.js, each file is treated as a separate module. For
141cb0ef41Sopenharmony_ciexample, consider a file named `foo.js`:
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci```js
171cb0ef41Sopenharmony_ciconst circle = require('./circle.js');
181cb0ef41Sopenharmony_ciconsole.log(`The area of a circle of radius 4 is ${circle.area(4)}`);
191cb0ef41Sopenharmony_ci```
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciOn the first line, `foo.js` loads the module `circle.js` that is in the same
221cb0ef41Sopenharmony_cidirectory as `foo.js`.
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciHere are the contents of `circle.js`:
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci```js
271cb0ef41Sopenharmony_ciconst { PI } = Math;
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciexports.area = (r) => PI * r ** 2;
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciexports.circumference = (r) => 2 * PI * r;
321cb0ef41Sopenharmony_ci```
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciThe module `circle.js` has exported the functions `area()` and
351cb0ef41Sopenharmony_ci`circumference()`. Functions and objects are added to the root of a module
361cb0ef41Sopenharmony_ciby specifying additional properties on the special `exports` object.
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciVariables local to the module will be private, because the module is wrapped
391cb0ef41Sopenharmony_ciin a function by Node.js (see [module wrapper](#the-module-wrapper)).
401cb0ef41Sopenharmony_ciIn this example, the variable `PI` is private to `circle.js`.
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciThe `module.exports` property can be assigned a new value (such as a function
431cb0ef41Sopenharmony_cior object).
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciBelow, `bar.js` makes use of the `square` module, which exports a Square class:
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci```js
481cb0ef41Sopenharmony_ciconst Square = require('./square.js');
491cb0ef41Sopenharmony_ciconst mySquare = new Square(2);
501cb0ef41Sopenharmony_ciconsole.log(`The area of mySquare is ${mySquare.area()}`);
511cb0ef41Sopenharmony_ci```
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ciThe `square` module is defined in `square.js`:
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci```js
561cb0ef41Sopenharmony_ci// Assigning to exports will not modify module, must use module.exports
571cb0ef41Sopenharmony_cimodule.exports = class Square {
581cb0ef41Sopenharmony_ci  constructor(width) {
591cb0ef41Sopenharmony_ci    this.width = width;
601cb0ef41Sopenharmony_ci  }
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  area() {
631cb0ef41Sopenharmony_ci    return this.width ** 2;
641cb0ef41Sopenharmony_ci  }
651cb0ef41Sopenharmony_ci};
661cb0ef41Sopenharmony_ci```
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciThe CommonJS module system is implemented in the [`module` core module][].
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci## Enabling
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci<!-- type=misc -->
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ciNode.js has two module systems: CommonJS modules and [ECMAScript modules][].
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ciBy default, Node.js will treat the following as CommonJS modules:
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci* Files with a `.cjs` extension;
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci* Files with a `.js` extension when the nearest parent `package.json` file
811cb0ef41Sopenharmony_ci  contains a top-level field [`"type"`][] with a value of `"commonjs"`.
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci* Files with a `.js` extension when the nearest parent `package.json` file
841cb0ef41Sopenharmony_ci  doesn't contain a top-level field [`"type"`][]. Package authors should include
851cb0ef41Sopenharmony_ci  the [`"type"`][] field, even in packages where all sources are CommonJS. Being
861cb0ef41Sopenharmony_ci  explicit about the `type` of the package will make things easier for build
871cb0ef41Sopenharmony_ci  tools and loaders to determine how the files in the package should be
881cb0ef41Sopenharmony_ci  interpreted.
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci* Files with an extension that is not `.mjs`, `.cjs`, `.json`, `.node`, or `.js`
911cb0ef41Sopenharmony_ci  (when the nearest parent `package.json` file contains a top-level field
921cb0ef41Sopenharmony_ci  [`"type"`][] with a value of `"module"`, those files will be recognized as
931cb0ef41Sopenharmony_ci  CommonJS modules only if they are being included via `require()`, not when
941cb0ef41Sopenharmony_ci  used as the command-line entry point of the program).
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ciSee [Determining module system][] for more details.
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ciCalling `require()` always use the CommonJS module loader. Calling `import()`
991cb0ef41Sopenharmony_cialways use the ECMAScript module loader.
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci## Accessing the main module
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci<!-- type=misc -->
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ciWhen a file is run directly from Node.js, `require.main` is set to its
1061cb0ef41Sopenharmony_ci`module`. That means that it is possible to determine whether a file has been
1071cb0ef41Sopenharmony_cirun directly by testing `require.main === module`.
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ciFor a file `foo.js`, this will be `true` if run via `node foo.js`, but
1101cb0ef41Sopenharmony_ci`false` if run by `require('./foo')`.
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ciWhen the entry point is not a CommonJS module, `require.main` is `undefined`,
1131cb0ef41Sopenharmony_ciand the main module is out of reach.
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci## Package manager tips
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci<!-- type=misc -->
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ciThe semantics of the Node.js `require()` function were designed to be general
1201cb0ef41Sopenharmony_cienough to support reasonable directory structures. Package manager programs
1211cb0ef41Sopenharmony_cisuch as `dpkg`, `rpm`, and `npm` will hopefully find it possible to build
1221cb0ef41Sopenharmony_cinative packages from Node.js modules without modification.
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ciBelow we give a suggested directory structure that could work:
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ciLet's say that we wanted to have the folder at
1271cb0ef41Sopenharmony_ci`/usr/lib/node/<some-package>/<some-version>` hold the contents of a
1281cb0ef41Sopenharmony_cispecific version of a package.
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ciPackages can depend on one another. In order to install package `foo`, it
1311cb0ef41Sopenharmony_cimay be necessary to install a specific version of package `bar`. The `bar`
1321cb0ef41Sopenharmony_cipackage may itself have dependencies, and in some cases, these may even collide
1331cb0ef41Sopenharmony_cior form cyclic dependencies.
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ciBecause Node.js looks up the `realpath` of any modules it loads (that is, it
1361cb0ef41Sopenharmony_ciresolves symlinks) and then [looks for their dependencies in `node_modules` folders](#loading-from-node_modules-folders),
1371cb0ef41Sopenharmony_cithis situation can be resolved with the following architecture:
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci* `/usr/lib/node/foo/1.2.3/`: Contents of the `foo` package, version 1.2.3.
1401cb0ef41Sopenharmony_ci* `/usr/lib/node/bar/4.3.2/`: Contents of the `bar` package that `foo` depends
1411cb0ef41Sopenharmony_ci  on.
1421cb0ef41Sopenharmony_ci* `/usr/lib/node/foo/1.2.3/node_modules/bar`: Symbolic link to
1431cb0ef41Sopenharmony_ci  `/usr/lib/node/bar/4.3.2/`.
1441cb0ef41Sopenharmony_ci* `/usr/lib/node/bar/4.3.2/node_modules/*`: Symbolic links to the packages that
1451cb0ef41Sopenharmony_ci  `bar` depends on.
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ciThus, even if a cycle is encountered, or if there are dependency
1481cb0ef41Sopenharmony_ciconflicts, every module will be able to get a version of its dependency
1491cb0ef41Sopenharmony_cithat it can use.
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ciWhen the code in the `foo` package does `require('bar')`, it will get the
1521cb0ef41Sopenharmony_civersion that is symlinked into `/usr/lib/node/foo/1.2.3/node_modules/bar`.
1531cb0ef41Sopenharmony_ciThen, when the code in the `bar` package calls `require('quux')`, it'll get
1541cb0ef41Sopenharmony_cithe version that is symlinked into
1551cb0ef41Sopenharmony_ci`/usr/lib/node/bar/4.3.2/node_modules/quux`.
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ciFurthermore, to make the module lookup process even more optimal, rather
1581cb0ef41Sopenharmony_cithan putting packages directly in `/usr/lib/node`, we could put them in
1591cb0ef41Sopenharmony_ci`/usr/lib/node_modules/<name>/<version>`. Then Node.js will not bother
1601cb0ef41Sopenharmony_cilooking for missing dependencies in `/usr/node_modules` or `/node_modules`.
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ciIn order to make modules available to the Node.js REPL, it might be useful to
1631cb0ef41Sopenharmony_cialso add the `/usr/lib/node_modules` folder to the `$NODE_PATH` environment
1641cb0ef41Sopenharmony_civariable. Since the module lookups using `node_modules` folders are all
1651cb0ef41Sopenharmony_cirelative, and based on the real path of the files making the calls to
1661cb0ef41Sopenharmony_ci`require()`, the packages themselves can be anywhere.
1671cb0ef41Sopenharmony_ci
1681cb0ef41Sopenharmony_ci## The `.mjs` extension
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ciDue to the synchronous nature of `require()`, it is not possible to use it to
1711cb0ef41Sopenharmony_ciload ECMAScript module files. Attempting to do so will throw a
1721cb0ef41Sopenharmony_ci[`ERR_REQUIRE_ESM`][] error. Use [`import()`][] instead.
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ciThe `.mjs` extension is reserved for [ECMAScript Modules][] which cannot be
1751cb0ef41Sopenharmony_ciloaded via `require()`. See [Determining module system][] section for more info
1761cb0ef41Sopenharmony_ciregarding which files are parsed as ECMAScript modules.
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci## All together
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci<!-- type=misc -->
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ciTo get the exact filename that will be loaded when `require()` is called, use
1831cb0ef41Sopenharmony_cithe `require.resolve()` function.
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ciPutting together all of the above, here is the high-level algorithm
1861cb0ef41Sopenharmony_ciin pseudocode of what `require()` does:
1871cb0ef41Sopenharmony_ci
1881cb0ef41Sopenharmony_ci<pre>
1891cb0ef41Sopenharmony_cirequire(X) from module at path Y
1901cb0ef41Sopenharmony_ci1. If X is a core module,
1911cb0ef41Sopenharmony_ci   a. return the core module
1921cb0ef41Sopenharmony_ci   b. STOP
1931cb0ef41Sopenharmony_ci2. If X begins with '/'
1941cb0ef41Sopenharmony_ci   a. set Y to be the file system root
1951cb0ef41Sopenharmony_ci3. If X begins with './' or '/' or '../'
1961cb0ef41Sopenharmony_ci   a. LOAD_AS_FILE(Y + X)
1971cb0ef41Sopenharmony_ci   b. LOAD_AS_DIRECTORY(Y + X)
1981cb0ef41Sopenharmony_ci   c. THROW "not found"
1991cb0ef41Sopenharmony_ci4. If X begins with '#'
2001cb0ef41Sopenharmony_ci   a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))
2011cb0ef41Sopenharmony_ci5. LOAD_PACKAGE_SELF(X, dirname(Y))
2021cb0ef41Sopenharmony_ci6. LOAD_NODE_MODULES(X, dirname(Y))
2031cb0ef41Sopenharmony_ci7. THROW "not found"
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ciLOAD_AS_FILE(X)
2061cb0ef41Sopenharmony_ci1. If X is a file, load X as its file extension format. STOP
2071cb0ef41Sopenharmony_ci2. If X.js is a file, load X.js as JavaScript text. STOP
2081cb0ef41Sopenharmony_ci3. If X.json is a file, parse X.json to a JavaScript Object. STOP
2091cb0ef41Sopenharmony_ci4. If X.node is a file, load X.node as binary addon. STOP
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ciLOAD_INDEX(X)
2121cb0ef41Sopenharmony_ci1. If X/index.js is a file, load X/index.js as JavaScript text. STOP
2131cb0ef41Sopenharmony_ci2. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
2141cb0ef41Sopenharmony_ci3. If X/index.node is a file, load X/index.node as binary addon. STOP
2151cb0ef41Sopenharmony_ci
2161cb0ef41Sopenharmony_ciLOAD_AS_DIRECTORY(X)
2171cb0ef41Sopenharmony_ci1. If X/package.json is a file,
2181cb0ef41Sopenharmony_ci   a. Parse X/package.json, and look for "main" field.
2191cb0ef41Sopenharmony_ci   b. If "main" is a falsy value, GOTO 2.
2201cb0ef41Sopenharmony_ci   c. let M = X + (json main field)
2211cb0ef41Sopenharmony_ci   d. LOAD_AS_FILE(M)
2221cb0ef41Sopenharmony_ci   e. LOAD_INDEX(M)
2231cb0ef41Sopenharmony_ci   f. LOAD_INDEX(X) DEPRECATED
2241cb0ef41Sopenharmony_ci   g. THROW "not found"
2251cb0ef41Sopenharmony_ci2. LOAD_INDEX(X)
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ciLOAD_NODE_MODULES(X, START)
2281cb0ef41Sopenharmony_ci1. let DIRS = NODE_MODULES_PATHS(START)
2291cb0ef41Sopenharmony_ci2. for each DIR in DIRS:
2301cb0ef41Sopenharmony_ci   a. LOAD_PACKAGE_EXPORTS(X, DIR)
2311cb0ef41Sopenharmony_ci   b. LOAD_AS_FILE(DIR/X)
2321cb0ef41Sopenharmony_ci   c. LOAD_AS_DIRECTORY(DIR/X)
2331cb0ef41Sopenharmony_ci
2341cb0ef41Sopenharmony_ciNODE_MODULES_PATHS(START)
2351cb0ef41Sopenharmony_ci1. let PARTS = path split(START)
2361cb0ef41Sopenharmony_ci2. let I = count of PARTS - 1
2371cb0ef41Sopenharmony_ci3. let DIRS = []
2381cb0ef41Sopenharmony_ci4. while I >= 0,
2391cb0ef41Sopenharmony_ci   a. if PARTS[I] = "node_modules" CONTINUE
2401cb0ef41Sopenharmony_ci   b. DIR = path join(PARTS[0 .. I] + "node_modules")
2411cb0ef41Sopenharmony_ci   c. DIRS = DIR + DIRS
2421cb0ef41Sopenharmony_ci   d. let I = I - 1
2431cb0ef41Sopenharmony_ci5. return DIRS + GLOBAL_FOLDERS
2441cb0ef41Sopenharmony_ci
2451cb0ef41Sopenharmony_ciLOAD_PACKAGE_IMPORTS(X, DIR)
2461cb0ef41Sopenharmony_ci1. Find the closest package scope SCOPE to DIR.
2471cb0ef41Sopenharmony_ci2. If no scope was found, return.
2481cb0ef41Sopenharmony_ci3. If the SCOPE/package.json "imports" is null or undefined, return.
2491cb0ef41Sopenharmony_ci4. let MATCH = PACKAGE_IMPORTS_RESOLVE(X, pathToFileURL(SCOPE),
2501cb0ef41Sopenharmony_ci  ["node", "require"]) <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
2511cb0ef41Sopenharmony_ci5. RESOLVE_ESM_MATCH(MATCH).
2521cb0ef41Sopenharmony_ci
2531cb0ef41Sopenharmony_ciLOAD_PACKAGE_EXPORTS(X, DIR)
2541cb0ef41Sopenharmony_ci1. Try to interpret X as a combination of NAME and SUBPATH where the name
2551cb0ef41Sopenharmony_ci   may have a @scope/ prefix and the subpath begins with a slash (`/`).
2561cb0ef41Sopenharmony_ci2. If X does not match this pattern or DIR/NAME/package.json is not a file,
2571cb0ef41Sopenharmony_ci   return.
2581cb0ef41Sopenharmony_ci3. Parse DIR/NAME/package.json, and look for "exports" field.
2591cb0ef41Sopenharmony_ci4. If "exports" is null or undefined, return.
2601cb0ef41Sopenharmony_ci5. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(DIR/NAME), "." + SUBPATH,
2611cb0ef41Sopenharmony_ci   `package.json` "exports", ["node", "require"]) <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
2621cb0ef41Sopenharmony_ci6. RESOLVE_ESM_MATCH(MATCH)
2631cb0ef41Sopenharmony_ci
2641cb0ef41Sopenharmony_ciLOAD_PACKAGE_SELF(X, DIR)
2651cb0ef41Sopenharmony_ci1. Find the closest package scope SCOPE to DIR.
2661cb0ef41Sopenharmony_ci2. If no scope was found, return.
2671cb0ef41Sopenharmony_ci3. If the SCOPE/package.json "exports" is null or undefined, return.
2681cb0ef41Sopenharmony_ci4. If the SCOPE/package.json "name" is not the first segment of X, return.
2691cb0ef41Sopenharmony_ci5. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(SCOPE),
2701cb0ef41Sopenharmony_ci   "." + X.slice("name".length), `package.json` "exports", ["node", "require"])
2711cb0ef41Sopenharmony_ci   <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
2721cb0ef41Sopenharmony_ci6. RESOLVE_ESM_MATCH(MATCH)
2731cb0ef41Sopenharmony_ci
2741cb0ef41Sopenharmony_ciRESOLVE_ESM_MATCH(MATCH)
2751cb0ef41Sopenharmony_ci1. let RESOLVED_PATH = fileURLToPath(MATCH)
2761cb0ef41Sopenharmony_ci2. If the file at RESOLVED_PATH exists, load RESOLVED_PATH as its extension
2771cb0ef41Sopenharmony_ci   format. STOP
2781cb0ef41Sopenharmony_ci3. THROW "not found"
2791cb0ef41Sopenharmony_ci</pre>
2801cb0ef41Sopenharmony_ci
2811cb0ef41Sopenharmony_ci## Caching
2821cb0ef41Sopenharmony_ci
2831cb0ef41Sopenharmony_ci<!--type=misc-->
2841cb0ef41Sopenharmony_ci
2851cb0ef41Sopenharmony_ciModules are cached after the first time they are loaded. This means (among other
2861cb0ef41Sopenharmony_cithings) that every call to `require('foo')` will get exactly the same object
2871cb0ef41Sopenharmony_cireturned, if it would resolve to the same file.
2881cb0ef41Sopenharmony_ci
2891cb0ef41Sopenharmony_ciProvided `require.cache` is not modified, multiple calls to `require('foo')`
2901cb0ef41Sopenharmony_ciwill not cause the module code to be executed multiple times. This is an
2911cb0ef41Sopenharmony_ciimportant feature. With it, "partially done" objects can be returned, thus
2921cb0ef41Sopenharmony_ciallowing transitive dependencies to be loaded even when they would cause cycles.
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ciTo have a module execute code multiple times, export a function, and call that
2951cb0ef41Sopenharmony_cifunction.
2961cb0ef41Sopenharmony_ci
2971cb0ef41Sopenharmony_ci### Module caching caveats
2981cb0ef41Sopenharmony_ci
2991cb0ef41Sopenharmony_ci<!--type=misc-->
3001cb0ef41Sopenharmony_ci
3011cb0ef41Sopenharmony_ciModules are cached based on their resolved filename. Since modules may resolve
3021cb0ef41Sopenharmony_cito a different filename based on the location of the calling module (loading
3031cb0ef41Sopenharmony_cifrom `node_modules` folders), it is not a _guarantee_ that `require('foo')` will
3041cb0ef41Sopenharmony_cialways return the exact same object, if it would resolve to different files.
3051cb0ef41Sopenharmony_ci
3061cb0ef41Sopenharmony_ciAdditionally, on case-insensitive file systems or operating systems, different
3071cb0ef41Sopenharmony_ciresolved filenames can point to the same file, but the cache will still treat
3081cb0ef41Sopenharmony_cithem as different modules and will reload the file multiple times. For example,
3091cb0ef41Sopenharmony_ci`require('./foo')` and `require('./FOO')` return two different objects,
3101cb0ef41Sopenharmony_ciirrespective of whether or not `./foo` and `./FOO` are the same file.
3111cb0ef41Sopenharmony_ci
3121cb0ef41Sopenharmony_ci## Core modules
3131cb0ef41Sopenharmony_ci
3141cb0ef41Sopenharmony_ci<!--type=misc-->
3151cb0ef41Sopenharmony_ci
3161cb0ef41Sopenharmony_ci<!-- YAML
3171cb0ef41Sopenharmony_cichanges:
3181cb0ef41Sopenharmony_ci  - version:
3191cb0ef41Sopenharmony_ci      - v16.0.0
3201cb0ef41Sopenharmony_ci      - v14.18.0
3211cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/37246
3221cb0ef41Sopenharmony_ci    description: Added `node:` import support to `require(...)`.
3231cb0ef41Sopenharmony_ci-->
3241cb0ef41Sopenharmony_ci
3251cb0ef41Sopenharmony_ciNode.js has several modules compiled into the binary. These modules are
3261cb0ef41Sopenharmony_cidescribed in greater detail elsewhere in this documentation.
3271cb0ef41Sopenharmony_ci
3281cb0ef41Sopenharmony_ciThe core modules are defined within the Node.js source and are located in the
3291cb0ef41Sopenharmony_ci`lib/` folder.
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_ciCore modules can be identified using the `node:` prefix, in which case
3321cb0ef41Sopenharmony_ciit bypasses the `require` cache. For instance, `require('node:http')` will
3331cb0ef41Sopenharmony_cialways return the built in HTTP module, even if there is `require.cache` entry
3341cb0ef41Sopenharmony_ciby that name.
3351cb0ef41Sopenharmony_ci
3361cb0ef41Sopenharmony_ciSome core modules are always preferentially loaded if their identifier is
3371cb0ef41Sopenharmony_cipassed to `require()`. For instance, `require('http')` will always
3381cb0ef41Sopenharmony_cireturn the built-in HTTP module, even if there is a file by that name. The list
3391cb0ef41Sopenharmony_ciof core modules that can be loaded without using the `node:` prefix is exposed
3401cb0ef41Sopenharmony_cias [`module.builtinModules`][].
3411cb0ef41Sopenharmony_ci
3421cb0ef41Sopenharmony_ci## Cycles
3431cb0ef41Sopenharmony_ci
3441cb0ef41Sopenharmony_ci<!--type=misc-->
3451cb0ef41Sopenharmony_ci
3461cb0ef41Sopenharmony_ciWhen there are circular `require()` calls, a module might not have finished
3471cb0ef41Sopenharmony_ciexecuting when it is returned.
3481cb0ef41Sopenharmony_ci
3491cb0ef41Sopenharmony_ciConsider this situation:
3501cb0ef41Sopenharmony_ci
3511cb0ef41Sopenharmony_ci`a.js`:
3521cb0ef41Sopenharmony_ci
3531cb0ef41Sopenharmony_ci```js
3541cb0ef41Sopenharmony_ciconsole.log('a starting');
3551cb0ef41Sopenharmony_ciexports.done = false;
3561cb0ef41Sopenharmony_ciconst b = require('./b.js');
3571cb0ef41Sopenharmony_ciconsole.log('in a, b.done = %j', b.done);
3581cb0ef41Sopenharmony_ciexports.done = true;
3591cb0ef41Sopenharmony_ciconsole.log('a done');
3601cb0ef41Sopenharmony_ci```
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_ci`b.js`:
3631cb0ef41Sopenharmony_ci
3641cb0ef41Sopenharmony_ci```js
3651cb0ef41Sopenharmony_ciconsole.log('b starting');
3661cb0ef41Sopenharmony_ciexports.done = false;
3671cb0ef41Sopenharmony_ciconst a = require('./a.js');
3681cb0ef41Sopenharmony_ciconsole.log('in b, a.done = %j', a.done);
3691cb0ef41Sopenharmony_ciexports.done = true;
3701cb0ef41Sopenharmony_ciconsole.log('b done');
3711cb0ef41Sopenharmony_ci```
3721cb0ef41Sopenharmony_ci
3731cb0ef41Sopenharmony_ci`main.js`:
3741cb0ef41Sopenharmony_ci
3751cb0ef41Sopenharmony_ci```js
3761cb0ef41Sopenharmony_ciconsole.log('main starting');
3771cb0ef41Sopenharmony_ciconst a = require('./a.js');
3781cb0ef41Sopenharmony_ciconst b = require('./b.js');
3791cb0ef41Sopenharmony_ciconsole.log('in main, a.done = %j, b.done = %j', a.done, b.done);
3801cb0ef41Sopenharmony_ci```
3811cb0ef41Sopenharmony_ci
3821cb0ef41Sopenharmony_ciWhen `main.js` loads `a.js`, then `a.js` in turn loads `b.js`. At that
3831cb0ef41Sopenharmony_cipoint, `b.js` tries to load `a.js`. In order to prevent an infinite
3841cb0ef41Sopenharmony_ciloop, an **unfinished copy** of the `a.js` exports object is returned to the
3851cb0ef41Sopenharmony_ci`b.js` module. `b.js` then finishes loading, and its `exports` object is
3861cb0ef41Sopenharmony_ciprovided to the `a.js` module.
3871cb0ef41Sopenharmony_ci
3881cb0ef41Sopenharmony_ciBy the time `main.js` has loaded both modules, they're both finished.
3891cb0ef41Sopenharmony_ciThe output of this program would thus be:
3901cb0ef41Sopenharmony_ci
3911cb0ef41Sopenharmony_ci```console
3921cb0ef41Sopenharmony_ci$ node main.js
3931cb0ef41Sopenharmony_cimain starting
3941cb0ef41Sopenharmony_cia starting
3951cb0ef41Sopenharmony_cib starting
3961cb0ef41Sopenharmony_ciin b, a.done = false
3971cb0ef41Sopenharmony_cib done
3981cb0ef41Sopenharmony_ciin a, b.done = true
3991cb0ef41Sopenharmony_cia done
4001cb0ef41Sopenharmony_ciin main, a.done = true, b.done = true
4011cb0ef41Sopenharmony_ci```
4021cb0ef41Sopenharmony_ci
4031cb0ef41Sopenharmony_ciCareful planning is required to allow cyclic module dependencies to work
4041cb0ef41Sopenharmony_cicorrectly within an application.
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_ci## File modules
4071cb0ef41Sopenharmony_ci
4081cb0ef41Sopenharmony_ci<!--type=misc-->
4091cb0ef41Sopenharmony_ci
4101cb0ef41Sopenharmony_ciIf the exact filename is not found, then Node.js will attempt to load the
4111cb0ef41Sopenharmony_cirequired filename with the added extensions: `.js`, `.json`, and finally
4121cb0ef41Sopenharmony_ci`.node`. When loading a file that has a different extension (e.g. `.cjs`), its
4131cb0ef41Sopenharmony_cifull name must be passed to `require()`, including its file extension (e.g.
4141cb0ef41Sopenharmony_ci`require('./file.cjs')`).
4151cb0ef41Sopenharmony_ci
4161cb0ef41Sopenharmony_ci`.json` files are parsed as JSON text files, `.node` files are interpreted as
4171cb0ef41Sopenharmony_cicompiled addon modules loaded with `process.dlopen()`. Files using any other
4181cb0ef41Sopenharmony_ciextension (or no extension at all) are parsed as JavaScript text files. Refer to
4191cb0ef41Sopenharmony_cithe [Determining module system][] section to understand what parse goal will be
4201cb0ef41Sopenharmony_ciused.
4211cb0ef41Sopenharmony_ci
4221cb0ef41Sopenharmony_ciA required module prefixed with `'/'` is an absolute path to the file. For
4231cb0ef41Sopenharmony_ciexample, `require('/home/marco/foo.js')` will load the file at
4241cb0ef41Sopenharmony_ci`/home/marco/foo.js`.
4251cb0ef41Sopenharmony_ci
4261cb0ef41Sopenharmony_ciA required module prefixed with `'./'` is relative to the file calling
4271cb0ef41Sopenharmony_ci`require()`. That is, `circle.js` must be in the same directory as `foo.js` for
4281cb0ef41Sopenharmony_ci`require('./circle')` to find it.
4291cb0ef41Sopenharmony_ci
4301cb0ef41Sopenharmony_ciWithout a leading `'/'`, `'./'`, or `'../'` to indicate a file, the module must
4311cb0ef41Sopenharmony_cieither be a core module or is loaded from a `node_modules` folder.
4321cb0ef41Sopenharmony_ci
4331cb0ef41Sopenharmony_ciIf the given path does not exist, `require()` will throw a
4341cb0ef41Sopenharmony_ci[`MODULE_NOT_FOUND`][] error.
4351cb0ef41Sopenharmony_ci
4361cb0ef41Sopenharmony_ci## Folders as modules
4371cb0ef41Sopenharmony_ci
4381cb0ef41Sopenharmony_ci<!--type=misc-->
4391cb0ef41Sopenharmony_ci
4401cb0ef41Sopenharmony_ci> Stability: 3 - Legacy: Use [subpath exports][] or [subpath imports][] instead.
4411cb0ef41Sopenharmony_ci
4421cb0ef41Sopenharmony_ciThere are three ways in which a folder may be passed to `require()` as
4431cb0ef41Sopenharmony_cian argument.
4441cb0ef41Sopenharmony_ci
4451cb0ef41Sopenharmony_ciThe first is to create a [`package.json`][] file in the root of the folder,
4461cb0ef41Sopenharmony_ciwhich specifies a `main` module. An example [`package.json`][] file might
4471cb0ef41Sopenharmony_cilook like this:
4481cb0ef41Sopenharmony_ci
4491cb0ef41Sopenharmony_ci```json
4501cb0ef41Sopenharmony_ci{ "name" : "some-library",
4511cb0ef41Sopenharmony_ci  "main" : "./lib/some-library.js" }
4521cb0ef41Sopenharmony_ci```
4531cb0ef41Sopenharmony_ci
4541cb0ef41Sopenharmony_ciIf this was in a folder at `./some-library`, then
4551cb0ef41Sopenharmony_ci`require('./some-library')` would attempt to load
4561cb0ef41Sopenharmony_ci`./some-library/lib/some-library.js`.
4571cb0ef41Sopenharmony_ci
4581cb0ef41Sopenharmony_ciIf there is no [`package.json`][] file present in the directory, or if the
4591cb0ef41Sopenharmony_ci[`"main"`][] entry is missing or cannot be resolved, then Node.js
4601cb0ef41Sopenharmony_ciwill attempt to load an `index.js` or `index.node` file out of that
4611cb0ef41Sopenharmony_cidirectory. For example, if there was no [`package.json`][] file in the previous
4621cb0ef41Sopenharmony_ciexample, then `require('./some-library')` would attempt to load:
4631cb0ef41Sopenharmony_ci
4641cb0ef41Sopenharmony_ci* `./some-library/index.js`
4651cb0ef41Sopenharmony_ci* `./some-library/index.node`
4661cb0ef41Sopenharmony_ci
4671cb0ef41Sopenharmony_ciIf these attempts fail, then Node.js will report the entire module as missing
4681cb0ef41Sopenharmony_ciwith the default error:
4691cb0ef41Sopenharmony_ci
4701cb0ef41Sopenharmony_ci```console
4711cb0ef41Sopenharmony_ciError: Cannot find module 'some-library'
4721cb0ef41Sopenharmony_ci```
4731cb0ef41Sopenharmony_ci
4741cb0ef41Sopenharmony_ciIn all three above cases, an `import('./some-library')` call would result in a
4751cb0ef41Sopenharmony_ci[`ERR_UNSUPPORTED_DIR_IMPORT`][] error. Using package [subpath exports][] or
4761cb0ef41Sopenharmony_ci[subpath imports][] can provide the same containment organization benefits as
4771cb0ef41Sopenharmony_cifolders as modules, and work for both `require` and `import`.
4781cb0ef41Sopenharmony_ci
4791cb0ef41Sopenharmony_ci## Loading from `node_modules` folders
4801cb0ef41Sopenharmony_ci
4811cb0ef41Sopenharmony_ci<!--type=misc-->
4821cb0ef41Sopenharmony_ci
4831cb0ef41Sopenharmony_ciIf the module identifier passed to `require()` is not a
4841cb0ef41Sopenharmony_ci[core](#core-modules) module, and does not begin with `'/'`, `'../'`, or
4851cb0ef41Sopenharmony_ci`'./'`, then Node.js starts at the directory of the current module, and
4861cb0ef41Sopenharmony_ciadds `/node_modules`, and attempts to load the module from that location.
4871cb0ef41Sopenharmony_ciNode.js will not append `node_modules` to a path already ending in
4881cb0ef41Sopenharmony_ci`node_modules`.
4891cb0ef41Sopenharmony_ci
4901cb0ef41Sopenharmony_ciIf it is not found there, then it moves to the parent directory, and so
4911cb0ef41Sopenharmony_cion, until the root of the file system is reached.
4921cb0ef41Sopenharmony_ci
4931cb0ef41Sopenharmony_ciFor example, if the file at `'/home/ry/projects/foo.js'` called
4941cb0ef41Sopenharmony_ci`require('bar.js')`, then Node.js would look in the following locations, in
4951cb0ef41Sopenharmony_cithis order:
4961cb0ef41Sopenharmony_ci
4971cb0ef41Sopenharmony_ci* `/home/ry/projects/node_modules/bar.js`
4981cb0ef41Sopenharmony_ci* `/home/ry/node_modules/bar.js`
4991cb0ef41Sopenharmony_ci* `/home/node_modules/bar.js`
5001cb0ef41Sopenharmony_ci* `/node_modules/bar.js`
5011cb0ef41Sopenharmony_ci
5021cb0ef41Sopenharmony_ciThis allows programs to localize their dependencies, so that they do not
5031cb0ef41Sopenharmony_ciclash.
5041cb0ef41Sopenharmony_ci
5051cb0ef41Sopenharmony_ciIt is possible to require specific files or sub modules distributed with a
5061cb0ef41Sopenharmony_cimodule by including a path suffix after the module name. For instance
5071cb0ef41Sopenharmony_ci`require('example-module/path/to/file')` would resolve `path/to/file`
5081cb0ef41Sopenharmony_cirelative to where `example-module` is located. The suffixed path follows the
5091cb0ef41Sopenharmony_cisame module resolution semantics.
5101cb0ef41Sopenharmony_ci
5111cb0ef41Sopenharmony_ci## Loading from the global folders
5121cb0ef41Sopenharmony_ci
5131cb0ef41Sopenharmony_ci<!-- type=misc -->
5141cb0ef41Sopenharmony_ci
5151cb0ef41Sopenharmony_ciIf the `NODE_PATH` environment variable is set to a colon-delimited list
5161cb0ef41Sopenharmony_ciof absolute paths, then Node.js will search those paths for modules if they
5171cb0ef41Sopenharmony_ciare not found elsewhere.
5181cb0ef41Sopenharmony_ci
5191cb0ef41Sopenharmony_ciOn Windows, `NODE_PATH` is delimited by semicolons (`;`) instead of colons.
5201cb0ef41Sopenharmony_ci
5211cb0ef41Sopenharmony_ci`NODE_PATH` was originally created to support loading modules from
5221cb0ef41Sopenharmony_civarying paths before the current [module resolution][] algorithm was defined.
5231cb0ef41Sopenharmony_ci
5241cb0ef41Sopenharmony_ci`NODE_PATH` is still supported, but is less necessary now that the Node.js
5251cb0ef41Sopenharmony_ciecosystem has settled on a convention for locating dependent modules.
5261cb0ef41Sopenharmony_ciSometimes deployments that rely on `NODE_PATH` show surprising behavior
5271cb0ef41Sopenharmony_ciwhen people are unaware that `NODE_PATH` must be set. Sometimes a
5281cb0ef41Sopenharmony_cimodule's dependencies change, causing a different version (or even a
5291cb0ef41Sopenharmony_cidifferent module) to be loaded as the `NODE_PATH` is searched.
5301cb0ef41Sopenharmony_ci
5311cb0ef41Sopenharmony_ciAdditionally, Node.js will search in the following list of GLOBAL\_FOLDERS:
5321cb0ef41Sopenharmony_ci
5331cb0ef41Sopenharmony_ci* 1: `$HOME/.node_modules`
5341cb0ef41Sopenharmony_ci* 2: `$HOME/.node_libraries`
5351cb0ef41Sopenharmony_ci* 3: `$PREFIX/lib/node`
5361cb0ef41Sopenharmony_ci
5371cb0ef41Sopenharmony_ciWhere `$HOME` is the user's home directory, and `$PREFIX` is the Node.js
5381cb0ef41Sopenharmony_ciconfigured `node_prefix`.
5391cb0ef41Sopenharmony_ci
5401cb0ef41Sopenharmony_ciThese are mostly for historic reasons.
5411cb0ef41Sopenharmony_ci
5421cb0ef41Sopenharmony_ciIt is strongly encouraged to place dependencies in the local `node_modules`
5431cb0ef41Sopenharmony_cifolder. These will be loaded faster, and more reliably.
5441cb0ef41Sopenharmony_ci
5451cb0ef41Sopenharmony_ci## The module wrapper
5461cb0ef41Sopenharmony_ci
5471cb0ef41Sopenharmony_ci<!-- type=misc -->
5481cb0ef41Sopenharmony_ci
5491cb0ef41Sopenharmony_ciBefore a module's code is executed, Node.js will wrap it with a function
5501cb0ef41Sopenharmony_ciwrapper that looks like the following:
5511cb0ef41Sopenharmony_ci
5521cb0ef41Sopenharmony_ci```js
5531cb0ef41Sopenharmony_ci(function(exports, require, module, __filename, __dirname) {
5541cb0ef41Sopenharmony_ci// Module code actually lives in here
5551cb0ef41Sopenharmony_ci});
5561cb0ef41Sopenharmony_ci```
5571cb0ef41Sopenharmony_ci
5581cb0ef41Sopenharmony_ciBy doing this, Node.js achieves a few things:
5591cb0ef41Sopenharmony_ci
5601cb0ef41Sopenharmony_ci* It keeps top-level variables (defined with `var`, `const`, or `let`) scoped to
5611cb0ef41Sopenharmony_ci  the module rather than the global object.
5621cb0ef41Sopenharmony_ci* It helps to provide some global-looking variables that are actually specific
5631cb0ef41Sopenharmony_ci  to the module, such as:
5641cb0ef41Sopenharmony_ci  * The `module` and `exports` objects that the implementor can use to export
5651cb0ef41Sopenharmony_ci    values from the module.
5661cb0ef41Sopenharmony_ci  * The convenience variables `__filename` and `__dirname`, containing the
5671cb0ef41Sopenharmony_ci    module's absolute filename and directory path.
5681cb0ef41Sopenharmony_ci
5691cb0ef41Sopenharmony_ci## The module scope
5701cb0ef41Sopenharmony_ci
5711cb0ef41Sopenharmony_ci### `__dirname`
5721cb0ef41Sopenharmony_ci
5731cb0ef41Sopenharmony_ci<!-- YAML
5741cb0ef41Sopenharmony_ciadded: v0.1.27
5751cb0ef41Sopenharmony_ci-->
5761cb0ef41Sopenharmony_ci
5771cb0ef41Sopenharmony_ci<!-- type=var -->
5781cb0ef41Sopenharmony_ci
5791cb0ef41Sopenharmony_ci* {string}
5801cb0ef41Sopenharmony_ci
5811cb0ef41Sopenharmony_ciThe directory name of the current module. This is the same as the
5821cb0ef41Sopenharmony_ci[`path.dirname()`][] of the [`__filename`][].
5831cb0ef41Sopenharmony_ci
5841cb0ef41Sopenharmony_ciExample: running `node example.js` from `/Users/mjr`
5851cb0ef41Sopenharmony_ci
5861cb0ef41Sopenharmony_ci```js
5871cb0ef41Sopenharmony_ciconsole.log(__dirname);
5881cb0ef41Sopenharmony_ci// Prints: /Users/mjr
5891cb0ef41Sopenharmony_ciconsole.log(path.dirname(__filename));
5901cb0ef41Sopenharmony_ci// Prints: /Users/mjr
5911cb0ef41Sopenharmony_ci```
5921cb0ef41Sopenharmony_ci
5931cb0ef41Sopenharmony_ci### `__filename`
5941cb0ef41Sopenharmony_ci
5951cb0ef41Sopenharmony_ci<!-- YAML
5961cb0ef41Sopenharmony_ciadded: v0.0.1
5971cb0ef41Sopenharmony_ci-->
5981cb0ef41Sopenharmony_ci
5991cb0ef41Sopenharmony_ci<!-- type=var -->
6001cb0ef41Sopenharmony_ci
6011cb0ef41Sopenharmony_ci* {string}
6021cb0ef41Sopenharmony_ci
6031cb0ef41Sopenharmony_ciThe file name of the current module. This is the current module file's absolute
6041cb0ef41Sopenharmony_cipath with symlinks resolved.
6051cb0ef41Sopenharmony_ci
6061cb0ef41Sopenharmony_ciFor a main program this is not necessarily the same as the file name used in the
6071cb0ef41Sopenharmony_cicommand line.
6081cb0ef41Sopenharmony_ci
6091cb0ef41Sopenharmony_ciSee [`__dirname`][] for the directory name of the current module.
6101cb0ef41Sopenharmony_ci
6111cb0ef41Sopenharmony_ciExamples:
6121cb0ef41Sopenharmony_ci
6131cb0ef41Sopenharmony_ciRunning `node example.js` from `/Users/mjr`
6141cb0ef41Sopenharmony_ci
6151cb0ef41Sopenharmony_ci```js
6161cb0ef41Sopenharmony_ciconsole.log(__filename);
6171cb0ef41Sopenharmony_ci// Prints: /Users/mjr/example.js
6181cb0ef41Sopenharmony_ciconsole.log(__dirname);
6191cb0ef41Sopenharmony_ci// Prints: /Users/mjr
6201cb0ef41Sopenharmony_ci```
6211cb0ef41Sopenharmony_ci
6221cb0ef41Sopenharmony_ciGiven two modules: `a` and `b`, where `b` is a dependency of
6231cb0ef41Sopenharmony_ci`a` and there is a directory structure of:
6241cb0ef41Sopenharmony_ci
6251cb0ef41Sopenharmony_ci* `/Users/mjr/app/a.js`
6261cb0ef41Sopenharmony_ci* `/Users/mjr/app/node_modules/b/b.js`
6271cb0ef41Sopenharmony_ci
6281cb0ef41Sopenharmony_ciReferences to `__filename` within `b.js` will return
6291cb0ef41Sopenharmony_ci`/Users/mjr/app/node_modules/b/b.js` while references to `__filename` within
6301cb0ef41Sopenharmony_ci`a.js` will return `/Users/mjr/app/a.js`.
6311cb0ef41Sopenharmony_ci
6321cb0ef41Sopenharmony_ci### `exports`
6331cb0ef41Sopenharmony_ci
6341cb0ef41Sopenharmony_ci<!-- YAML
6351cb0ef41Sopenharmony_ciadded: v0.1.12
6361cb0ef41Sopenharmony_ci-->
6371cb0ef41Sopenharmony_ci
6381cb0ef41Sopenharmony_ci<!-- type=var -->
6391cb0ef41Sopenharmony_ci
6401cb0ef41Sopenharmony_ci* {Object}
6411cb0ef41Sopenharmony_ci
6421cb0ef41Sopenharmony_ciA reference to the `module.exports` that is shorter to type.
6431cb0ef41Sopenharmony_ciSee the section about the [exports shortcut][] for details on when to use
6441cb0ef41Sopenharmony_ci`exports` and when to use `module.exports`.
6451cb0ef41Sopenharmony_ci
6461cb0ef41Sopenharmony_ci### `module`
6471cb0ef41Sopenharmony_ci
6481cb0ef41Sopenharmony_ci<!-- YAML
6491cb0ef41Sopenharmony_ciadded: v0.1.16
6501cb0ef41Sopenharmony_ci-->
6511cb0ef41Sopenharmony_ci
6521cb0ef41Sopenharmony_ci<!-- type=var -->
6531cb0ef41Sopenharmony_ci
6541cb0ef41Sopenharmony_ci* {module}
6551cb0ef41Sopenharmony_ci
6561cb0ef41Sopenharmony_ciA reference to the current module, see the section about the
6571cb0ef41Sopenharmony_ci[`module` object][]. In particular, `module.exports` is used for defining what
6581cb0ef41Sopenharmony_cia module exports and makes available through `require()`.
6591cb0ef41Sopenharmony_ci
6601cb0ef41Sopenharmony_ci### `require(id)`
6611cb0ef41Sopenharmony_ci
6621cb0ef41Sopenharmony_ci<!-- YAML
6631cb0ef41Sopenharmony_ciadded: v0.1.13
6641cb0ef41Sopenharmony_ci-->
6651cb0ef41Sopenharmony_ci
6661cb0ef41Sopenharmony_ci<!-- type=var -->
6671cb0ef41Sopenharmony_ci
6681cb0ef41Sopenharmony_ci* `id` {string} module name or path
6691cb0ef41Sopenharmony_ci* Returns: {any} exported module content
6701cb0ef41Sopenharmony_ci
6711cb0ef41Sopenharmony_ciUsed to import modules, `JSON`, and local files. Modules can be imported
6721cb0ef41Sopenharmony_cifrom `node_modules`. Local modules and JSON files can be imported using
6731cb0ef41Sopenharmony_cia relative path (e.g. `./`, `./foo`, `./bar/baz`, `../foo`) that will be
6741cb0ef41Sopenharmony_ciresolved against the directory named by [`__dirname`][] (if defined) or
6751cb0ef41Sopenharmony_cithe current working directory. The relative paths of POSIX style are resolved
6761cb0ef41Sopenharmony_ciin an OS independent fashion, meaning that the examples above will work on
6771cb0ef41Sopenharmony_ciWindows in the same way they would on Unix systems.
6781cb0ef41Sopenharmony_ci
6791cb0ef41Sopenharmony_ci```js
6801cb0ef41Sopenharmony_ci// Importing a local module with a path relative to the `__dirname` or current
6811cb0ef41Sopenharmony_ci// working directory. (On Windows, this would resolve to .\path\myLocalModule.)
6821cb0ef41Sopenharmony_ciconst myLocalModule = require('./path/myLocalModule');
6831cb0ef41Sopenharmony_ci
6841cb0ef41Sopenharmony_ci// Importing a JSON file:
6851cb0ef41Sopenharmony_ciconst jsonData = require('./path/filename.json');
6861cb0ef41Sopenharmony_ci
6871cb0ef41Sopenharmony_ci// Importing a module from node_modules or Node.js built-in module:
6881cb0ef41Sopenharmony_ciconst crypto = require('node:crypto');
6891cb0ef41Sopenharmony_ci```
6901cb0ef41Sopenharmony_ci
6911cb0ef41Sopenharmony_ci#### `require.cache`
6921cb0ef41Sopenharmony_ci
6931cb0ef41Sopenharmony_ci<!-- YAML
6941cb0ef41Sopenharmony_ciadded: v0.3.0
6951cb0ef41Sopenharmony_ci-->
6961cb0ef41Sopenharmony_ci
6971cb0ef41Sopenharmony_ci* {Object}
6981cb0ef41Sopenharmony_ci
6991cb0ef41Sopenharmony_ciModules are cached in this object when they are required. By deleting a key
7001cb0ef41Sopenharmony_civalue from this object, the next `require` will reload the module.
7011cb0ef41Sopenharmony_ciThis does not apply to [native addons][], for which reloading will result in an
7021cb0ef41Sopenharmony_cierror.
7031cb0ef41Sopenharmony_ci
7041cb0ef41Sopenharmony_ciAdding or replacing entries is also possible. This cache is checked before
7051cb0ef41Sopenharmony_cibuilt-in modules and if a name matching a built-in module is added to the cache,
7061cb0ef41Sopenharmony_cionly `node:`-prefixed require calls are going to receive the built-in module.
7071cb0ef41Sopenharmony_ciUse with care!
7081cb0ef41Sopenharmony_ci
7091cb0ef41Sopenharmony_ci<!-- eslint-disable node-core/no-duplicate-requires -->
7101cb0ef41Sopenharmony_ci
7111cb0ef41Sopenharmony_ci```js
7121cb0ef41Sopenharmony_ciconst assert = require('node:assert');
7131cb0ef41Sopenharmony_ciconst realFs = require('node:fs');
7141cb0ef41Sopenharmony_ci
7151cb0ef41Sopenharmony_ciconst fakeFs = {};
7161cb0ef41Sopenharmony_cirequire.cache.fs = { exports: fakeFs };
7171cb0ef41Sopenharmony_ci
7181cb0ef41Sopenharmony_ciassert.strictEqual(require('fs'), fakeFs);
7191cb0ef41Sopenharmony_ciassert.strictEqual(require('node:fs'), realFs);
7201cb0ef41Sopenharmony_ci```
7211cb0ef41Sopenharmony_ci
7221cb0ef41Sopenharmony_ci#### `require.extensions`
7231cb0ef41Sopenharmony_ci
7241cb0ef41Sopenharmony_ci<!-- YAML
7251cb0ef41Sopenharmony_ciadded: v0.3.0
7261cb0ef41Sopenharmony_cideprecated: v0.10.6
7271cb0ef41Sopenharmony_ci-->
7281cb0ef41Sopenharmony_ci
7291cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated
7301cb0ef41Sopenharmony_ci
7311cb0ef41Sopenharmony_ci* {Object}
7321cb0ef41Sopenharmony_ci
7331cb0ef41Sopenharmony_ciInstruct `require` on how to handle certain file extensions.
7341cb0ef41Sopenharmony_ci
7351cb0ef41Sopenharmony_ciProcess files with the extension `.sjs` as `.js`:
7361cb0ef41Sopenharmony_ci
7371cb0ef41Sopenharmony_ci```js
7381cb0ef41Sopenharmony_cirequire.extensions['.sjs'] = require.extensions['.js'];
7391cb0ef41Sopenharmony_ci```
7401cb0ef41Sopenharmony_ci
7411cb0ef41Sopenharmony_ci**Deprecated.** In the past, this list has been used to load non-JavaScript
7421cb0ef41Sopenharmony_cimodules into Node.js by compiling them on-demand. However, in practice, there
7431cb0ef41Sopenharmony_ciare much better ways to do this, such as loading modules via some other Node.js
7441cb0ef41Sopenharmony_ciprogram, or compiling them to JavaScript ahead of time.
7451cb0ef41Sopenharmony_ci
7461cb0ef41Sopenharmony_ciAvoid using `require.extensions`. Use could cause subtle bugs and resolving the
7471cb0ef41Sopenharmony_ciextensions gets slower with each registered extension.
7481cb0ef41Sopenharmony_ci
7491cb0ef41Sopenharmony_ci#### `require.main`
7501cb0ef41Sopenharmony_ci
7511cb0ef41Sopenharmony_ci<!-- YAML
7521cb0ef41Sopenharmony_ciadded: v0.1.17
7531cb0ef41Sopenharmony_ci-->
7541cb0ef41Sopenharmony_ci
7551cb0ef41Sopenharmony_ci* {module | undefined}
7561cb0ef41Sopenharmony_ci
7571cb0ef41Sopenharmony_ciThe `Module` object representing the entry script loaded when the Node.js
7581cb0ef41Sopenharmony_ciprocess launched, or `undefined` if the entry point of the program is not a
7591cb0ef41Sopenharmony_ciCommonJS module.
7601cb0ef41Sopenharmony_ciSee ["Accessing the main module"](#accessing-the-main-module).
7611cb0ef41Sopenharmony_ci
7621cb0ef41Sopenharmony_ciIn `entry.js` script:
7631cb0ef41Sopenharmony_ci
7641cb0ef41Sopenharmony_ci```js
7651cb0ef41Sopenharmony_ciconsole.log(require.main);
7661cb0ef41Sopenharmony_ci```
7671cb0ef41Sopenharmony_ci
7681cb0ef41Sopenharmony_ci```bash
7691cb0ef41Sopenharmony_cinode entry.js
7701cb0ef41Sopenharmony_ci```
7711cb0ef41Sopenharmony_ci
7721cb0ef41Sopenharmony_ci<!-- eslint-skip -->
7731cb0ef41Sopenharmony_ci
7741cb0ef41Sopenharmony_ci```js
7751cb0ef41Sopenharmony_ciModule {
7761cb0ef41Sopenharmony_ci  id: '.',
7771cb0ef41Sopenharmony_ci  path: '/absolute/path/to',
7781cb0ef41Sopenharmony_ci  exports: {},
7791cb0ef41Sopenharmony_ci  filename: '/absolute/path/to/entry.js',
7801cb0ef41Sopenharmony_ci  loaded: false,
7811cb0ef41Sopenharmony_ci  children: [],
7821cb0ef41Sopenharmony_ci  paths:
7831cb0ef41Sopenharmony_ci   [ '/absolute/path/to/node_modules',
7841cb0ef41Sopenharmony_ci     '/absolute/path/node_modules',
7851cb0ef41Sopenharmony_ci     '/absolute/node_modules',
7861cb0ef41Sopenharmony_ci     '/node_modules' ] }
7871cb0ef41Sopenharmony_ci```
7881cb0ef41Sopenharmony_ci
7891cb0ef41Sopenharmony_ci#### `require.resolve(request[, options])`
7901cb0ef41Sopenharmony_ci
7911cb0ef41Sopenharmony_ci<!-- YAML
7921cb0ef41Sopenharmony_ciadded: v0.3.0
7931cb0ef41Sopenharmony_cichanges:
7941cb0ef41Sopenharmony_ci  - version: v8.9.0
7951cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/16397
7961cb0ef41Sopenharmony_ci    description: The `paths` option is now supported.
7971cb0ef41Sopenharmony_ci-->
7981cb0ef41Sopenharmony_ci
7991cb0ef41Sopenharmony_ci* `request` {string} The module path to resolve.
8001cb0ef41Sopenharmony_ci* `options` {Object}
8011cb0ef41Sopenharmony_ci  * `paths` {string\[]} Paths to resolve module location from. If present, these
8021cb0ef41Sopenharmony_ci    paths are used instead of the default resolution paths, with the exception
8031cb0ef41Sopenharmony_ci    of [GLOBAL\_FOLDERS][GLOBAL_FOLDERS] like `$HOME/.node_modules`, which are
8041cb0ef41Sopenharmony_ci    always included. Each of these paths is used as a starting point for
8051cb0ef41Sopenharmony_ci    the module resolution algorithm, meaning that the `node_modules` hierarchy
8061cb0ef41Sopenharmony_ci    is checked from this location.
8071cb0ef41Sopenharmony_ci* Returns: {string}
8081cb0ef41Sopenharmony_ci
8091cb0ef41Sopenharmony_ciUse the internal `require()` machinery to look up the location of a module,
8101cb0ef41Sopenharmony_cibut rather than loading the module, just return the resolved filename.
8111cb0ef41Sopenharmony_ci
8121cb0ef41Sopenharmony_ciIf the module can not be found, a `MODULE_NOT_FOUND` error is thrown.
8131cb0ef41Sopenharmony_ci
8141cb0ef41Sopenharmony_ci##### `require.resolve.paths(request)`
8151cb0ef41Sopenharmony_ci
8161cb0ef41Sopenharmony_ci<!-- YAML
8171cb0ef41Sopenharmony_ciadded: v8.9.0
8181cb0ef41Sopenharmony_ci-->
8191cb0ef41Sopenharmony_ci
8201cb0ef41Sopenharmony_ci* `request` {string} The module path whose lookup paths are being retrieved.
8211cb0ef41Sopenharmony_ci* Returns: {string\[]|null}
8221cb0ef41Sopenharmony_ci
8231cb0ef41Sopenharmony_ciReturns an array containing the paths searched during resolution of `request` or
8241cb0ef41Sopenharmony_ci`null` if the `request` string references a core module, for example `http` or
8251cb0ef41Sopenharmony_ci`fs`.
8261cb0ef41Sopenharmony_ci
8271cb0ef41Sopenharmony_ci## The `module` object
8281cb0ef41Sopenharmony_ci
8291cb0ef41Sopenharmony_ci<!-- YAML
8301cb0ef41Sopenharmony_ciadded: v0.1.16
8311cb0ef41Sopenharmony_ci-->
8321cb0ef41Sopenharmony_ci
8331cb0ef41Sopenharmony_ci<!-- type=var -->
8341cb0ef41Sopenharmony_ci
8351cb0ef41Sopenharmony_ci<!-- name=module -->
8361cb0ef41Sopenharmony_ci
8371cb0ef41Sopenharmony_ci* {Object}
8381cb0ef41Sopenharmony_ci
8391cb0ef41Sopenharmony_ciIn each module, the `module` free variable is a reference to the object
8401cb0ef41Sopenharmony_cirepresenting the current module. For convenience, `module.exports` is
8411cb0ef41Sopenharmony_cialso accessible via the `exports` module-global. `module` is not actually
8421cb0ef41Sopenharmony_cia global but rather local to each module.
8431cb0ef41Sopenharmony_ci
8441cb0ef41Sopenharmony_ci### `module.children`
8451cb0ef41Sopenharmony_ci
8461cb0ef41Sopenharmony_ci<!-- YAML
8471cb0ef41Sopenharmony_ciadded: v0.1.16
8481cb0ef41Sopenharmony_ci-->
8491cb0ef41Sopenharmony_ci
8501cb0ef41Sopenharmony_ci* {module\[]}
8511cb0ef41Sopenharmony_ci
8521cb0ef41Sopenharmony_ciThe module objects required for the first time by this one.
8531cb0ef41Sopenharmony_ci
8541cb0ef41Sopenharmony_ci### `module.exports`
8551cb0ef41Sopenharmony_ci
8561cb0ef41Sopenharmony_ci<!-- YAML
8571cb0ef41Sopenharmony_ciadded: v0.1.16
8581cb0ef41Sopenharmony_ci-->
8591cb0ef41Sopenharmony_ci
8601cb0ef41Sopenharmony_ci* {Object}
8611cb0ef41Sopenharmony_ci
8621cb0ef41Sopenharmony_ciThe `module.exports` object is created by the `Module` system. Sometimes this is
8631cb0ef41Sopenharmony_cinot acceptable; many want their module to be an instance of some class. To do
8641cb0ef41Sopenharmony_cithis, assign the desired export object to `module.exports`. Assigning
8651cb0ef41Sopenharmony_cithe desired object to `exports` will simply rebind the local `exports` variable,
8661cb0ef41Sopenharmony_ciwhich is probably not what is desired.
8671cb0ef41Sopenharmony_ci
8681cb0ef41Sopenharmony_ciFor example, suppose we were making a module called `a.js`:
8691cb0ef41Sopenharmony_ci
8701cb0ef41Sopenharmony_ci```js
8711cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
8721cb0ef41Sopenharmony_ci
8731cb0ef41Sopenharmony_cimodule.exports = new EventEmitter();
8741cb0ef41Sopenharmony_ci
8751cb0ef41Sopenharmony_ci// Do some work, and after some time emit
8761cb0ef41Sopenharmony_ci// the 'ready' event from the module itself.
8771cb0ef41Sopenharmony_cisetTimeout(() => {
8781cb0ef41Sopenharmony_ci  module.exports.emit('ready');
8791cb0ef41Sopenharmony_ci}, 1000);
8801cb0ef41Sopenharmony_ci```
8811cb0ef41Sopenharmony_ci
8821cb0ef41Sopenharmony_ciThen in another file we could do:
8831cb0ef41Sopenharmony_ci
8841cb0ef41Sopenharmony_ci```js
8851cb0ef41Sopenharmony_ciconst a = require('./a');
8861cb0ef41Sopenharmony_cia.on('ready', () => {
8871cb0ef41Sopenharmony_ci  console.log('module "a" is ready');
8881cb0ef41Sopenharmony_ci});
8891cb0ef41Sopenharmony_ci```
8901cb0ef41Sopenharmony_ci
8911cb0ef41Sopenharmony_ciAssignment to `module.exports` must be done immediately. It cannot be
8921cb0ef41Sopenharmony_cidone in any callbacks. This does not work:
8931cb0ef41Sopenharmony_ci
8941cb0ef41Sopenharmony_ci`x.js`:
8951cb0ef41Sopenharmony_ci
8961cb0ef41Sopenharmony_ci```js
8971cb0ef41Sopenharmony_cisetTimeout(() => {
8981cb0ef41Sopenharmony_ci  module.exports = { a: 'hello' };
8991cb0ef41Sopenharmony_ci}, 0);
9001cb0ef41Sopenharmony_ci```
9011cb0ef41Sopenharmony_ci
9021cb0ef41Sopenharmony_ci`y.js`:
9031cb0ef41Sopenharmony_ci
9041cb0ef41Sopenharmony_ci```js
9051cb0ef41Sopenharmony_ciconst x = require('./x');
9061cb0ef41Sopenharmony_ciconsole.log(x.a);
9071cb0ef41Sopenharmony_ci```
9081cb0ef41Sopenharmony_ci
9091cb0ef41Sopenharmony_ci#### `exports` shortcut
9101cb0ef41Sopenharmony_ci
9111cb0ef41Sopenharmony_ci<!-- YAML
9121cb0ef41Sopenharmony_ciadded: v0.1.16
9131cb0ef41Sopenharmony_ci-->
9141cb0ef41Sopenharmony_ci
9151cb0ef41Sopenharmony_ciThe `exports` variable is available within a module's file-level scope, and is
9161cb0ef41Sopenharmony_ciassigned the value of `module.exports` before the module is evaluated.
9171cb0ef41Sopenharmony_ci
9181cb0ef41Sopenharmony_ciIt allows a shortcut, so that `module.exports.f = ...` can be written more
9191cb0ef41Sopenharmony_cisuccinctly as `exports.f = ...`. However, be aware that like any variable, if a
9201cb0ef41Sopenharmony_cinew value is assigned to `exports`, it is no longer bound to `module.exports`:
9211cb0ef41Sopenharmony_ci
9221cb0ef41Sopenharmony_ci```js
9231cb0ef41Sopenharmony_cimodule.exports.hello = true; // Exported from require of module
9241cb0ef41Sopenharmony_ciexports = { hello: false };  // Not exported, only available in the module
9251cb0ef41Sopenharmony_ci```
9261cb0ef41Sopenharmony_ci
9271cb0ef41Sopenharmony_ciWhen the `module.exports` property is being completely replaced by a new
9281cb0ef41Sopenharmony_ciobject, it is common to also reassign `exports`:
9291cb0ef41Sopenharmony_ci
9301cb0ef41Sopenharmony_ci<!-- eslint-disable func-name-matching -->
9311cb0ef41Sopenharmony_ci
9321cb0ef41Sopenharmony_ci```js
9331cb0ef41Sopenharmony_cimodule.exports = exports = function Constructor() {
9341cb0ef41Sopenharmony_ci  // ... etc.
9351cb0ef41Sopenharmony_ci};
9361cb0ef41Sopenharmony_ci```
9371cb0ef41Sopenharmony_ci
9381cb0ef41Sopenharmony_ciTo illustrate the behavior, imagine this hypothetical implementation of
9391cb0ef41Sopenharmony_ci`require()`, which is quite similar to what is actually done by `require()`:
9401cb0ef41Sopenharmony_ci
9411cb0ef41Sopenharmony_ci```js
9421cb0ef41Sopenharmony_cifunction require(/* ... */) {
9431cb0ef41Sopenharmony_ci  const module = { exports: {} };
9441cb0ef41Sopenharmony_ci  ((module, exports) => {
9451cb0ef41Sopenharmony_ci    // Module code here. In this example, define a function.
9461cb0ef41Sopenharmony_ci    function someFunc() {}
9471cb0ef41Sopenharmony_ci    exports = someFunc;
9481cb0ef41Sopenharmony_ci    // At this point, exports is no longer a shortcut to module.exports, and
9491cb0ef41Sopenharmony_ci    // this module will still export an empty default object.
9501cb0ef41Sopenharmony_ci    module.exports = someFunc;
9511cb0ef41Sopenharmony_ci    // At this point, the module will now export someFunc, instead of the
9521cb0ef41Sopenharmony_ci    // default object.
9531cb0ef41Sopenharmony_ci  })(module, module.exports);
9541cb0ef41Sopenharmony_ci  return module.exports;
9551cb0ef41Sopenharmony_ci}
9561cb0ef41Sopenharmony_ci```
9571cb0ef41Sopenharmony_ci
9581cb0ef41Sopenharmony_ci### `module.filename`
9591cb0ef41Sopenharmony_ci
9601cb0ef41Sopenharmony_ci<!-- YAML
9611cb0ef41Sopenharmony_ciadded: v0.1.16
9621cb0ef41Sopenharmony_ci-->
9631cb0ef41Sopenharmony_ci
9641cb0ef41Sopenharmony_ci* {string}
9651cb0ef41Sopenharmony_ci
9661cb0ef41Sopenharmony_ciThe fully resolved filename of the module.
9671cb0ef41Sopenharmony_ci
9681cb0ef41Sopenharmony_ci### `module.id`
9691cb0ef41Sopenharmony_ci
9701cb0ef41Sopenharmony_ci<!-- YAML
9711cb0ef41Sopenharmony_ciadded: v0.1.16
9721cb0ef41Sopenharmony_ci-->
9731cb0ef41Sopenharmony_ci
9741cb0ef41Sopenharmony_ci* {string}
9751cb0ef41Sopenharmony_ci
9761cb0ef41Sopenharmony_ciThe identifier for the module. Typically this is the fully resolved
9771cb0ef41Sopenharmony_cifilename.
9781cb0ef41Sopenharmony_ci
9791cb0ef41Sopenharmony_ci### `module.isPreloading`
9801cb0ef41Sopenharmony_ci
9811cb0ef41Sopenharmony_ci<!-- YAML
9821cb0ef41Sopenharmony_ciadded:
9831cb0ef41Sopenharmony_ci  - v15.4.0
9841cb0ef41Sopenharmony_ci  - v14.17.0
9851cb0ef41Sopenharmony_ci-->
9861cb0ef41Sopenharmony_ci
9871cb0ef41Sopenharmony_ci* Type: {boolean} `true` if the module is running during the Node.js preload
9881cb0ef41Sopenharmony_ci  phase.
9891cb0ef41Sopenharmony_ci
9901cb0ef41Sopenharmony_ci### `module.loaded`
9911cb0ef41Sopenharmony_ci
9921cb0ef41Sopenharmony_ci<!-- YAML
9931cb0ef41Sopenharmony_ciadded: v0.1.16
9941cb0ef41Sopenharmony_ci-->
9951cb0ef41Sopenharmony_ci
9961cb0ef41Sopenharmony_ci* {boolean}
9971cb0ef41Sopenharmony_ci
9981cb0ef41Sopenharmony_ciWhether or not the module is done loading, or is in the process of
9991cb0ef41Sopenharmony_ciloading.
10001cb0ef41Sopenharmony_ci
10011cb0ef41Sopenharmony_ci### `module.parent`
10021cb0ef41Sopenharmony_ci
10031cb0ef41Sopenharmony_ci<!-- YAML
10041cb0ef41Sopenharmony_ciadded: v0.1.16
10051cb0ef41Sopenharmony_cideprecated:
10061cb0ef41Sopenharmony_ci  - v14.6.0
10071cb0ef41Sopenharmony_ci  - v12.19.0
10081cb0ef41Sopenharmony_ci-->
10091cb0ef41Sopenharmony_ci
10101cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Please use [`require.main`][] and
10111cb0ef41Sopenharmony_ci> [`module.children`][] instead.
10121cb0ef41Sopenharmony_ci
10131cb0ef41Sopenharmony_ci* {module | null | undefined}
10141cb0ef41Sopenharmony_ci
10151cb0ef41Sopenharmony_ciThe module that first required this one, or `null` if the current module is the
10161cb0ef41Sopenharmony_cientry point of the current process, or `undefined` if the module was loaded by
10171cb0ef41Sopenharmony_cisomething that is not a CommonJS module (E.G.: REPL or `import`).
10181cb0ef41Sopenharmony_ci
10191cb0ef41Sopenharmony_ci### `module.path`
10201cb0ef41Sopenharmony_ci
10211cb0ef41Sopenharmony_ci<!-- YAML
10221cb0ef41Sopenharmony_ciadded: v11.14.0
10231cb0ef41Sopenharmony_ci-->
10241cb0ef41Sopenharmony_ci
10251cb0ef41Sopenharmony_ci* {string}
10261cb0ef41Sopenharmony_ci
10271cb0ef41Sopenharmony_ciThe directory name of the module. This is usually the same as the
10281cb0ef41Sopenharmony_ci[`path.dirname()`][] of the [`module.id`][].
10291cb0ef41Sopenharmony_ci
10301cb0ef41Sopenharmony_ci### `module.paths`
10311cb0ef41Sopenharmony_ci
10321cb0ef41Sopenharmony_ci<!-- YAML
10331cb0ef41Sopenharmony_ciadded: v0.4.0
10341cb0ef41Sopenharmony_ci-->
10351cb0ef41Sopenharmony_ci
10361cb0ef41Sopenharmony_ci* {string\[]}
10371cb0ef41Sopenharmony_ci
10381cb0ef41Sopenharmony_ciThe search paths for the module.
10391cb0ef41Sopenharmony_ci
10401cb0ef41Sopenharmony_ci### `module.require(id)`
10411cb0ef41Sopenharmony_ci
10421cb0ef41Sopenharmony_ci<!-- YAML
10431cb0ef41Sopenharmony_ciadded: v0.5.1
10441cb0ef41Sopenharmony_ci-->
10451cb0ef41Sopenharmony_ci
10461cb0ef41Sopenharmony_ci* `id` {string}
10471cb0ef41Sopenharmony_ci* Returns: {any} exported module content
10481cb0ef41Sopenharmony_ci
10491cb0ef41Sopenharmony_ciThe `module.require()` method provides a way to load a module as if
10501cb0ef41Sopenharmony_ci`require()` was called from the original module.
10511cb0ef41Sopenharmony_ci
10521cb0ef41Sopenharmony_ciIn order to do this, it is necessary to get a reference to the `module` object.
10531cb0ef41Sopenharmony_ciSince `require()` returns the `module.exports`, and the `module` is typically
10541cb0ef41Sopenharmony_ci_only_ available within a specific module's code, it must be explicitly exported
10551cb0ef41Sopenharmony_ciin order to be used.
10561cb0ef41Sopenharmony_ci
10571cb0ef41Sopenharmony_ci## The `Module` object
10581cb0ef41Sopenharmony_ci
10591cb0ef41Sopenharmony_ciThis section was moved to
10601cb0ef41Sopenharmony_ci[Modules: `module` core module](module.md#the-module-object).
10611cb0ef41Sopenharmony_ci
10621cb0ef41Sopenharmony_ci<!-- Anchors to make sure old links find a target -->
10631cb0ef41Sopenharmony_ci
10641cb0ef41Sopenharmony_ci* <a id="modules_module_builtinmodules" href="module.html#modulebuiltinmodules">`module.builtinModules`</a>
10651cb0ef41Sopenharmony_ci* <a id="modules_module_createrequire_filename" href="module.html#modulecreaterequirefilename">`module.createRequire(filename)`</a>
10661cb0ef41Sopenharmony_ci* <a id="modules_module_syncbuiltinesmexports" href="module.html#modulesyncbuiltinesmexports">`module.syncBuiltinESMExports()`</a>
10671cb0ef41Sopenharmony_ci
10681cb0ef41Sopenharmony_ci## Source map v3 support
10691cb0ef41Sopenharmony_ci
10701cb0ef41Sopenharmony_ciThis section was moved to
10711cb0ef41Sopenharmony_ci[Modules: `module` core module](module.md#source-map-v3-support).
10721cb0ef41Sopenharmony_ci
10731cb0ef41Sopenharmony_ci<!-- Anchors to make sure old links find a target -->
10741cb0ef41Sopenharmony_ci
10751cb0ef41Sopenharmony_ci* <a id="modules_module_findsourcemap_path_error" href="module.html#modulefindsourcemappath">`module.findSourceMap(path)`</a>
10761cb0ef41Sopenharmony_ci* <a id="modules_class_module_sourcemap" href="module.html#class-modulesourcemap">Class: `module.SourceMap`</a>
10771cb0ef41Sopenharmony_ci  * <a id="modules_new_sourcemap_payload" href="module.html#new-sourcemappayload">`new SourceMap(payload)`</a>
10781cb0ef41Sopenharmony_ci  * <a id="modules_sourcemap_payload" href="module.html#sourcemappayload">`sourceMap.payload`</a>
10791cb0ef41Sopenharmony_ci  * <a id="modules_sourcemap_findentry_linenumber_columnnumber" href="module.html#sourcemapfindentrylinenumber-columnnumber">`sourceMap.findEntry(lineNumber, columnNumber)`</a>
10801cb0ef41Sopenharmony_ci
10811cb0ef41Sopenharmony_ci[Determining module system]: packages.md#determining-module-system
10821cb0ef41Sopenharmony_ci[ECMAScript Modules]: esm.md
10831cb0ef41Sopenharmony_ci[GLOBAL_FOLDERS]: #loading-from-the-global-folders
10841cb0ef41Sopenharmony_ci[`"main"`]: packages.md#main
10851cb0ef41Sopenharmony_ci[`"type"`]: packages.md#type
10861cb0ef41Sopenharmony_ci[`ERR_REQUIRE_ESM`]: errors.md#err_require_esm
10871cb0ef41Sopenharmony_ci[`ERR_UNSUPPORTED_DIR_IMPORT`]: errors.md#err_unsupported_dir_import
10881cb0ef41Sopenharmony_ci[`MODULE_NOT_FOUND`]: errors.md#module_not_found
10891cb0ef41Sopenharmony_ci[`__dirname`]: #__dirname
10901cb0ef41Sopenharmony_ci[`__filename`]: #__filename
10911cb0ef41Sopenharmony_ci[`import()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import
10921cb0ef41Sopenharmony_ci[`module.builtinModules`]: module.md#modulebuiltinmodules
10931cb0ef41Sopenharmony_ci[`module.children`]: #modulechildren
10941cb0ef41Sopenharmony_ci[`module.id`]: #moduleid
10951cb0ef41Sopenharmony_ci[`module` core module]: module.md
10961cb0ef41Sopenharmony_ci[`module` object]: #the-module-object
10971cb0ef41Sopenharmony_ci[`package.json`]: packages.md#nodejs-packagejson-field-definitions
10981cb0ef41Sopenharmony_ci[`path.dirname()`]: path.md#pathdirnamepath
10991cb0ef41Sopenharmony_ci[`require.main`]: #requiremain
11001cb0ef41Sopenharmony_ci[exports shortcut]: #exports-shortcut
11011cb0ef41Sopenharmony_ci[module resolution]: #all-together
11021cb0ef41Sopenharmony_ci[native addons]: addons.md
11031cb0ef41Sopenharmony_ci[subpath exports]: packages.md#subpath-exports
11041cb0ef41Sopenharmony_ci[subpath imports]: packages.md#subpath-imports
1105