• Home
  • Raw
  • Download

Lines Matching +full:node +full:- +full:core

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
13 In Node.js, each file is treated as a separate module. For
39 in a function by Node.js (see [module wrapper](#the-module-wrapper)).
68 The CommonJS module system is implemented in the [`module` core module][].
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
90 * Files with an extension that is not `.mjs`, `.cjs`, `.json`, `.node`, or `.js`
91 (when the nearest parent `package.json` file contains a top-level field
94 used as the command-line entry point of the program).
103 <!-- type=misc -->
105 When a file is run directly from Node.js, `require.main` is set to its
109 For a file `foo.js`, this will be `true` if run via `node foo.js`, but
117 <!-- type=misc -->
119 The semantics of the Node.js `require()` function were designed to be general
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),
139 * `/usr/lib/node/foo/1.2.3/`: Contents of the `foo` package, version 1.2.3.
140 * `/usr/lib/node/bar/4.3.2/`: Contents of the `bar` package that `foo` depends
142 * `/usr/lib/node/foo/1.2.3/node_modules/bar`: Symbolic link to
143 `/usr/lib/node/bar/4.3.2/`.
144 * `/usr/lib/node/bar/4.3.2/node_modules/*`: Symbolic links to the packages that
152 version that is symlinked into `/usr/lib/node/foo/1.2.3/node_modules/bar`.
155 `/usr/lib/node/bar/4.3.2/node_modules/quux`.
158 than putting packages directly in `/usr/lib/node`, we could put them in
159 `/usr/lib/node_modules/<name>/<version>`. Then Node.js will not bother
162 In order to make modules available to the Node.js REPL, it might be useful to
180 <!-- type=misc -->
185 Putting together all of the above, here is the high-level algorithm
190 1. If X is a core module,
191 a. return the core module
209 4. If X.node is a file, load X.node as binary addon. STOP
214 3. If X/index.node is a file, load X/index.node as binary addon. STOP
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…
270 "." + X.slice("name".length), `package.json` "exports", ["node", "require"])
271 <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
283 <!--type=misc-->
299 <!--type=misc-->
306 Additionally, on case-insensitive file systems or operating systems, different
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
322 description: Added `node:` import support to `require(...)`.
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
332 it bypasses the `require` cache. For instance, `require('node:http')` will
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-->
392 $ node main.js
408 <!--type=misc-->
410 If the exact filename is not found, then Node.js will attempt to load the
412 `.node`. When loading a file that has a different extension (e.g. `.cjs`), its
416 `.json` files are parsed as JSON text files, `.node` files are interpreted as
431 either be a core module or is loaded from a `node_modules` folder.
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`.
459 [`"main"`][] entry is missing or cannot be resolved, then Node.js
460 will attempt to load an `index.js` or `index.node` file out of that
462 example, then `require('./some-library')` would attempt to load:
464 * `./some-library/index.js`
465 * `./some-library/index.node`
467 If these attempts fail, then Node.js will report the entire module as missing
471 Error: Cannot find module 'some-library'
474 In all three above cases, an `import('./some-library')` call would result in a
481 <!--type=misc-->
484 [core](#core-modules) module, and does not begin with `'/'`, `'../'`, or
485 `'./'`, then Node.js starts at the directory of the current module, and
487 Node.js will not append `node_modules` to a path already ending in
494 `require('bar.js')`, then Node.js would look in the following locations, in
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
524 `NODE_PATH` is still supported, but is less necessary now that the Node.js
531 Additionally, Node.js will search in the following list of GLOBAL\_FOLDERS:
535 * 3: `$PREFIX/lib/node`
537 Where `$HOME` is the user's home directory, and `$PREFIX` is the Node.js
547 <!-- type=misc -->
549 Before a module's code is executed, Node.js will wrap it with a function
558 By doing this, Node.js achieves a few things:
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 -->
584 Example: running `node example.js` from `/Users/mjr`
595 <!-- YAML
597 -->
599 <!-- type=var -->
613 Running `node example.js` from `/Users/mjr`
634 <!-- YAML
636 -->
638 <!-- type=var -->
648 <!-- YAML
650 -->
652 <!-- type=var -->
662 <!-- YAML
664 -->
666 <!-- type=var -->
687 // Importing a module from node_modules or Node.js built-in module:
688 const crypto = require('node:crypto');
693 <!-- YAML
695 -->
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 -->
712 const assert = require('node:assert');
713 const realFs = require('node:fs');
719 assert.strictEqual(require('node:fs'), realFs);
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 -->
757 The `Module` object representing the entry script loaded when the Node.js
760 See ["Accessing the main module"](#accessing-the-main-module).
769 node entry.js
772 <!-- eslint-skip -->
791 <!-- YAML
794 - version: v8.9.0
795 pr-url: https://github.com/nodejs/node/pull/16397
797 -->
816 <!-- YAML
818 -->
824 `null` if the `request` string references a core module, for example `http` or
829 <!-- YAML
831 -->
833 <!-- type=var -->
835 <!-- name=module -->
841 also accessible via the `exports` module-global. `module` is not actually
846 <!-- YAML
848 -->
856 <!-- YAML
858 -->
871 const EventEmitter = require('node:events');
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 -->
987 * Type: {boolean} `true` if the module is running during the Node.js preload
992 <!-- YAML
994 -->
1003 <!-- YAML
1006 - v14.6.0
1007 - v12.19.0
1008 -->
1010 > Stability: 0 - Deprecated: Please use [`require.main`][] and
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
1083 [GLOBAL_FOLDERS]: #loading-from-the-global-folders
1091 [`import()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import
1095 [`module` core module]: module.md
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