diff --git a/doc/node.1 b/doc/node.1 index 0d64b62dca461d..07b5d09e1a4c7c 100644 --- a/doc/node.1 +++ b/doc/node.1 @@ -179,7 +179,7 @@ Use this flag to enable ShadowRealm support. Enable code coverage in the test runner. . .It Fl -experimental-type Ns = Ns Ar type -Interpret as either ES modules or CommonJS modules input via --eval, --print or STDIN; +Interpret as either ES modules or CommonJS modules input via --eval or STDIN, when --input-type is not specified; .js or extensionless files with no sibling or parent package.json; .js or extensionless files whose nearest parent package.json lacks a "type" field, unless under node_modules. . diff --git a/lib/internal/main/check_syntax.js b/lib/internal/main/check_syntax.js index dc9de45420f692..636ee049b9e922 100644 --- a/lib/internal/main/check_syntax.js +++ b/lib/internal/main/check_syntax.js @@ -60,7 +60,8 @@ function loadESMIfNeeded(cb) { async function checkSyntax(source, filename) { let isModule = true; if (filename === '[stdin]' || filename === '[eval]') { - isModule = getOptionValue('--input-type') === 'module' || getOptionValue('--experimental-type') === 'module'; + isModule = getOptionValue('--input-type') === 'module' || + (getOptionValue('--experimental-type') === 'module' && getOptionValue('--input-type') !== 'commonjs'); } else { const { defaultResolve } = require('internal/modules/esm/resolve'); const { defaultGetFormat } = require('internal/modules/esm/get_format'); diff --git a/lib/internal/main/eval_stdin.js b/lib/internal/main/eval_stdin.js index d457f4f3c1da8d..51f14be0e1afef 100644 --- a/lib/internal/main/eval_stdin.js +++ b/lib/internal/main/eval_stdin.js @@ -25,12 +25,14 @@ readStdin((code) => { const print = getOptionValue('--print'); const loadESM = getOptionValue('--import').length > 0; - if (getOptionValue('--input-type') === 'module' || getOptionValue('--experimental-type') === 'module') + if (getOptionValue('--input-type') === 'module' || + (getOptionValue('--experimental-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) { evalModule(code, print); - else + } else { evalScript('[stdin]', code, getOptionValue('--inspect-brk'), print, loadESM); + } }); diff --git a/lib/internal/main/eval_string.js b/lib/internal/main/eval_string.js index f2b89ead38db3f..90746047df6897 100644 --- a/lib/internal/main/eval_string.js +++ b/lib/internal/main/eval_string.js @@ -25,9 +25,10 @@ markBootstrapComplete(); const source = getOptionValue('--eval'); const print = getOptionValue('--print'); const loadESM = getOptionValue('--import').length > 0 || getOptionValue('--experimental-loader').length > 0; -if (getOptionValue('--input-type') === 'module' || getOptionValue('--experimental-type') === 'module') +if (getOptionValue('--input-type') === 'module' || + (getOptionValue('--experimental-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) { evalModule(source, print); -else { +} else { // For backward compatibility, we want the identifier crypto to be the // `node:crypto` module rather than WebCrypto. const isUsingCryptoIdentifier = diff --git a/src/node_options.cc b/src/node_options.cc index 77b705516af477..989d8727b19424 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -127,11 +127,6 @@ void EnvironmentOptions::CheckOptions(std::vector* errors, } } - if (!input_type.empty() && !type.empty()) { - errors->push_back("--input-type and --experimental-type cannot be used " - "together"); - } - if (syntax_check_only && has_eval_string) { errors->push_back("either --check or --eval can be used, not both"); } diff --git a/test/es-module/test-esm-type-flag-string-input.mjs b/test/es-module/test-esm-type-flag-string-input.mjs index 5895bdc549e259..7a7cb199bea74d 100644 --- a/test/es-module/test-esm-type-flag-string-input.mjs +++ b/test/es-module/test-esm-type-flag-string-input.mjs @@ -27,4 +27,18 @@ describe('the type flag should change the interpretation of string input', { con match((await child.stdout.toArray()).toString(), /^function\r?\n$/); }); + + it('should be overridden by --input-type', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ + '--experimental-type=module', + '--input-type=commonjs', + '--eval', + 'console.log(require("process").version)', + ]); + + strictEqual(stderr, ''); + strictEqual(stdout, `${process.version}\n`); + strictEqual(code, 0); + strictEqual(signal, null); + }); });