| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 30d6041 commit b8ab533
29 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,16 +25,14 @@ For more info about `node inspect`, see the [debugger][] documentation. | |||
| 25 | 25 | ||
| 26 | 26 | The program entry point is a specifier-like string. If the string is not an | |
| 27 | 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, or by the | ||
| 29 | - [ES module loader][Modules loaders] if [`--experimental-default-type=module`][] | ||
| 30 | - is passed. If no corresponding file is found, an error is thrown. | ||
| 28 | + directory. That path is then resolved by [CommonJS][] module loader. If no | ||
| 29 | + corresponding file is found, an error is thrown. | ||
| 31 | 30 | ||
| 32 | 31 | If a file is found, its path will be passed to the | |
| 33 | 32 | [ES module loader][Modules loaders] under any of the following conditions: | |
| 34 | 33 | ||
| 35 | 34 | * The program was started with a command-line flag that forces the entry | |
| 36 | - point to be loaded with ECMAScript module loader, such as `--import` or | ||
| 37 | - [`--experimental-default-type=module`][]. | ||
| 35 | + point to be loaded with ECMAScript module loader, such as `--import`. | ||
| 38 | 36 | * The file has an `.mjs` extension. | |
| 39 | 37 | * The file does not have a `.cjs` extension, and the nearest parent | |
| 40 | 38 | `package.json` file contains a top-level [`"type"`][] field with a value of | |
@@ -47,9 +45,8 @@ Otherwise, the file is loaded using the CommonJS module loader. See | |||
| 47 | 45 | ||
| 48 | 46 | When loading, the [ES module loader][Modules loaders] loads the program | |
| 49 | 47 | entry point, the `node` command will accept as input only files with `.js`, | |
| 50 | - `.mjs`, or `.cjs` extensions; with `.wasm` extensions when | ||
| 51 | - [`--experimental-wasm-modules`][] is enabled; and with no extension when | ||
| 52 | - [`--experimental-default-type=module`][] is passed. | ||
| 48 | + `.mjs`, or `.cjs` extensions; and with `.wasm` extensions when | ||
| 49 | + [`--experimental-wasm-modules`][] is enabled. | ||
| 53 | 50 | ||
| 54 | 51 | ## Options | |
| 55 | 52 | ||
@@ -910,38 +907,6 @@ flows within the application. As such, it is presently recommended to be sure | |||
| 910 | 907 | your application behaviour is unaffected by this change before using it in | |
| 911 | 908 | production. | |
| 912 | 909 | ||
| 913 | - ### `--experimental-default-type=type` | ||
| 914 | - | ||
| 915 | - <!-- YAML | ||
| 916 | - added: | ||
| 917 | - - v21.0.0 | ||
| 918 | - - v20.10.0 | ||
| 919 | - - v18.19.0 | ||
| 920 | - --> | ||
| 921 | - | ||
| 922 | - > Stability: 1.0 - Early development | ||
| 923 | - | ||
| 924 | - Define which module system, `module` or `commonjs`, to use for the following: | ||
| 925 | - | ||
| 926 | - * String input provided via `--eval` or STDIN, if `--input-type` is unspecified. | ||
| 927 | - | ||
| 928 | - * Files ending in `.js` or with no extension, if there is no `package.json` file | ||
| 929 | - present in the same folder or any parent folder. | ||
| 930 | - | ||
| 931 | - * Files ending in `.js` or with no extension, if the nearest parent | ||
| 932 | - `package.json` field lacks a `"type"` field; unless the `package.json` folder | ||
| 933 | - or any parent folder is inside a `node_modules` folder. | ||
| 934 | - | ||
| 935 | - In other words, `--experimental-default-type=module` flips all the places where | ||
| 936 | - Node.js currently defaults to CommonJS to instead default to ECMAScript modules, | ||
| 937 | - with the exception of folders and subfolders below `node_modules`, for backward | ||
| 938 | - compatibility. | ||
| 939 | - | ||
| 940 | - Under `--experimental-default-type=module` and `--experimental-wasm-modules`, | ||
| 941 | - files with no extension will be treated as WebAssembly if they begin with the | ||
| 942 | - WebAssembly magic number (`\0asm`); otherwise they will be treated as ES module | ||
| 943 | - JavaScript. | ||
| 944 | - | ||
| 945 | 910 | ### `--experimental-transform-types` | |
| 946 | 911 | ||
| 947 | 912 | <!-- YAML | |
@@ -1418,7 +1383,7 @@ added: v12.0.0 | |||
| 1418 | 1383 | ||
| 1419 | 1384 | This configures Node.js to interpret `--eval` or `STDIN` input as CommonJS or | |
| 1420 | 1385 | as an ES module. Valid values are `"commonjs"` or `"module"`. The default is | |
| 1421 | - `"commonjs"` unless [`--experimental-default-type=module`][] is used. | ||
| 1386 | + `"commonjs"`. | ||
| 1422 | 1387 | ||
| 1423 | 1388 | The REPL does not support this option. Usage of `--input-type=module` with | |
| 1424 | 1389 | [`--print`][] will throw an error, as `--print` does not support ES module | |
@@ -3059,7 +3024,6 @@ one is included in the list below. | |||
| 3059 | 3024 | * `--entry-url` | |
| 3060 | 3025 | * `--experimental-abortcontroller` | |
| 3061 | 3026 | * `--experimental-async-context-frame` | |
| 3062 | - * `--experimental-default-type` | ||
| 3063 | 3027 | * `--experimental-detect-module` | |
| 3064 | 3028 | * `--experimental-eventsource` | |
| 3065 | 3029 | * `--experimental-import-meta-resolve` | |
@@ -3621,7 +3585,6 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12 | |||
| 3621 | 3585 | [`--diagnostic-dir`]: #--diagnostic-dirdirectory | |
| 3622 | 3586 | [`--env-file-if-exists`]: #--env-file-if-existsconfig | |
| 3623 | 3587 | [`--env-file`]: #--env-fileconfig | |
| 3624 | - [`--experimental-default-type=module`]: #--experimental-default-typetype | ||
| 3625 | 3588 | [`--experimental-sea-config`]: single-executable-applications.md#generating-single-executable-preparation-blobs | |
| 3626 | 3589 | [`--experimental-strip-types`]: #--experimental-strip-types | |
| 3627 | 3590 | [`--experimental-wasm-modules`]: #--experimental-wasm-modules | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -122,14 +122,13 @@ Node.js has two module systems: [CommonJS][] modules and ECMAScript modules. | |||
| 122 | 122 | ||
| 123 | 123 | Authors can tell Node.js to interpret JavaScript as an ES module via the `.mjs` | |
| 124 | 124 | file extension, the `package.json` [`"type"`][] field with a value `"module"`, | |
| 125 | - the [`--input-type`][] flag with a value of `"module"`, or the | ||
| 126 | - [`--experimental-default-type`][] flag with a value of `"module"`. These are | ||
| 127 | - explicit markers of code being intended to run as an ES module. | ||
| 125 | + or the [`--input-type`][] flag with a value of `"module"`. These are explicit | ||
| 126 | + markers of code being intended to run as an ES module. | ||
| 128 | 127 | ||
| 129 | - Inversely, authors can tell Node.js to interpret JavaScript as CommonJS via the | ||
| 130 | - `.cjs` file extension, the `package.json` [`"type"`][] field with a value | ||
| 131 | - `"commonjs"`, the [`--input-type`][] flag with a value of `"commonjs"`, or the | ||
| 132 | - [`--experimental-default-type`][] flag with a value of `"commonjs"`. | ||
| 128 | + Inversely, authors can explicitly tell Node.js to interpret JavaScript as | ||
| 129 | + CommonJS via the `.cjs` file extension, the `package.json` [`"type"`][] field | ||
| 130 | + with a value `"commonjs"`, or the [`--input-type`][] flag with a value of | ||
| 131 | + `"commonjs"`. | ||
| 133 | 132 | ||
| 134 | 133 | When code lacks explicit markers for either module system, Node.js will inspect | |
| 135 | 134 | the source code of a module to look for ES module syntax. If such syntax is | |
@@ -1120,7 +1119,6 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][]. | |||
| 1120 | 1119 | [URL]: https://url.spec.whatwg.org/ | |
| 1121 | 1120 | [`"exports"`]: packages.md#exports | |
| 1122 | 1121 | [`"type"`]: packages.md#type | |
| 1123 | - [`--experimental-default-type`]: cli.md#--experimental-default-typetype | ||
| 1124 | 1122 | [`--input-type`]: cli.md#--input-typetype | |
| 1125 | 1123 | [`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs | |
| 1126 | 1124 | [`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -980,8 +980,7 @@ Omitting vs providing a `source` for `'commonjs'` has very different effects: | |||
| 980 | 980 | registered hooks. This behavior for nullish `source` is temporary — in the | |
| 981 | 981 | future, nullish `source` will not be supported. | |
| 982 | 982 | ||
| 983 | - When `node` is run with `--experimental-default-type=commonjs`, the Node.js | ||
| 984 | - internal `load` implementation, which is the value of `next` for the | ||
| 983 | + The Node.js internal `load` implementation, which is the value of `next` for the | ||
| 985 | 984 | last hook in the `load` chain, returns `null` for `source` when `format` is | |
| 986 | 985 | `'commonjs'` for backward compatibility. Here is an example hook that would | |
| 987 | 986 | opt-in to using the non-default behavior: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -73,10 +73,9 @@ expressions: | |||
| 73 | 73 | `import` or `export` statements or `import.meta`, with no explicit marker of | |
| 74 | 74 | how it should be interpreted. Explicit markers are `.mjs` or `.cjs` | |
| 75 | 75 | extensions, `package.json` `"type"` fields with either `"module"` or | |
| 76 | - `"commonjs"` values, or `--input-type` or `--experimental-default-type` flags. | ||
| 77 | - Dynamic `import()` expressions are supported in either CommonJS or ES modules | ||
| 78 | - and would not force a file to be treated as an ES module. See | ||
| 79 | - [Syntax detection][]. | ||
| 76 | + `"commonjs"` values, or the `--input-type` flag. Dynamic `import()` | ||
| 77 | + expressions are supported in either CommonJS or ES modules and would not force | ||
| 78 | + a file to be treated as an ES module. See [Syntax detection][]. | ||
| 80 | 79 | ||
| 81 | 80 | Node.js will treat the following as [CommonJS][] when passed to `node` as the | |
| 82 | 81 | initial input, or when referenced by `import` statements or `import()` | |
@@ -90,30 +89,21 @@ expressions: | |||
| 90 | 89 | * Strings passed in as an argument to `--eval` or `--print`, or piped to `node` | |
| 91 | 90 | via `STDIN`, with the flag `--input-type=commonjs`. | |
| 92 | 91 | ||
| 93 | - Aside from these explicit cases, there are other cases where Node.js defaults to | ||
| 94 | - one module system or the other based on the value of the | ||
| 95 | - [`--experimental-default-type`][] flag: | ||
| 96 | - | ||
| 97 | - * Files ending in `.js` or with no extension, if there is no `package.json` file | ||
| 98 | - present in the same folder or any parent folder. | ||
| 99 | - | ||
| 100 | - * Files ending in `.js` or with no extension, if the nearest parent | ||
| 101 | - `package.json` field lacks a `"type"` field; unless the folder is inside a | ||
| 102 | - `node_modules` folder. (Package scopes under `node_modules` are always treated | ||
| 103 | - as CommonJS when the `package.json` file lacks a `"type"` field, regardless | ||
| 104 | - of `--experimental-default-type`, for backward compatibility.) | ||
| 105 | - | ||
| 106 | - * Strings passed in as an argument to `--eval` or piped to `node` via `STDIN`, | ||
| 107 | - when `--input-type` is unspecified. | ||
| 108 | - | ||
| 109 | - This flag currently defaults to `"commonjs"`, but it may change in the future to | ||
| 110 | - default to `"module"`. For this reason it is best to be explicit wherever | ||
| 111 | - possible; in particular, package authors should always include the [`"type"`][] | ||
| 112 | - field in their `package.json` files, even in packages where all sources are | ||
| 113 | - CommonJS. Being explicit about the `type` of the package will future-proof the | ||
| 114 | - package in case the default type of Node.js ever changes, and it will also make | ||
| 115 | - things easier for build tools and loaders to determine how the files in the | ||
| 116 | - package should be interpreted. | ||
| 92 | + * Files with a `.js` extension with no parent `package.json` file or where the | ||
| 93 | + nearest parent `package.json` file lacks a `type` field, and where the code | ||
| 94 | + can evaluate successfully as CommonJS. In other words, Node.js tries to run | ||
| 95 | + such "ambiguous" files as CommonJS first, and will retry evaluating them as ES | ||
| 96 | + modules if the evaluation as CommonJS fails because the parser found ES module | ||
| 97 | + syntax. | ||
| 98 | + | ||
| 99 | + Writing ES module syntax in "ambiguous" files incurs a performance cost, and | ||
| 100 | + therefore it is encouraged that authors be explicit wherever possible. In | ||
| 101 | + particular, package authors should always include the [`"type"`][] field in | ||
| 102 | + their `package.json` files, even in packages where all sources are CommonJS. | ||
| 103 | + Being explicit about the `type` of the package will future-proof the package in | ||
| 104 | + case the default type of Node.js ever changes, and it will also make things | ||
| 105 | + easier for build tools and loaders to determine how the files in the package | ||
| 106 | + should be interpreted. | ||
| 117 | 107 | ||
| 118 | 108 | ### Syntax detection | |
| 119 | 109 | ||
@@ -137,10 +127,8 @@ as an ES module. | |||
| 137 | 127 | Ambiguous input is defined as: | |
| 138 | 128 | ||
| 139 | 129 | * Files with a `.js` extension or no extension; and either no controlling | |
| 140 | - `package.json` file or one that lacks a `type` field; and | ||
| 141 | - `--experimental-default-type` is not specified. | ||
| 142 | - * String input (`--eval` or STDIN) when neither `--input-type` nor | ||
| 143 | - `--experimental-default-type` are specified. | ||
| 130 | + `package.json` file or one that lacks a `type` field. | ||
| 131 | + * String input (`--eval` or `STDIN`) when `--input-type`is not specified. | ||
| 144 | 132 | ||
| 145 | 133 | ES module syntax is defined as syntax that would throw when evaluated as | |
| 146 | 134 | CommonJS. This includes the following: | |
@@ -1162,7 +1150,6 @@ This field defines [subpath imports][] for the current package. | |||
| 1162 | 1150 | [`"packageManager"`]: #packagemanager | |
| 1163 | 1151 | [`"type"`]: #type | |
| 1164 | 1152 | [`--conditions` / `-C` flag]: #resolving-user-conditions | |
| 1165 | - [`--experimental-default-type`]: cli.md#--experimental-default-typetype | ||
| 1166 | 1153 | [`--no-addons` flag]: cli.md#--no-addons | |
| 1167 | 1154 | [`ERR_PACKAGE_PATH_NOT_EXPORTED`]: errors.md#err_package_path_not_exported | |
| 1168 | 1155 | [`package.json`]: #nodejs-packagejson-field-definitions | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -163,11 +163,6 @@ Enable Source Map V3 support for stack traces. | |||
| 163 | 163 | .It Fl -entry-url | |
| 164 | 164 | Interpret the entry point as a URL. | |
| 165 | 165 | . | |
| 166 | - .It Fl -experimental-default-type Ns = Ns Ar type | ||
| 167 | - Interpret as either ES modules or CommonJS modules input via --eval or STDIN, when --input-type is unspecified; | ||
| 168 | - .js or extensionless files with no sibling or parent package.json; | ||
| 169 | - .js or extensionless files whose nearest parent package.json lacks a "type" field, unless under node_modules. | ||
| 170 | - . | ||
| 171 | 166 | .It Fl -experimental-import-meta-resolve | |
| 172 | 167 | Enable experimental ES modules support for import.meta.resolve(). | |
| 173 | 168 | . | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,9 +59,7 @@ function loadESMIfNeeded(cb) { | |||
| 59 | 59 | async function checkSyntax(source, filename) { | |
| 60 | 60 | let format; | |
| 61 | 61 | if (filename === '[stdin]' || filename === '[eval]') { | |
| 62 | - format = (getOptionValue('--input-type') === 'module' || | ||
| 63 | - (getOptionValue('--experimental-default-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) ? | ||
| 64 | - 'module' : 'commonjs'; | ||
| 62 | + format = (getOptionValue('--input-type') === 'module') ? 'module' : 'commonjs'; | ||
| 65 | 63 | } else { | |
| 66 | 64 | const { defaultResolve } = require('internal/modules/esm/resolve'); | |
| 67 | 65 | const { defaultGetFormat } = require('internal/modules/esm/get_format'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,8 +25,7 @@ readStdin((code) => { | |||
| 25 | 25 | ||
| 26 | 26 | const print = getOptionValue('--print'); | |
| 27 | 27 | const shouldLoadESM = getOptionValue('--import').length > 0; | |
| 28 | - if (getOptionValue('--input-type') === 'module' || | ||
| 29 | - (getOptionValue('--experimental-default-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) { | ||
| 28 | + if (getOptionValue('--input-type') === 'module') { | ||
| 30 | 29 | evalModuleEntryPoint(code, print); | |
| 31 | 30 | } else { | |
| 32 | 31 | evalScript('[stdin]', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,8 +29,7 @@ const source = getOptionValue('--experimental-strip-types') ? | |||
| 29 | 29 | ||
| 30 | 30 | const print = getOptionValue('--print'); | |
| 31 | 31 | const shouldLoadESM = getOptionValue('--import').length > 0 || getOptionValue('--experimental-loader').length > 0; | |
| 32 | - if (getOptionValue('--input-type') === 'module' || | ||
| 33 | - (getOptionValue('--experimental-default-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) { | ||
| 32 | + if (getOptionValue('--input-type') === 'module') { | ||
| 34 | 33 | evalModuleEntryPoint(source, print); | |
| 35 | 34 | } else { | |
| 36 | 35 | // For backward compatibility, we want the identifier crypto to be the | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,15 +23,11 @@ if (isEntryURL) { | |||
| 23 | 23 | emitExperimentalWarning('--entry-url'); | |
| 24 | 24 | } | |
| 25 | 25 | ||
| 26 | - if (getOptionValue('--experimental-default-type') === 'module') { | ||
| 27 | - require('internal/modules/run_main').executeUserEntryPoint(mainEntry); | ||
| 28 | - } else { | ||
| 29 | - /** | ||
| 30 | - * To support legacy monkey-patching of `Module.runMain`, we call `runMain` here to have the CommonJS loader begin | ||
| 31 | - * the execution of the main entry point, even if the ESM loader immediately takes over because the main entry is an | ||
| 32 | - * ES module or one of the other opt-in conditions (such as the use of `--import`) are met. Users can monkey-patch | ||
| 33 | - * before the main entry point is loaded by doing so via scripts loaded through `--require`. This monkey-patchability | ||
| 34 | - * is undesirable and is removed in `--experimental-default-type=module` mode. | ||
| 35 | - */ | ||
| 36 | - require('internal/modules/cjs/loader').Module.runMain(mainEntry); | ||
| 37 | - } | ||
| 26 | + /** | ||
| 27 | + * To support legacy monkey-patching of `Module.runMain`, we call `runMain` here to have the CommonJS loader begin the | ||
| 28 | + * execution of the main entry point, even if the ESM loader immediately takes over because the main entry is an ES | ||
| 29 | + * module or one of the other opt-in conditions (such as the use of `--import`) are met. Users can monkey-patch before | ||
| 30 | + * the main entry point is loaded by doing so via scripts loaded through `--require`. This monkey-patchability is | ||
| 31 | + * undesirable and should be removed once the module customization hooks provide equivalent functionality. | ||
| 32 | + */ | ||
| 33 | + require('internal/modules/cjs/loader').Module.runMain(mainEntry); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -46,8 +46,8 @@ function mimeToFormat(mime) { | |||
| 46 | 46 | } | |
| 47 | 47 | ||
| 48 | 48 | /** | |
| 49 | - * For extensionless files in a `module` package scope, or a default `module` scope enabled by the | ||
| 50 | - * `--experimental-default-type` flag, we check the file contents to disambiguate between ES module JavaScript and Wasm. | ||
| 49 | + * For extensionless files in a `module` package scope, we check the file contents to disambiguate between ES module | ||
| 50 | + * JavaScript and Wasm. | ||
| 51 | 51 | * We do this by taking advantage of the fact that all Wasm files start with the header `0x00 0x61 0x73 0x6d` (`_asm`). | |
| 52 | 52 | * @param {URL} url | |
| 53 | 53 | */ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments