|
5 | 5 | <!-- YAML |
6 | 6 | added: v8.5.0 |
7 | 7 | 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. |
8 | 11 | - version: |
9 | 12 | - v15.3.0 |
10 | 13 | pr-url: https://github.com/nodejs/node/pull/35781 |
@@ -422,21 +425,6 @@ These CommonJS variables are not available in ES modules. |
422 | 425 | `__filename` and `__dirname` use cases can be replicated via |
423 | 426 | [`import.meta.url`][]. |
424 | 427 |
|
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 | | -
|
440 | 428 | #### No Native Module Loading |
441 | 429 |
|
442 | 430 | Native modules are not currently supported with ES module imports. |
@@ -471,35 +459,35 @@ separate cache. |
471 | 459 | <i id="esm_experimental_json_modules"></i> |
472 | 460 |
|
473 | 461 | ## 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 | +--> |
474 | 469 |
|
475 | 470 | > Stability: 1 - Experimental |
476 | 471 |
|
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 |
485 | 473 | support for named exports. A cache entry is created in the CommonJS |
486 | 474 | cache to avoid duplication. The same object is returned in |
487 | 475 | CommonJS if the JSON module has already been imported from the |
488 | 476 | same path. |
489 | 477 |
|
490 | | -Assuming an `index.mjs` with |
491 | | -
|
492 | | -<!-- eslint-skip --> |
493 | | -```js |
| 478 | +```js esm |
494 | 479 | import packageConfig from './package.json'; |
| 480 | +const { version, name } = packageConfig; |
| 481 | + |
| 482 | +console.log(version); |
495 | 483 | ``` |
496 | 484 |
|
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'); |
499 | 488 |
|
500 | | -```bash |
501 | | -node index.mjs # fails |
502 | | -node --experimental-json-modules index.mjs # works |
| 489 | + console.log(version); |
| 490 | +})().catch(console.error); |
503 | 491 | ``` |
504 | 492 |
|
505 | 493 | <i id="esm_experimental_wasm_modules"></i> |
|
0 commit comments