Skip to content

Commit 9c6b078

Browse files
GeoffreyBoothruyadorno
authored andcommitted
module: unflag esm json modules
PR-URL: #41736 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 9c704b1 commit 9c6b078

23 files changed

+41
-114
lines changed

doc/api/cli.md

-8
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,6 @@ added:
290290

291291
Enable experimental `import.meta.resolve()` support.
292292

293-
### `--experimental-json-modules`
294-
295-
<!-- YAML
296-
added: v12.9.0
297-
-->
298-
299-
Enable experimental JSON support for the ES Module loader.
300-
301293
### `--experimental-loader=module`
302294

303295
<!-- YAML

doc/api/esm.md

+6-39
Original file line numberDiff line numberDiff line change
@@ -476,22 +476,6 @@ These CommonJS variables are not available in ES modules.
476476
`__filename` and `__dirname` use cases can be replicated via
477477
[`import.meta.url`][].
478478
479-
#### No JSON Module Loading
480-
481-
JSON imports are still experimental and only supported via the
482-
`--experimental-json-modules` flag.
483-
484-
Local JSON files can be loaded relative to `import.meta.url` with `fs` directly:
485-
486-
<!-- eslint-skip -->
487-
488-
```js
489-
import { readFile } from 'fs/promises';
490-
const json = JSON.parse(await readFile(new URL('./dat.json', import.meta.url)));
491-
```
492-
493-
Alternatively `module.createRequire()` can be used.
494-
495479
#### No Native Module Loading
496480
497481
Native modules are not currently supported with ES module imports.
@@ -529,35 +513,19 @@ separate cache.
529513
530514
> Stability: 1 - Experimental
531515
532-
Currently importing JSON modules are only supported in the `commonjs` mode
533-
and are loaded using the CJS loader. [WHATWG JSON modules specification][] are
534-
still being standardized, and are experimentally supported by including the
535-
additional flag `--experimental-json-modules` when running Node.js.
536-
537-
When the `--experimental-json-modules` flag is included, both the
538-
`commonjs` and `module` mode use the new experimental JSON
539-
loader. The imported JSON only exposes a `default`. There is no
540-
support for named exports. A cache entry is created in the CommonJS
541-
cache to avoid duplication. The same object is returned in
542-
CommonJS if the JSON module has already been imported from the
543-
same path.
544-
545-
Assuming an `index.mjs` with
516+
JSON files can be referenced by `import`:
546517
547518
```js
548519
import packageConfig from './package.json' assert { type: 'json' };
549520
```
550521
551-
The `--experimental-json-modules` flag is needed for the module
552-
to work.
553-
554-
```bash
555-
node index.mjs # fails
556-
node --experimental-json-modules index.mjs # works
557-
```
558-
559522
The `assert { type: 'json' }` syntax is mandatory; see [Import Assertions][].
560523
524+
The imported JSON only exposes a `default` export. There is no support for named
525+
exports. A cache entry is created in the CommonJS cache to avoid duplication.
526+
The same object is returned in CommonJS if the JSON module has already been
527+
imported from the same path.
528+
561529
<i id="esm_experimental_wasm_modules"></i>
562530
563531
## Wasm modules
@@ -1451,7 +1419,6 @@ success!
14511419
[Node.js Module Resolution Algorithm]: #resolver-algorithm-specification
14521420
[Terminology]: #terminology
14531421
[URL]: https://url.spec.whatwg.org/
1454-
[WHATWG JSON modules specification]: https://html.spec.whatwg.org/#creating-a-json-module-script
14551422
[`"exports"`]: packages.md#exports
14561423
[`"type"`]: packages.md#type
14571424
[`--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
@@ -315,19 +315,13 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
315315
kAllowedInEnvironment);
316316
AddOption("--experimental-abortcontroller", "",
317317
NoOp{}, kAllowedInEnvironment);
318-
AddOption("--experimental-json-modules",
319-
"experimental JSON interop support for the ES Module loader",
320-
&EnvironmentOptions::experimental_json_modules,
321-
kAllowedInEnvironment);
318+
AddOption("--experimental-json-modules", "", NoOp{}, kAllowedInEnvironment);
322319
AddOption("--experimental-loader",
323320
"use the specified module as a custom loader",
324321
&EnvironmentOptions::userland_loader,
325322
kAllowedInEnvironment);
326323
AddAlias("--loader", "--experimental-loader");
327-
AddOption("--experimental-modules",
328-
"",
329-
&EnvironmentOptions::experimental_modules,
330-
kAllowedInEnvironment);
324+
AddOption("--experimental-modules", "", NoOp{}, kAllowedInEnvironment);
331325
AddOption("--experimental-wasm-modules",
332326
"experimental ES Module support for webassembly modules",
333327
&EnvironmentOptions::experimental_wasm_modules,

src/node_options.h

-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ class EnvironmentOptions : public Options {
107107
std::vector<std::string> conditions;
108108
std::string dns_result_order;
109109
bool enable_source_maps = false;
110-
bool experimental_json_modules = false;
111-
bool experimental_modules = false;
112110
std::string experimental_specifier_resolution;
113111
bool experimental_wasm_modules = false;
114112
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)