Skip to content

Commit 2890a79

Browse files
committed
module: Unflag JSON modules
JSON modules TC39 proposal has reached stage 3. Fixes: #37141
1 parent bcb1964 commit 2890a79

File tree

5 files changed

+34
-63
lines changed

5 files changed

+34
-63
lines changed

doc/api/cli.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,6 @@ added:
222222

223223
Enable experimental `import.meta.resolve()` support.
224224

225-
### `--experimental-json-modules`
226-
<!-- YAML
227-
added: v12.9.0
228-
-->
229-
230-
Enable experimental JSON support for the ES Module loader.
231-
232225
### `--experimental-loader=module`
233226
<!-- YAML
234227
added: v9.0.0

doc/api/esm.md

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<!-- YAML
66
added: v8.5.0
77
changes:
8+
- version: REPLACEME
9+
pr-url: https://githubcom/nodejs/node/pull/00000
10+
description: Loading JSON modules no longer requires a command-line flag.
811
- version:
912
- v15.3.0
1013
pr-url: https://github.com/nodejs/node/pull/35781
@@ -422,21 +425,6 @@ These CommonJS variables are not available in ES modules.
422425
`__filename` and `__dirname` use cases can be replicated via
423426
[`import.meta.url`][].
424427
425-
#### No JSON Module Loading
426-
427-
JSON imports are still experimental and only supported via the
428-
`--experimental-json-modules` flag.
429-
430-
Local JSON files can be loaded relative to `import.meta.url` with `fs` directly:
431-
432-
<!-- eslint-skip -->
433-
```js
434-
import { readFile } from 'fs/promises';
435-
const json = JSON.parse(await readFile(new URL('./dat.json', import.meta.url)));
436-
```
437-
438-
Alterantively `module.createRequire()` can be used.
439-
440428
#### No Native Module Loading
441429
442430
Native modules are not currently supported with ES module imports.
@@ -471,35 +459,35 @@ separate cache.
471459
<i id="esm_experimental_json_modules"></i>
472460
473461
## JSON modules
462+
<!--YAML
463+
added: v12.9.0
464+
changes:
465+
- version: REPLACEME
466+
pr-url: https://githubcom/nodejs/node/pull/00000
467+
description: Loading JSON modules no longer requires a command-line flag.
468+
-->
474469
475470
> Stability: 1 - Experimental
476471
477-
Currently importing JSON modules are only supported in the `commonjs` mode
478-
and are loaded using the CJS loader. [WHATWG JSON modules specification][] are
479-
still being standardized, and are experimentally supported by including the
480-
additional flag `--experimental-json-modules` when running Node.js.
481-
482-
When the `--experimental-json-modules` flag is included, both the
483-
`commonjs` and `module` mode use the new experimental JSON
484-
loader. The imported JSON only exposes a `default`. There is no
472+
The imported JSON only exposes a `default` export. There is no
485473
support for named exports. A cache entry is created in the CommonJS
486474
cache to avoid duplication. The same object is returned in
487475
CommonJS if the JSON module has already been imported from the
488476
same path.
489477
490-
Assuming an `index.mjs` with
491-
492-
<!-- eslint-skip -->
493-
```js
478+
```js esm
494479
import packageConfig from './package.json';
480+
const { version, name } = packageConfig;
481+
482+
console.log(version);
495483
```
496484
497-
The `--experimental-json-modules` flag is needed for the module
498-
to work.
485+
```js cjs
486+
(async () => {
487+
const { default: { version, name } } = await import('./package.json');
499488

500-
```bash
501-
node index.mjs # fails
502-
node --experimental-json-modules index.mjs # works
489+
console.log(version);
490+
})().catch(console.error);
503491
```
504492
505493
<i id="esm_experimental_wasm_modules"></i>

lib/internal/modules/esm/get_format.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const {
66
const { extname } = require('path');
77
const { getOptionValue } = require('internal/options');
88

9-
const experimentalJsonModules = getOptionValue('--experimental-json-modules');
109
const experimentalSpecifierResolution =
1110
getOptionValue('--experimental-specifier-resolution');
1211
const experimentalWasmModules = getOptionValue('--experimental-wasm-modules');
@@ -18,7 +17,8 @@ const extensionFormatMap = {
1817
'__proto__': null,
1918
'.cjs': 'commonjs',
2019
'.js': 'module',
21-
'.mjs': 'module'
20+
'.mjs': 'module',
21+
'.json': 'json',
2222
};
2323

2424
const legacyExtensionFormatMap = {
@@ -33,9 +33,6 @@ const legacyExtensionFormatMap = {
3333
if (experimentalWasmModules)
3434
extensionFormatMap['.wasm'] = legacyExtensionFormatMap['.wasm'] = 'wasm';
3535

36-
if (experimentalJsonModules)
37-
extensionFormatMap['.json'] = legacyExtensionFormatMap['.json'] = 'json';
38-
3936
function defaultGetFormat(url, context, defaultGetFormatUnused) {
4037
if (StringPrototypeStartsWith(url, 'node:')) {
4138
return { format: 'builtin' };
@@ -49,7 +46,7 @@ function defaultGetFormat(url, context, defaultGetFormatUnused) {
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
return { format };

src/node_options.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,7 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
303303
kAllowedInEnvironment);
304304
AddOption("--experimental-abortcontroller", "",
305305
NoOp{}, kAllowedInEnvironment);
306-
AddOption("--experimental-json-modules",
307-
"experimental JSON interop support for the ES Module loader",
306+
AddOption("--experimental-json-modules", "",
308307
&EnvironmentOptions::experimental_json_modules,
309308
kAllowedInEnvironment);
310309
AddOption("--experimental-loader",

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

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
'use strict';
22

33
const common = require('../common');
4-
const { spawn } = require('child_process');
5-
const assert = require('assert');
6-
7-
const entry = require.resolve('./test-esm-json.mjs');
4+
const fixtures = require('../common/fixtures');
85

9-
// Verify non-js extensions fail for ESM
10-
const child = spawn(process.execPath, [entry]);
11-
12-
let stderr = '';
13-
child.stderr.setEncoding('utf8');
14-
child.stderr.on('data', (data) => {
15-
stderr += data;
16-
});
17-
child.on('close', common.mustCall((code, signal) => {
18-
assert.strictEqual(code, 1);
19-
assert.strictEqual(signal, null);
20-
assert.ok(stderr.indexOf('ERR_UNKNOWN_FILE_EXTENSION') !== -1);
6+
const assert = require('assert');
7+
const { pathToFileURL } = require('url');
8+
const { Worker } = require('worker_threads');
9+
10+
new Worker(new URL(
11+
'data:text/javascript,' +
12+
`import ${JSON.stringify(pathToFileURL(fixtures.path('altdocs.md')))};`
13+
)).on('error', common.mustCall((err) => {
14+
assert.strictEqual(err.code, 'ERR_UNKNOWN_FILE_EXTENSION');
2115
}));

0 commit comments

Comments
 (0)