• Home
  • Raw
  • Download

Lines Matching full:module

7 <!--name=module-->
9 In the Node.js module system, each file is treated as a separate module. For
17 On the first line, `foo.js` loads the module `circle.js` that is in the same
30 The module `circle.js` has exported the functions `area()` and
31 `circumference()`. Functions and objects are added to the root of a module
34 Variables local to the module will be private, because the module is wrapped
35 in a function by Node.js (see [module wrapper](#modules_the_module_wrapper)).
38 The `module.exports` property can be assigned a new value (such as a function
41 Below, `bar.js` makes use of the `square` module, which exports a Square class:
49 The `square` module is defined in `square.js`:
52 // Assigning to exports will not modify module, must use module.exports
53 module.exports = class Square {
64 The module system is implemented in the `require('module')` module.
66 ## Accessing the main module
71 `module`. That means that it is possible to determine whether a file has been
72 run directly by testing `require.main === module`.
77 Because `module` provides a `filename` property (normally equivalent to
114 conflicts, every module will be able to get a version of its dependency
123 Furthermore, to make the module lookup process even more optimal, rather
130 variable. Since the module lookups using `node_modules` folders are all
152 require(X) from module at path Y
153 1. If X is a core module,
154 a. return the core module
258 will not cause the module code to be executed multiple times. This is an
262 To have a module execute code multiple times, export a function, and call that
265 ### Module caching caveats
270 to a different filename based on the location of the calling module (loading
298 return the built in HTTP module, even if there is a file by that name.
302 always return the built in HTTP module, even if there is `require.cache` entry
309 When there are circular `require()` calls, a module might not have finished
348 `b.js` module. `b.js` then finishes loading, and its `exports` object is
349 provided to the `a.js` module.
366 Careful planning is required to allow cyclic module dependencies to work
381 A required module prefixed with `'/'` is an absolute path to the file. For
385 A required module prefixed with `'./'` is relative to the file calling
389 Without a leading `'/'`, `'./'`, or `'../'` to indicate a file, the module must
390 either be a core module or is loaded from a `node_modules` folder.
405 which specifies a `main` module. An example [`package.json`][] file might
428 If these attempts fail, then Node.js will report the entire module as missing
432 Error: Cannot find module 'some-library'
439 If the module identifier passed to `require()` is not a
440 [core](#modules_core_modules) module, and does not begin with `'/'`, `'../'`, or
441 `'./'`, then Node.js starts at the parent directory of the current module, and
442 adds `/node_modules`, and attempts to load the module from that location.
462 module by including a path suffix after the module name. For instance
463 `require('example-module/path/to/file')` would resolve `path/to/file`
464 relative to where `example-module` is located. The suffixed path follows the
465 same module resolution semantics.
478 varying paths before the current [module resolution][] algorithm was defined.
484 module's dependencies change, causing a different version (or even a
485 different module) to be loaded as the `NODE_PATH` is searched.
501 ## The module wrapper
505 Before a module's code is executed, Node.js will wrap it with a function
509 (function(exports, require, module, __filename, __dirname) {
510 // Module code actually lives in here
517 the module rather than the global object.
519 to the module, such as:
520 * The `module` and `exports` objects that the implementor can use to export
521 values from the module.
523 module's absolute filename and directory path.
525 ## The module scope
536 The directory name of the current module. This is the same as the
557 The file name of the current module. This is the current module file's absolute
563 See [`__dirname`][] for the directory name of the current module.
595 A reference to the `module.exports` that is shorter to type.
597 `exports` and when to use `module.exports`.
599 ### `module`
606 * {module}
608 A reference to the current module, see the section about the
609 [`module` object][]. In particular, `module.exports` is used for defining what
610 a module exports and makes available through `require()`.
619 * `id` {string} module name or path
620 * Returns: {any} exported module content
631 // Importing a local module with a path relative to the `__dirname` or current
638 // Importing a module from node_modules or Node.js built-in module:
650 value from this object, the next `require` will reload the module.
655 native modules and if a name matching a native module is added to the cache,
656 only `node:`-prefixed require calls are going to receive the native module.
702 * {module}
704 The `Module` object representing the entry script loaded when the Node.js
706 See ["Accessing the main module"](#modules_accessing_the_main_module).
720 Module {
744 * `request` {string} The module path to resolve.
746 * `paths` {string[]} Paths to resolve module location from. If present, these
750 the module resolution algorithm, meaning that the `node_modules` hierarchy
754 Use the internal `require()` machinery to look up the location of a module,
755 but rather than loading the module, just return the resolved filename.
757 If the module can not be found, a `MODULE_NOT_FOUND` error is thrown.
764 * `request` {string} The module path whose lookup paths are being retrieved.
768 `null` if the `request` string references a core module, for example `http` or
771 ## The `module` object
777 <!-- name=module -->
781 In each module, the `module` free variable is a reference to the object
782 representing the current module. For convenience, `module.exports` is
783 also accessible via the `exports` module-global. `module` is not actually
784 a global but rather local to each module.
786 ### `module.children`
791 * {module[]}
793 The module objects required for the first time by this one.
795 ### `module.exports`
802 The `module.exports` object is created by the `Module` system. Sometimes this is
803 not acceptable; many want their module to be an instance of some class. To do
804 this, assign the desired export object to `module.exports`. Assigning
808 For example, suppose we were making a module called `a.js`:
813 module.exports = new EventEmitter();
816 // the 'ready' event from the module itself.
818 module.exports.emit('ready');
827 console.log('module "a" is ready');
831 Assignment to `module.exports` must be done immediately. It cannot be
838 module.exports = { a: 'hello' };
854 The `exports` variable is available within a module's file-level scope, and is
855 assigned the value of `module.exports` before the module is evaluated.
857 It allows a shortcut, so that `module.exports.f = ...` can be written more
859 new value is assigned to `exports`, it is no longer bound to `module.exports`:
862 module.exports.hello = true; // Exported from require of module
863 exports = { hello: false }; // Not exported, only available in the module
866 When the `module.exports` property is being completely replaced by a new
871 module.exports = exports = function Constructor() {
881 const module = { exports: {} };
882 ((module, exports) => {
883 // Module code here. In this example, define a function.
886 // At this point, exports is no longer a shortcut to module.exports, and
887 // this module will still export an empty default object.
888 module.exports = someFunc;
889 // At this point, the module will now export someFunc, instead of the
891 })(module, module.exports);
892 return module.exports;
896 ### `module.filename`
903 The fully resolved filename of the module.
905 ### `module.id`
912 The identifier for the module. Typically this is the fully resolved
915 ### `module.isPreloading`
920 * Type: {boolean} `true` if the module is running during the Node.js preload
923 ### `module.loaded`
930 Whether or not the module is done loading, or is in the process of
933 ### `module.parent`
942 > [`module.children`][] instead.
944 * {module | null | undefined}
946 The module that first required this one, or `null` if the current module is the
947 entry point of the current process, or `undefined` if the module was loaded by
948 something that is not a CommonJS module (E.G.: REPL or `import`).
950 ### `module.path`
957 The directory name of the module. This is usually the same as the
958 [`path.dirname()`][] of the [`module.id`][].
960 ### `module.paths`
967 The search paths for the module.
969 ### `module.require(id)`
975 * Returns: {any} exported module content
977 The `module.require()` method provides a way to load a module as if
978 `require()` was called from the original module.
980 In order to do this, it is necessary to get a reference to the `module` object.
981 Since `require()` returns the `module.exports`, and the `module` is typically
982 *only* available within a specific module's code, it must be explicitly exported
985 ## The `Module` object
988 [Modules: `module` core module](module.md#module_the_module_object).
991 * <a id="modules_module_builtinmodules" href="module.html#module_module_builtinmodules">`module.bui…
992 …d="modules_module_createrequire_filename" href="module.html#module_module_createrequire_filename">…
993 …odule_createrequirefrompath_filename" href="module.html#module_module_createrequirefrompath_filena…
994 …id="modules_module_syncbuiltinesmexports" href="module.html#module_module_syncbuiltinesmexports">`
999 [Modules: `module` core module](module.md#module_source_map_v3_support).
1002 …d="modules_module_findsourcemap_path_error" href="module.html#module_module_findsourcemap_path">`m…
1003 * <a id="modules_class_module_sourcemap" href="module.html#module_class_module_sourcemap">Class: `m…
1004 …* <a id="modules_new_sourcemap_payload" href="module.html#module_new_sourcemap_payload">`new Sourc…
1005 …* <a id="modules_sourcemap_payload" href="module.html#module_sourcemap_payload">`sourceMap.payload…
1006 …* <a id="modules_sourcemap_findentry_linenumber_columnnumber" href="module.html#module_sourcemap_f…
1014 [`module.children`]: #modules_module_children
1015 [`module.id`]: #modules_module_id
1016 [`module` object]: #modules_the_module_object
1022 [module resolution]: #modules_all_together