| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ef7c798 commit 8429216
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,4 @@ | |||
| 1 | - # Command-line options | ||
| 1 | + # Command-line API | ||
| 2 | 2 | ||
| 3 | 3 | <!--introduced_in=v5.9.1--> | |
| 4 | 4 | ||
@@ -11,16 +11,43 @@ To view this documentation as a manual page in a terminal, run `man node`. | |||
| 11 | 11 | ||
| 12 | 12 | ## Synopsis | |
| 13 | 13 | ||
| 14 | - `node [options] [V8 options] [script.js | -e "script" | -] [--] [arguments]` | ||
| 14 | + `node [options] [V8 options] [<program-entry-point> | -e "script" | -] [--] [arguments]` | ||
| 15 | 15 | ||
| 16 | - `node inspect [script.js | -e "script" | <host>:<port>] …` | ||
| 16 | + `node inspect [<program-entry-point> | -e "script" | <host>:<port>] …` | ||
| 17 | 17 | ||
| 18 | 18 | `node --v8-options` | |
| 19 | 19 | ||
| 20 | 20 | Execute without arguments to start the [REPL][]. | |
| 21 | 21 | ||
| 22 | 22 | For more info about `node inspect`, see the [debugger][] documentation. | |
| 23 | 23 | ||
| 24 | + ## Program entry point | ||
| 25 | + | ||
| 26 | + The program entry point is a specifier-like string. If the string is not an | ||
| 27 | + absolute path, it's resolved as a relative path from the current working | ||
| 28 | + directory. That path is then resolved by [CommonJS][] module loader. If no | ||
| 29 | + corresponding file is found, an error is thrown. | ||
| 30 | + | ||
| 31 | + If a file is found, its path will be passed to the [ECMAScript module loader][] | ||
| 32 | + under any of the following conditions: | ||
| 33 | + | ||
| 34 | + * The program was started with a command-line flag that forces the entry | ||
| 35 | + point to be loaded with ECMAScript module loader. | ||
| 36 | + * The file has an `.mjs` extension. | ||
| 37 | + * The file does not have a `.cjs` extension, and the nearest parent | ||
| 38 | + `package.json` file contains a top-level [`"type"`][] field with a value of | ||
| 39 | + `"module"`. | ||
| 40 | + | ||
| 41 | + Otherwise, the file is loaded using the CommonJS module loader. See | ||
| 42 | + [Modules loaders][] for more details. | ||
| 43 | + | ||
| 44 | + ### ECMAScript modules loader entry point caveat | ||
| 45 | + | ||
| 46 | + When loading [ECMAScript module loader][] loads the program entry point, the `node` | ||
| 47 | + command will only accept as input only files with `.js`, `.mjs`, or `.cjs` | ||
| 48 | + extensions; and with `.wasm` extensions when | ||
| 49 | + [`--experimental-wasm-modules`][] is enabled. | ||
| 50 | + | ||
| 24 | 51 | ## Options | |
| 25 | 52 | ||
| 26 | 53 | <!-- YAML | |
@@ -277,8 +304,8 @@ Enable experimental JSON support for the ES Module loader. | |||
| 277 | 304 | added: v9.0.0 | |
| 278 | 305 | --> | |
| 279 | 306 | ||
| 280 | - Specify the `module` of a custom experimental [ECMAScript Module loader][]. | ||
| 281 | - `module` may be either a path to a file, or an ECMAScript Module name. | ||
| 307 | + Specify the `module` of a custom experimental [ECMAScript module loader][]. | ||
| 308 | + `module` may be any string accepted as an [`import` specifier][]. | ||
| 282 | 309 | ||
| 283 | 310 | ### `--experimental-policy` | |
| 284 | 311 | ||
@@ -1931,15 +1958,19 @@ $ node --max-old-space-size=1536 index.js | |||
| 1931 | 1958 | ``` | |
| 1932 | 1959 | ||
| 1933 | 1960 | [Chrome DevTools Protocol]: https://chromedevtools.github.io/devtools-protocol/ | |
| 1934 | - [ECMAScript Module loader]: esm.md#loaders | ||
| 1961 | + [CommonJS]: modules.md | ||
| 1962 | + [ECMAScript module loader]: esm.md#loaders | ||
| 1963 | + [Modules loaders]: packages.md#modules-loaders | ||
| 1935 | 1964 | [OSSL_PROVIDER-legacy]: https://www.openssl.org/docs/man3.0/man7/OSSL_PROVIDER-legacy.html | |
| 1936 | 1965 | [REPL]: repl.md | |
| 1937 | 1966 | [ScriptCoverage]: https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ScriptCoverage | |
| 1938 | 1967 | [Source Map]: https://sourcemaps.info/spec.html | |
| 1939 | 1968 | [Subresource Integrity]: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity | |
| 1940 | 1969 | [V8 JavaScript code coverage]: https://v8project.blogspot.com/2017/12/javascript-code-coverage.html | |
| 1970 | + [`"type"`]: packages.md#type | ||
| 1941 | 1971 | [`--cpu-prof-dir`]: #--cpu-prof-dir | |
| 1942 | 1972 | [`--diagnostic-dir`]: #--diagnostic-dirdirectory | |
| 1973 | + [`--experimental-wasm-modules`]: #--experimental-wasm-modules | ||
| 1943 | 1974 | [`--heap-prof-dir`]: #--heap-prof-dir | |
| 1944 | 1975 | [`--openssl-config`]: #--openssl-configfile | |
| 1945 | 1976 | [`--redirect-warnings`]: #--redirect-warningsfile | |
@@ -1952,6 +1983,7 @@ $ node --max-old-space-size=1536 index.js | |||
| 1952 | 1983 | [`dns.lookup()`]: dns.md#dnslookuphostname-options-callback | |
| 1953 | 1984 | [`dns.setDefaultResultOrder()`]: dns.md#dnssetdefaultresultorderorder | |
| 1954 | 1985 | [`dnsPromises.lookup()`]: dns.md#dnspromiseslookuphostname-options | |
| 1986 | + [`import` specifier]: esm.md#import-specifiers | ||
| 1955 | 1987 | [`process.setUncaughtExceptionCaptureCallback()`]: process.md#processsetuncaughtexceptioncapturecallbackfn | |
| 1956 | 1988 | [`tls.DEFAULT_MAX_VERSION`]: tls.md#tlsdefault_max_version | |
| 1957 | 1989 | [`tls.DEFAULT_MIN_VERSION`]: tls.md#tlsdefault_min_version | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -93,12 +93,12 @@ provides interoperability between them and its original module format, | |||
| 93 | 93 | ||
| 94 | 94 | <!-- type=misc --> | |
| 95 | 95 | ||
| 96 | - Node.js treats JavaScript code as CommonJS modules by default. | ||
| 97 | - Authors can tell Node.js to treat JavaScript code as ECMAScript modules | ||
| 96 | + Node.js has two module systems: [CommonJS][] modules and ECMAScript modules. | ||
| 97 | + | ||
| 98 | + Authors can tell Node.js to use the ECMAScript modules loader | ||
| 98 | 99 | via the `.mjs` file extension, the `package.json` [`"type"`][] field, or the | |
| 99 | - `--input-type` flag. See | ||
| 100 | - [Modules: Packages](packages.md#determining-module-system) for more | ||
| 101 | - details. | ||
| 100 | + [`--input-type`][] flag. Outside of those cases, Node.js will use the CommonJS | ||
| 101 | + module loader. See [Determining module system][] for more details. | ||
| 102 | 102 | ||
| 103 | 103 | <!-- Anchors to make sure old links find a target --> | |
| 104 | 104 | ||
@@ -1442,6 +1442,7 @@ success! | |||
| 1442 | 1442 | [CommonJS]: modules.md | |
| 1443 | 1443 | [Conditional exports]: packages.md#conditional-exports | |
| 1444 | 1444 | [Core modules]: modules.md#core-modules | |
| 1445 | + [Determining module system]: packages.md#determining-module-system | ||
| 1445 | 1446 | [Dynamic `import()`]: https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports | |
| 1446 | 1447 | [ES Module Integration Proposal for WebAssembly]: https://github.com/webassembly/esm-integration | |
| 1447 | 1448 | [Import Assertions]: #import-assertions | |
@@ -1453,6 +1454,7 @@ success! | |||
| 1453 | 1454 | [WHATWG JSON modules specification]: https://html.spec.whatwg.org/#creating-a-json-module-script | |
| 1454 | 1455 | [`"exports"`]: packages.md#exports | |
| 1455 | 1456 | [`"type"`]: packages.md#type | |
| 1457 | + [`--input-type`]: cli.md#--input-typetype | ||
| 1456 | 1458 | [`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer | |
| 1457 | 1459 | [`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer | |
| 1458 | 1460 | [`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -61,7 +61,38 @@ module.exports = class Square { | |||
| 61 | 61 | }; | |
| 62 | 62 | ``` | |
| 63 | 63 | ||
| 64 | - The module system is implemented in the `require('module')` module. | ||
| 64 | + The CommonJS module system is implemented in the [`module` core module][]. | ||
| 65 | + | ||
| 66 | + ## Enabling | ||
| 67 | + | ||
| 68 | + <!-- type=misc --> | ||
| 69 | + | ||
| 70 | + Node.js has two module systems: CommonJS modules and [ECMAScript modules][]. | ||
| 71 | + | ||
| 72 | + By default, Node.js will treat the following as CommonJS modules: | ||
| 73 | + | ||
| 74 | + * Files with a `.cjs` extension; | ||
| 75 | + | ||
| 76 | + * Files with a `.js` extension when the nearest parent `package.json` file | ||
| 77 | + contains a top-level field [`"type"`][] with a value of `"commonjs"`. | ||
| 78 | + | ||
| 79 | + * Files with a `.js` extension when the nearest parent `package.json` file | ||
| 80 | + doesn't contain a top-level field [`"type"`][]. Package authors should include | ||
| 81 | + the [`"type"`][] field, even in packages where all sources are CommonJS. Being | ||
| 82 | + explicit about the `type` of the package will make things easier for build | ||
| 83 | + tools and loaders to determine how the files in the package should be | ||
| 84 | + interpreted. | ||
| 85 | + | ||
| 86 | + * Files with an extension that is not `.mjs`, `.cjs`, `.json`, `.node`, or `.js` | ||
| 87 | + (when the nearest parent `package.json` file contains a top-level field | ||
| 88 | + [`"type"`][] with a value of `"module"`, those files will be recognized as | ||
| 89 | + CommonJS modules only if they are being `require`d, not when used as the | ||
| 90 | + command-line entry point of the program). | ||
| 91 | + | ||
| 92 | + See [Determining module system][] for more details. | ||
| 93 | + | ||
| 94 | + Calling `require()` always use the CommonJS module loader. Calling `import()` | ||
| 95 | + always use the ECMAScript module loader. | ||
| 65 | 96 | ||
| 66 | 97 | ## Accessing the main module | |
| 67 | 98 | ||
@@ -1047,13 +1078,15 @@ This section was moved to | |||
| 1047 | 1078 | [ECMAScript Modules]: esm.md | |
| 1048 | 1079 | [GLOBAL_FOLDERS]: #loading-from-the-global-folders | |
| 1049 | 1080 | [`"main"`]: packages.md#main | |
| 1081 | + [`"type"`]: packages.md#type | ||
| 1050 | 1082 | [`ERR_REQUIRE_ESM`]: errors.md#err_require_esm | |
| 1051 | 1083 | [`Error`]: errors.md#class-error | |
| 1052 | 1084 | [`__dirname`]: #__dirname | |
| 1053 | 1085 | [`__filename`]: #__filename | |
| 1054 | 1086 | [`import()`]: https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports | |
| 1055 | 1087 | [`module.children`]: #modulechildren | |
| 1056 | 1088 | [`module.id`]: #moduleid | |
| 1089 | + [`module` core module]: module.md | ||
| 1057 | 1090 | [`module` object]: #the-module-object | |
| 1058 | 1091 | [`package.json`]: packages.md#nodejs-packagejson-field-definitions | |
| 1059 | 1092 | [`path.dirname()`]: path.md#pathdirnamepath | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -51,12 +51,13 @@ along with a reference for the [`package.json`][] fields defined by Node.js. | |||
| 51 | 51 | ## Determining module system | |
| 52 | 52 | ||
| 53 | 53 | Node.js will treat the following as [ES modules][] when passed to `node` as the | |
| 54 | - initial input, or when referenced by `import` statements within ES module code: | ||
| 54 | + initial input, or when referenced by `import` statements or `import()` | ||
| 55 | + expressions: | ||
| 55 | 56 | ||
| 56 | - * Files ending in `.mjs`. | ||
| 57 | + * Files with an `.mjs` extension. | ||
| 57 | 58 | ||
| 58 | - * Files ending in `.js` when the nearest parent `package.json` file contains a | ||
| 59 | - top-level [`"type"`][] field with a value of `"module"`. | ||
| 59 | + * Files with a `.js` extension when the nearest parent `package.json` file | ||
| 60 | + contains a top-level [`"type"`][] field with a value of `"module"`. | ||
| 60 | 61 | ||
| 61 | 62 | * Strings passed in as an argument to `--eval`, or piped to `node` via `STDIN`, | |
| 62 | 63 | with the flag `--input-type=module`. | |
@@ -67,12 +68,13 @@ field, or string input without the flag `--input-type`. This behavior is to | |||
| 67 | 68 | preserve backward compatibility. However, now that Node.js supports both | |
| 68 | 69 | CommonJS and ES modules, it is best to be explicit whenever possible. Node.js | |
| 69 | 70 | will treat the following as CommonJS when passed to `node` as the initial input, | |
| 70 | - or when referenced by `import` statements within ES module code: | ||
| 71 | + or when referenced by `import` statements, `import()` expressions, or | ||
| 72 | + `require()` expressions: | ||
| 71 | 73 | ||
| 72 | - * Files ending in `.cjs`. | ||
| 74 | + * Files with a `.cjs` extension. | ||
| 73 | 75 | ||
| 74 | - * Files ending in `.js` when the nearest parent `package.json` file contains a | ||
| 75 | - top-level field [`"type"`][] with a value of `"commonjs"`. | ||
| 76 | + * Files with a `.js` extension when the nearest parent `package.json` file | ||
| 77 | + contains a top-level field [`"type"`][] with a value of `"commonjs"`. | ||
| 76 | 78 | ||
| 77 | 79 | * Strings passed in as an argument to `--eval` or `--print`, or piped to `node` | |
| 78 | 80 | via `STDIN`, with the flag `--input-type=commonjs`. | |
@@ -83,6 +85,48 @@ future-proof the package in case the default type of Node.js ever changes, and | |||
| 83 | 85 | it will also make things easier for build tools and loaders to determine how the | |
| 84 | 86 | files in the package should be interpreted. | |
| 85 | 87 | ||
| 88 | + ### Modules loaders | ||
| 89 | + | ||
| 90 | + Node.js has two systems for resolving a specifier and loading modules. | ||
| 91 | + | ||
| 92 | + There is the CommonJS module loader: | ||
| 93 | + | ||
| 94 | + * It is fully synchronous. | ||
| 95 | + * It is responsible for handling `require()` calls. | ||
| 96 | + * It is monkey patchable. | ||
| 97 | + * It supports [folders as modules][]. | ||
| 98 | + * When resolving a specifier, if no exact match is found, it will try to add | ||
| 99 | + extensions (`.js`, `.json`, and finally `.node`) and then attempt to resolve | ||
| 100 | + [folders as modules][]. | ||
| 101 | + * It treats `.json` as JSON text files. | ||
| 102 | + * `.node` files are interpreted as compiled addon modules loaded with | ||
| 103 | + `process.dlopen()`. | ||
| 104 | + * It treats all files that lack `.json` or `.node` extensions as JavaScript | ||
| 105 | + text files. | ||
| 106 | + * It cannot be used to load ECMAScript modules (although it is possible to | ||
| 107 | + [load ECMASCript modules from CommonJS modules][]). When used to load a | ||
| 108 | + JavaScript text file that is not an ECMAScript module, it loads it as a | ||
| 109 | + CommonJS module. | ||
| 110 | + | ||
| 111 | + There is the ECMAScript module loader: | ||
| 112 | + | ||
| 113 | + * It is asynchronous. | ||
| 114 | + * It is responsible for handling `import` statements and `import()` expressions. | ||
| 115 | + * It is not monkey patchable, can be customized using [loader hooks][]. | ||
| 116 | + * It does not support folders as modules, directory indexes (e.g. | ||
| 117 | + `'./startup/index.js'`) must be fully specified. | ||
| 118 | + * It does no extension searching. A file extension must be provided | ||
| 119 | + when the specifier is a relative or absolute file URL. | ||
| 120 | + * It can load JSON modules, but an import assertion is required (behind | ||
| 121 | + `--experimental-json-modules` flag). | ||
| 122 | + * It accepts only `.js`, `.mjs`, and `.cjs` extensions for JavaScript text | ||
| 123 | + files. | ||
| 124 | + * It can be used to load JavaScript CommonJS modules. Such modules | ||
| 125 | + are passed through the `es-module-lexer` to try to identify named exports, | ||
| 126 | + which are available if they can be determined through static analysis. | ||
| 127 | + Imported CommonJS modules have their URLs converted to absolute | ||
| 128 | + paths and are then loaded via the CommonJS module loader. | ||
| 129 | + | ||
| 86 | 130 | ### `package.json` and file extensions | |
| 87 | 131 | ||
| 88 | 132 | Within a package, the [`package.json`][] [`"type"`][] field defines how | |
@@ -1236,6 +1280,9 @@ This field defines [subpath imports][] for the current package. | |||
| 1236 | 1280 | [`esm`]: https://github.com/standard-things/esm#readme | |
| 1237 | 1281 | [`package.json`]: #nodejs-packagejson-field-definitions | |
| 1238 | 1282 | [entry points]: #package-entry-points | |
| 1283 | + [folders as modules]: modules.md#folders-as-modules | ||
| 1284 | + [load ECMASCript modules from CommonJS modules]: modules.md#the-mjs-extension | ||
| 1285 | + [loader hooks]: esm.md#loaders | ||
| 1239 | 1286 | [self-reference]: #self-referencing-a-package-using-its-name | |
| 1240 | 1287 | [subpath exports]: #subpath-exports | |
| 1241 | 1288 | [subpath imports]: #subpath-imports | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -88,6 +88,6 @@ Now, open any preferred web browser and visit `http://127.0.0.1:3000`. | |||
| 88 | 88 | If the browser displays the string `Hello, World!`, that indicates | |
| 89 | 89 | the server is working. | |
| 90 | 90 | ||
| 91 | - [Command-line options]: cli.md#command-line-options | ||
| 91 | + [Command-line options]: cli.md#options | ||
| 92 | 92 | [Installing Node.js via package manager]: https://nodejs.org/en/download/package-manager/ | |
| 93 | 93 | [web server]: http.md | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | { | |
| 2 | 2 | "doc/api/synopsis.md": { | |
| 3 | - "command line options": "cli.html#command-line-options", | ||
| 3 | + "command line options": "cli.html#options", | ||
| 4 | 4 | "web server": "http.html" | |
| 5 | 5 | } | |
| 6 | - } | ||
| 6 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments