• Home
  • Raw
  • Download

Lines Matching +full:required +full:- +full:modules

1 # Modules: CommonJS modules
3 <!--introduced_in=v0.10.0-->
5 > Stability: 2 - Stable
7 <!--name=module-->
9 CommonJS modules are the original way to package JavaScript code for Node.js.
10 Node.js also supports the [ECMAScript modules][] standard used by browsers
39 in a function by Node.js (see [module wrapper](#the-module-wrapper)).
72 <!-- type=misc -->
74 Node.js has two module systems: CommonJS modules and [ECMAScript modules][].
76 By default, Node.js will treat the following as CommonJS modules:
81 contains a top-level field [`"type"`][] with a value of `"commonjs"`.
84 doesn't contain a top-level field [`"type"`][]. Package authors should include
91 (when the nearest parent `package.json` file contains a top-level field
93 CommonJS modules only if they are being included via `require()`, not when
94 used as the command-line entry point of the program).
103 <!-- type=misc -->
117 <!-- type=misc -->
122 native packages from Node.js modules without modification.
127 `/usr/lib/node/<some-package>/<some-version>` hold the contents of a
135 Because Node.js looks up the `realpath` of any modules it loads (that is, it
136 … then [looks for their dependencies in `node_modules` folders](#loading-from-node_modules-folders),
162 In order to make modules available to the Node.js REPL, it might be useful to
174 The `.mjs` extension is reserved for [ECMAScript Modules][] which cannot be
176 regarding which files are parsed as ECMAScript modules.
180 <!-- type=misc -->
185 Putting together all of the above, here is the high-level algorithm
236 2. let I = count of PARTS - 1
242 d. let I = I - 1
250 …["node", "require"]) <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver…
261 …`package.json` "exports", ["node", "require"]) <a href="esm.md#resolver-algorithm-specification">d…
271 <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
283 <!--type=misc-->
285 Modules are cached after the first time they are loaded. This means (among other
299 <!--type=misc-->
301 Modules are cached based on their resolved filename. Since modules may resolve
306 Additionally, on case-insensitive file systems or operating systems, different
308 them as different modules and will reload the file multiple times. For example,
312 ## Core modules
314 <!--type=misc-->
316 <!-- YAML
318 - version:
319 - v16.0.0
320 - v14.18.0
321 pr-url: https://github.com/nodejs/node/pull/37246
323 -->
325 Node.js has several modules compiled into the binary. These modules are
328 The core modules are defined within the Node.js source and are located in the
331 Core modules can be identified using the `node:` prefix, in which case
336 Some core modules are always preferentially loaded if their identifier is
338 return the built-in HTTP module, even if there is a file by that name. The list
339 of core modules that can be loaded without using the `node:` prefix is exposed
344 <!--type=misc-->
388 By the time `main.js` has loaded both modules, they're both finished.
403 Careful planning is required to allow cyclic module dependencies to work
406 ## File modules
408 <!--type=misc-->
411 required filename with the added extensions: `.js`, `.json`, and finally
417 compiled addon modules loaded with `process.dlopen()`. Files using any other
422 A required module prefixed with `'/'` is an absolute path to the file. For
426 A required module prefixed with `'./'` is relative to the file calling
436 ## Folders as modules
438 <!--type=misc-->
440 > Stability: 3 - Legacy: Use [subpath exports][] or [subpath imports][] instead.
450 { "name" : "some-library",
451 "main" : "./lib/some-library.js" }
454 If this was in a folder at `./some-library`, then
455 `require('./some-library')` would attempt to load
456 `./some-library/lib/some-library.js`.
462 example, then `require('./some-library')` would attempt to load:
464 * `./some-library/index.js`
465 * `./some-library/index.node`
471 Error: Cannot find module 'some-library'
474 In all three above cases, an `import('./some-library')` call would result in a
477 folders as modules, and work for both `require` and `import`.
481 <!--type=misc-->
484 [core](#core-modules) module, and does not begin with `'/'`, `'../'`, or
505 It is possible to require specific files or sub modules distributed with a
507 `require('example-module/path/to/file')` would resolve `path/to/file`
508 relative to where `example-module` is located. The suffixed path follows the
513 <!-- type=misc -->
515 If the `NODE_PATH` environment variable is set to a colon-delimited list
516 of absolute paths, then Node.js will search those paths for modules if they
521 `NODE_PATH` was originally created to support loading modules from
525 ecosystem has settled on a convention for locating dependent modules.
547 <!-- type=misc -->
560 * It keeps top-level variables (defined with `var`, `const`, or `let`) scoped to
562 * It helps to provide some global-looking variables that are actually specific
573 <!-- YAML
575 -->
577 <!-- type=var -->
595 <!-- YAML
597 -->
599 <!-- type=var -->
622 Given two modules: `a` and `b`, where `b` is a dependency of
634 <!-- YAML
636 -->
638 <!-- type=var -->
648 <!-- YAML
650 -->
652 <!-- type=var -->
662 <!-- YAML
664 -->
666 <!-- type=var -->
671 Used to import modules, `JSON`, and local files. Modules can be imported
672 from `node_modules`. Local modules and JSON files can be imported using
687 // Importing a module from node_modules or Node.js built-in module:
693 <!-- YAML
695 -->
699 Modules are cached in this object when they are required. By deleting a key
705 built-in modules and if a name matching a built-in module is added to the cache,
706 only `node:`-prefixed require calls are going to receive the built-in module.
709 <!-- eslint-disable node-core/no-duplicate-requires -->
724 <!-- YAML
727 -->
729 > Stability: 0 - Deprecated
741 **Deprecated.** In the past, this list has been used to load non-JavaScript
742 modules into Node.js by compiling them on-demand. However, in practice, there
743 are much better ways to do this, such as loading modules via some other Node.js
751 <!-- YAML
753 -->
760 See ["Accessing the main module"](#accessing-the-main-module).
772 <!-- eslint-skip -->
791 <!-- YAML
794 - version: v8.9.0
795 pr-url: https://github.com/nodejs/node/pull/16397
797 -->
816 <!-- YAML
818 -->
829 <!-- YAML
831 -->
833 <!-- type=var -->
835 <!-- name=module -->
841 also accessible via the `exports` module-global. `module` is not actually
846 <!-- YAML
848 -->
852 The module objects required for the first time by this one.
856 <!-- YAML
858 -->
911 <!-- YAML
913 -->
915 The `exports` variable is available within a module's file-level scope, and is
930 <!-- eslint-disable func-name-matching -->
960 <!-- YAML
962 -->
970 <!-- YAML
972 -->
981 <!-- YAML
983 - v15.4.0
984 - v14.17.0
985 -->
992 <!-- YAML
994 -->
1003 <!-- YAML
1006 - v14.6.0
1007 - v12.19.0
1008 -->
1010 > Stability: 0 - Deprecated: Please use [`require.main`][] and
1015 The module that first required this one, or `null` if the current module is the
1021 <!-- YAML
1023 -->
1032 <!-- YAML
1034 -->
1042 <!-- YAML
1044 -->
1060 [Modules: `module` core module](module.md#the-module-object).
1062 <!-- Anchors to make sure old links find a target -->
1071 [Modules: `module` core module](module.md#source-map-v3-support).
1073 <!-- Anchors to make sure old links find a target -->
1076 * <a id="modules_class_module_sourcemap" href="module.html#class-modulesourcemap">Class: `module.So…
1077 …* <a id="modules_new_sourcemap_payload" href="module.html#new-sourcemappayload">`new SourceMap(pay…
1079 …ntry_linenumber_columnnumber" href="module.html#sourcemapfindentrylinenumber-columnnumber">`source…
1081 [Determining module system]: packages.md#determining-module-system
1082 [ECMAScript Modules]: esm.md
1083 [GLOBAL_FOLDERS]: #loading-from-the-global-folders
1091 [`import()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import
1096 [`module` object]: #the-module-object
1097 [`package.json`]: packages.md#nodejs-packagejson-field-definitions
1100 [exports shortcut]: #exports-shortcut
1101 [module resolution]: #all-together
1103 [subpath exports]: packages.md#subpath-exports
1104 [subpath imports]: packages.md#subpath-imports