File tree Expand file tree Collapse file tree 8 files changed +44
-8
lines changed Open diff view settings
Expand file tree Collapse file tree 8 files changed +44
-8
lines changed Open diff view settings
Original file line number Diff line number Diff line change @@ -165,6 +165,13 @@ added: v12.7.0
165165
166166Enable experimental resolution using the ` exports ` field in ` package.json ` .
167167
168+ ### ` --experimental-json-modules `
169+ <!-- YAML
170+ added: v12.9.0
171+ -->
172+
173+ Enable experimental JSON support for the ES Module loader.
174+
168175### ` --experimental-modules `
169176<!-- YAML
170177added: v8.5.0
@@ -1019,6 +1026,7 @@ Node.js options that are allowed are:
10191026* ` --enable-source-maps `
10201027* ` --es-module-specifier-resolution `
10211028* ` --experimental-exports `
1029+ * ` --experimental-json-modules `
10221030* ` --experimental-loader `
10231031* ` --experimental-modules `
10241032* ` --experimental-policy `
Original file line number Diff line number Diff line change @@ -587,11 +587,16 @@ syncBuiltinESMExports();
587587fs .readFileSync === readFileSync;
588588` ` `
589589
590- ## JSON Modules
590+ ## Experimental JSON Modules
591591
592- JSON modules follow the [WHATWG JSON modules specification][].
592+ Currently importing JSON modules are only supported in the ` commonjs` mode
593+ and are loaded using the CJS loader. [WHATWG JSON modules specification][] are
594+ still being standardized, and are experimentally supported by including the
595+ additional flag ` -- experimental- json- modules` when running Node.js.
593596
594- The imported JSON only exposes a ` default` . There is no
597+ When the ` -- experimental- json- modules` flag is included both the
598+ ` commonjs` and ` module ` mode will use the new experimental JSON
599+ loader. The imported JSON only exposes a ` default` , there is no
595600support for named exports. A cache entry is created in the CommonJS
596601cache, to avoid duplication. The same object will be returned in
597602CommonJS if the JSON module has already been imported from the
@@ -604,6 +609,14 @@ Assuming an `index.mjs` with
604609import packageConfig from ' ./package.json' ;
605610` ` `
606611
612+ The ` -- experimental- json- modules` flag is needed for the module
613+ to work.
614+
615+ ` ` ` bash
616+ node -- experimental- modules index .mjs # fails
617+ node -- experimental- modules -- experimental- json- modules index .mjs # works
618+ ` ` `
619+
607620## Experimental Wasm Modules
608621
609622Importing Web Assembly modules is supported under the
Original file line number Diff line number Diff line change @@ -108,6 +108,9 @@ Requires Node.js to be built with
108108.It Fl -es-module-specifier-resolution
109109Select extension resolution algorithm for ES Modules; either 'explicit' (default) or 'node'
110110.
111+ .It Fl -experimental-json-modules
112+ Enable experimental JSON interop support for the ES Module loader.
113+ .
111114.It Fl -experimental-modules
112115Enable experimental ES module support and caching modules.
113116.
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ const { getOptionValue } = require('internal/options');
88
99const preserveSymlinks = getOptionValue ( '--preserve-symlinks' ) ;
1010const preserveSymlinksMain = getOptionValue ( '--preserve-symlinks-main' ) ;
11+ const experimentalJsonModules = getOptionValue ( '--experimental-json-modules' ) ;
1112const typeFlag = getOptionValue ( '--input-type' ) ;
1213const experimentalWasmModules = getOptionValue ( '--experimental-wasm-modules' ) ;
1314const { resolve : moduleWrapResolve ,
@@ -28,22 +29,24 @@ const extensionFormatMap = {
2829 '__proto__' : null ,
2930 '.cjs' : 'commonjs' ,
3031 '.js' : 'module' ,
31- '.json' : 'json' ,
3232 '.mjs' : 'module'
3333} ;
3434
3535const legacyExtensionFormatMap = {
3636 '__proto__' : null ,
3737 '.cjs' : 'commonjs' ,
3838 '.js' : 'commonjs' ,
39- '.json' : 'json ' ,
39+ '.json' : 'commonjs ' ,
4040 '.mjs' : 'module' ,
4141 '.node' : 'commonjs'
4242} ;
4343
4444if ( experimentalWasmModules )
4545 extensionFormatMap [ '.wasm' ] = legacyExtensionFormatMap [ '.wasm' ] = 'wasm' ;
4646
47+ if ( experimentalJsonModules )
48+ extensionFormatMap [ '.json' ] = legacyExtensionFormatMap [ '.json' ] = 'json' ;
49+
4750function resolve ( specifier , parentURL ) {
4851 try {
4952 const parsed = new URL ( specifier ) ;
Original file line number Diff line number Diff line change @@ -135,6 +135,11 @@ void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors) {
135135 }
136136 }
137137
138+ if (experimental_json_modules && !experimental_modules) {
139+ errors->push_back (" --experimental-json-modules requires "
140+ " --experimental-modules be enabled" );
141+ }
142+
138143 if (experimental_wasm_modules && !experimental_modules) {
139144 errors->push_back (" --experimental-wasm-modules requires "
140145 " --experimental-modules be enabled" );
@@ -320,6 +325,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
320325 " experimental support for exports in package.json" ,
321326 &EnvironmentOptions::experimental_exports,
322327 kAllowedInEnvironment );
328+ AddOption (" --experimental-json-modules" ,
329+ " experimental JSON interop support for the ES Module loader" ,
330+ &EnvironmentOptions::experimental_json_modules,
331+ kAllowedInEnvironment );
323332 AddOption (" --experimental-loader" ,
324333 " (with --experimental-modules) use the specified file as a "
325334 " custom loader" ,
Original file line number Diff line number Diff line change @@ -102,6 +102,7 @@ class EnvironmentOptions : public Options {
102102 bool abort_on_uncaught_exception = false ;
103103 bool enable_source_maps = false ;
104104 bool experimental_exports = false ;
105+ bool experimental_json_modules = false ;
105106 bool experimental_modules = false ;
106107 std::string es_module_specifier_resolution;
107108 bool experimental_wasm_modules = false ;
Original file line number Diff line number Diff line change 1- // Flags: --experimental-modules
1+ // Flags: --experimental-modules --experimental-json-modules
22import '../common/index.mjs' ;
33
44import { strictEqual , deepStrictEqual } from 'assert' ;
Original file line number Diff line number Diff line change 1- // Flags: --experimental-modules
2-
1+ // Flags: --experimental-modules --experimental-json-modules
32import '../common/index.mjs' ;
43import { strictEqual } from 'assert' ;
54
You can’t perform that action at this time.
0 commit comments