Skip to content

Commit 363028c

Browse files
GeoffreyBoothdanielleadams
authored andcommitted
module: unflag esm json modules
PR-URL: #41736 Backport-PR-URL: #42726 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Bradley Farias <[email protected]>
1 parent f8819e8 commit 363028c

23 files changed

+41
-114
lines changed

doc/api/cli.md

-8
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,6 @@ added:
282282

283283
Enable experimental `import.meta.resolve()` support.
284284

285-
### `--experimental-json-modules`
286-
287-
<!-- YAML
288-
added: v12.9.0
289-
-->
290-
291-
Enable experimental JSON support for the ES Module loader.
292-
293285
### `--experimental-loader=module`
294286

295287
<!-- YAML

doc/api/esm.md

+6-39
Original file line numberDiff line numberDiff line change
@@ -471,22 +471,6 @@ These CommonJS variables are not available in ES modules.
471471
`__filename` and `__dirname` use cases can be replicated via
472472
[`import.meta.url`][].
473473
474-
#### No JSON Module Loading
475-
476-
JSON imports are still experimental and only supported via the
477-
`--experimental-json-modules` flag.
478-
479-
Local JSON files can be loaded relative to `import.meta.url` with `fs` directly:
480-
481-
<!-- eslint-skip -->
482-
483-
```js
484-
import { readFile } from 'fs/promises';
485-
const json = JSON.parse(await readFile(new URL('./dat.json', import.meta.url)));
486-
```
487-
488-
Alternatively `module.createRequire()` can be used.
489-
490474
#### No Native Module Loading
491475
492476
Native modules are not currently supported with ES module imports.
@@ -524,35 +508,19 @@ separate cache.
524508
525509
> Stability: 1 - Experimental
526510
527-
Currently importing JSON modules are only supported in the `commonjs` mode
528-
and are loaded using the CJS loader. [WHATWG JSON modules specification][] are
529-
still being standardized, and are experimentally supported by including the
530-
additional flag `--experimental-json-modules` when running Node.js.
531-
532-
When the `--experimental-json-modules` flag is included, both the
533-
`commonjs` and `module` mode use the new experimental JSON
534-
loader. The imported JSON only exposes a `default`. There is no
535-
support for named exports. A cache entry is created in the CommonJS
536-
cache to avoid duplication. The same object is returned in
537-
CommonJS if the JSON module has already been imported from the
538-
same path.
539-
540-
Assuming an `index.mjs` with
511+
JSON files can be referenced by `import`:
541512
542513
```js
543514
import packageConfig from './package.json' assert { type: 'json' };
544515
```
545516
546-
The `--experimental-json-modules` flag is needed for the module
547-
to work.
548-
549-
```bash
550-
node index.mjs # fails
551-
node --experimental-json-modules index.mjs # works
552-
```
553-
554517
The `assert { type: 'json' }` syntax is mandatory; see [Import Assertions][].
555518
519+
The imported JSON only exposes a `default` export. There is no support for named
520+
exports. A cache entry is created in the CommonJS cache to avoid duplication.
521+
The same object is returned in CommonJS if the JSON module has already been
522+
imported from the same path.
523+
556524
<i id="esm_experimental_wasm_modules"></i>
557525
558526
## Wasm modules
@@ -1467,7 +1435,6 @@ success!
14671435
[Node.js Module Resolution Algorithm]: #resolver-algorithm-specification
14681436
[Terminology]: #terminology
14691437
[URL]: https://url.spec.whatwg.org/
1470-
[WHATWG JSON modules specification]: https://html.spec.whatwg.org/#creating-a-json-module-script
14711438
[`"exports"`]: packages.md#exports
14721439
[`"type"`]: packages.md#type
14731440
[`--input-type`]: cli.md#--input-typetype

doc/api/packages.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ There is the ECMAScript module loader:
117117
`'./startup/index.js'`) must be fully specified.
118118
* It does no extension searching. A file extension must be provided
119119
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).
120+
* It can load JSON modules, but an import assertion is required.
122121
* It accepts only `.js`, `.mjs`, and `.cjs` extensions for JavaScript text
123122
files.
124123
* It can be used to load JavaScript CommonJS modules. Such modules

doc/node.1

-3
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,6 @@ Enable Source Map V3 support for stack traces.
142142
.It Fl -experimental-import-meta-resolve
143143
Enable experimental ES modules support for import.meta.resolve().
144144
.
145-
.It Fl -experimental-json-modules
146-
Enable experimental JSON interop support for the ES Module loader.
147-
.
148145
.It Fl -experimental-loader Ns = Ns Ar module
149146
Specify the
150147
.Ar module

lib/internal/modules/esm/get_format.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const {
88
const { extname } = require('path');
99
const { getOptionValue } = require('internal/options');
1010

11-
const experimentalJsonModules = getOptionValue('--experimental-json-modules');
1211
const experimentalSpecifierResolution =
1312
getOptionValue('--experimental-specifier-resolution');
1413
const experimentalWasmModules = getOptionValue('--experimental-wasm-modules');
@@ -20,7 +19,8 @@ const extensionFormatMap = {
2019
'__proto__': null,
2120
'.cjs': 'commonjs',
2221
'.js': 'module',
23-
'.mjs': 'module'
22+
'.json': 'json',
23+
'.mjs': 'module',
2424
};
2525

2626
const legacyExtensionFormatMap = {
@@ -29,17 +29,14 @@ const legacyExtensionFormatMap = {
2929
'.js': 'commonjs',
3030
'.json': 'commonjs',
3131
'.mjs': 'module',
32-
'.node': 'commonjs'
32+
'.node': 'commonjs',
3333
};
3434

3535
let experimentalSpecifierResolutionWarned = false;
3636

3737
if (experimentalWasmModules)
3838
extensionFormatMap['.wasm'] = legacyExtensionFormatMap['.wasm'] = 'wasm';
3939

40-
if (experimentalJsonModules)
41-
extensionFormatMap['.json'] = legacyExtensionFormatMap['.json'] = 'json';
42-
4340
const protocolHandlers = ObjectAssign(ObjectCreate(null), {
4441
'data:'(parsed) {
4542
const { 1: mime } = RegExpPrototypeExec(
@@ -49,7 +46,7 @@ const protocolHandlers = ObjectAssign(ObjectCreate(null), {
4946
const format = ({
5047
'__proto__': null,
5148
'text/javascript': 'module',
52-
'application/json': experimentalJsonModules ? 'json' : null,
49+
'application/json': 'json',
5350
'application/wasm': experimentalWasmModules ? 'wasm' : null
5451
})[mime] || null;
5552

src/node_options.cc

+2-8
Original file line numberDiff line numberDiff line change
@@ -312,19 +312,13 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
312312
kAllowedInEnvironment);
313313
AddOption("--experimental-abortcontroller", "",
314314
NoOp{}, kAllowedInEnvironment);
315-
AddOption("--experimental-json-modules",
316-
"experimental JSON interop support for the ES Module loader",
317-
&EnvironmentOptions::experimental_json_modules,
318-
kAllowedInEnvironment);
315+
AddOption("--experimental-json-modules", "", NoOp{}, kAllowedInEnvironment);
319316
AddOption("--experimental-loader",
320317
"use the specified module as a custom loader",
321318
&EnvironmentOptions::userland_loader,
322319
kAllowedInEnvironment);
323320
AddAlias("--loader", "--experimental-loader");
324-
AddOption("--experimental-modules",
325-
"",
326-
&EnvironmentOptions::experimental_modules,
327-
kAllowedInEnvironment);
321+
AddOption("--experimental-modules", "", NoOp{}, kAllowedInEnvironment);
328322
AddOption("--experimental-wasm-modules",
329323
"experimental ES Module support for webassembly modules",
330324
&EnvironmentOptions::experimental_wasm_modules,

src/node_options.h

-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ class EnvironmentOptions : public Options {
103103
std::vector<std::string> conditions;
104104
std::string dns_result_order;
105105
bool enable_source_maps = false;
106-
bool experimental_json_modules = false;
107-
bool experimental_modules = false;
108106
std::string experimental_specifier_resolution;
109107
bool experimental_wasm_modules = false;
110108
bool experimental_import_meta_resolve = false;

test/es-module/test-esm-assertionless-json-import.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Flags: --experimental-json-modules --experimental-loader ./test/fixtures/es-module-loaders/assertionless-json-import.mjs
1+
// Flags: --experimental-loader ./test/fixtures/es-module-loaders/assertionless-json-import.mjs
22
'use strict';
33
const common = require('../common');
44
const { strictEqual } = require('assert');

test/es-module/test-esm-data-urls.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Flags: --experimental-json-modules
21
'use strict';
32
const common = require('../common');
43
const assert = require('assert');

test/es-module/test-esm-dynamic-import-assertion.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Flags: --experimental-json-modules
21
'use strict';
32
const common = require('../common');
43
const { strictEqual } = require('assert');

test/es-module/test-esm-dynamic-import-assertion.mjs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Flags: --experimental-json-modules
21
import '../common/index.mjs';
32
import { strictEqual } from 'assert';
43

test/es-module/test-esm-import-assertion-1.mjs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Flags: --experimental-json-modules
21
import '../common/index.mjs';
32
import { strictEqual } from 'assert';
43

test/es-module/test-esm-import-assertion-2.mjs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Flags: --experimental-json-modules
21
import '../common/index.mjs';
32
import { strictEqual } from 'assert';
43

test/es-module/test-esm-import-assertion-3.mjs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Flags: --experimental-json-modules
21
import '../common/index.mjs';
32
import { strictEqual } from 'assert';
43

test/es-module/test-esm-import-assertion-4.mjs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Flags: --experimental-json-modules
21
import '../common/index.mjs';
32
import { strictEqual } from 'assert';
43

test/es-module/test-esm-import-assertion-errors.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Flags: --experimental-json-modules
21
'use strict';
32
const common = require('../common');
43
const { rejects } = require('assert');

test/es-module/test-esm-import-assertion-errors.mjs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Flags: --experimental-json-modules
21
import '../common/index.mjs';
32
import { rejects } from 'assert';
43

test/es-module/test-esm-import-json-named-export.mjs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { spawn } from 'child_process';
55
import { execPath } from 'process';
66

77
const child = spawn(execPath, [
8-
'--experimental-json-modules',
98
path('es-modules', 'import-json-named-export.mjs'),
109
]);
1110

test/es-module/test-esm-invalid-data-urls.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@ const assert = require('assert');
55
(async () => {
66
await assert.rejects(import('data:text/plain,export default0'), {
77
code: 'ERR_INVALID_MODULE_SPECIFIER',
8-
message:
9-
'Invalid module "data:text/plain,export default0" has an unsupported ' +
10-
'MIME type "text/plain"',
8+
message: 'Invalid module "data:text/plain,export default0" has an unsupported MIME type "text/plain"',
119
});
1210
await assert.rejects(import('data:text/plain;base64,'), {
1311
code: 'ERR_INVALID_MODULE_SPECIFIER',
14-
message:
15-
'Invalid module "data:text/plain;base64," has an unsupported ' +
16-
'MIME type "text/plain"',
12+
message: 'Invalid module "data:text/plain;base64," has an unsupported MIME type "text/plain"',
1713
});
18-
await assert.rejects(import('data:application/json,[]'), {
14+
await assert.rejects(import('data:text/css,.error { color: red; }'), {
1915
code: 'ERR_INVALID_MODULE_SPECIFIER',
20-
message:
21-
'Invalid module "data:application/json,[]" has an unsupported ' +
22-
'MIME type "application/json"',
16+
message: 'Invalid module "data:text/css,.error { color: red; }" has an unsupported MIME type "text/css"',
2317
});
2418
})().then(common.mustCall());

test/es-module/test-esm-json-cache.mjs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Flags: --experimental-json-modules
21
import '../common/index.mjs';
32

43
import { strictEqual, deepStrictEqual } from 'assert';

test/es-module/test-esm-json.mjs

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Flags: --experimental-json-modules
21
import '../common/index.mjs';
32
import { path } from '../common/fixtures.mjs';
43
import { strictEqual, ok } from 'assert';
@@ -10,7 +9,6 @@ strictEqual(secret.ofLife, 42);
109

1110
// Test warning message
1211
const child = spawn(process.execPath, [
13-
'--experimental-json-modules',
1412
path('/es-modules/json-modules.mjs'),
1513
]);
1614

test/es-module/test-esm-non-js.js

-21
This file was deleted.

test/es-module/test-esm-non-js.mjs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { mustCall } from '../common/index.mjs';
2+
import { fileURL } from '../common/fixtures.mjs';
3+
import { match, strictEqual } from 'assert';
4+
import { spawn } from 'child_process';
5+
import { execPath } from 'process';
6+
7+
// Verify non-js extensions fail for ESM
8+
const child = spawn(execPath, [
9+
'--input-type=module',
10+
'--eval',
11+
`import ${JSON.stringify(fileURL('es-modules', 'file.unknown'))}`,
12+
]);
13+
14+
let stderr = '';
15+
child.stderr.setEncoding('utf8');
16+
child.stderr.on('data', (data) => {
17+
stderr += data;
18+
});
19+
child.on('close', mustCall((code, signal) => {
20+
strictEqual(code, 1);
21+
strictEqual(signal, null);
22+
match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/);
23+
}));

0 commit comments

Comments
 (0)