diff --git a/doc/node.1 b/doc/node.1 index 8b5560b1f1..2b742c4e3a 100644 --- a/doc/node.1 +++ b/doc/node.1 @@ -119,6 +119,9 @@ Enable FIPS-compliant crypto at startup. Requires Node.js to be built with .Sy ./configure --openssl-fips . . +.It Fl -entry-type Ns = Ns Ar type +Set the top-level module resolution type. +. .It Fl -experimental-modules Enable experimental ES module support and caching modules. . @@ -280,9 +283,6 @@ Print stack traces for process warnings (including deprecations). .It Fl -track-heap-objects Track heap object allocations for heap snapshots. . -.It Fl -type Ns = Ns Ar type -Set the top-level module resolution type. -. .It Fl -use-bundled-ca , Fl -use-openssl-ca Use bundled Mozilla CA store as supplied by current Node.js version or use OpenSSL's default CA store. The default store is selectable at build-time. diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 71ee52b3d8..21e0db9fce 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -873,8 +873,6 @@ E('ERR_INVALID_PROTOCOL', TypeError); E('ERR_INVALID_REPL_EVAL_CONFIG', 'Cannot specify both "breakEvalOnSigint" and "eval" for REPL', TypeError); -E('ERR_INVALID_REPL_TYPE', - 'Cannot specify --type for REPL', TypeError); E('ERR_INVALID_RETURN_PROPERTY', (input, name, prop, value) => { return `Expected a valid ${input} to be returned for the "${prop}" from the` + ` "${name}" function but got ${value}.`; @@ -905,9 +903,6 @@ E('ERR_INVALID_SYNC_FORK_INPUT', TypeError); E('ERR_INVALID_THIS', 'Value of "this" must be of type %s', TypeError); E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple', TypeError); -E('ERR_INVALID_TYPE_FLAG', - 'Type flag must be one of "module", "commonjs". Received --type=%s', - TypeError); E('ERR_INVALID_URI', 'URI malformed', URIError); E('ERR_INVALID_URL', function(input) { this.input = input; @@ -1055,12 +1050,12 @@ E('ERR_TRANSFORM_WITH_LENGTH_0', E('ERR_TTY_INIT_FAILED', 'TTY initialization failed', SystemError); E('ERR_TYPE_MISMATCH', (filename, ext, typeFlag, conflict) => { const typeString = - typeFlag === 'module' ? '--type=module' : '--type=commonjs'; - // --type mismatches file extension + typeFlag === 'module' ? '--entry-type=module' : '--entry-type=commonjs'; + // --entry-type mismatches file extension if (conflict === 'extension') return `Extension ${ext} is not supported for ` + `${typeString} loading ${filename}`; - // --type mismatches package.json "type" + // --entry-type mismatches package.json "type" else if (conflict === 'scope') return `Cannot use ${typeString} because nearest parent package.json ` + ((typeFlag === 'module') ? diff --git a/lib/internal/main/check_syntax.js b/lib/internal/main/check_syntax.js index 2795f5766e..8c73d522ed 100644 --- a/lib/internal/main/check_syntax.js +++ b/lib/internal/main/check_syntax.js @@ -56,12 +56,12 @@ function checkSyntax(source, filename) { // Remove Shebang. source = stripShebang(source); - const experimentalModules = - require('internal/options').getOptionValue('--experimental-modules'); + const { getOptionValue } = require('internal/options'); + const experimentalModules = getOptionValue('--experimental-modules'); if (experimentalModules) { let isModule = false; if (filename === '[stdin]' || filename === '[eval]') { - isModule = require('internal/process/esm_loader').typeFlag === 'module'; + isModule = getOptionValue('--entry-type') === 'module'; } else { const resolve = require('internal/modules/esm/default_resolve'); const { format } = resolve(pathToFileURL(filename).toString()); diff --git a/lib/internal/main/eval_stdin.js b/lib/internal/main/eval_stdin.js index 869a3675b6..4face9e61e 100644 --- a/lib/internal/main/eval_stdin.js +++ b/lib/internal/main/eval_stdin.js @@ -17,7 +17,7 @@ markBootstrapComplete(); readStdin((code) => { process._eval = code; - if (require('internal/process/esm_loader').typeFlag === 'module') + if (require('internal/options').getOptionValue('--entry-type') === 'module') evalModule(process._eval); else evalScript('[stdin]', process._eval, process._breakFirstLine); diff --git a/lib/internal/main/eval_string.js b/lib/internal/main/eval_string.js index 9328a114aa..b032281925 100644 --- a/lib/internal/main/eval_string.js +++ b/lib/internal/main/eval_string.js @@ -9,11 +9,12 @@ const { const { evalModule, evalScript } = require('internal/process/execution'); const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers'); -const source = require('internal/options').getOptionValue('--eval'); +const { getOptionValue } = require('internal/options'); +const source = getOptionValue('--eval'); prepareMainThreadExecution(); addBuiltinLibsToObject(global); markBootstrapComplete(); -if (require('internal/process/esm_loader').typeFlag === 'module') +if (getOptionValue('--entry-type') === 'module') evalModule(source); else evalScript('[eval]', source, process._breakFirstLine); diff --git a/lib/internal/main/repl.js b/lib/internal/main/repl.js index 7656af46a3..c2bf54f8bb 100644 --- a/lib/internal/main/repl.js +++ b/lib/internal/main/repl.js @@ -11,13 +11,12 @@ const { evalScript } = require('internal/process/execution'); -const { ERR_INVALID_REPL_TYPE } = require('internal/errors').codes; - prepareMainThreadExecution(); -// --type flag not supported in REPL -if (require('internal/process/esm_loader').typeFlag) { - throw ERR_INVALID_REPL_TYPE(); +// --entry-type flag not supported in REPL +if (require('internal/options').getOptionValue('--entry-type')) { + console.error('Cannot specify --entry-type for REPL'); + process.exit(1); } const cliRepl = require('internal/repl'); diff --git a/lib/internal/modules/esm/default_resolve.js b/lib/internal/modules/esm/default_resolve.js index 3abf4e04a9..0615875071 100644 --- a/lib/internal/modules/esm/default_resolve.js +++ b/lib/internal/modules/esm/default_resolve.js @@ -9,11 +9,11 @@ const { getOptionValue } = require('internal/options'); const preserveSymlinks = getOptionValue('--preserve-symlinks'); const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); const experimentalJsonModules = getOptionValue('--experimental-json-modules'); +const typeFlag = getOptionValue('--entry-type'); const { resolve: moduleWrapResolve, getPackageType } = internalBinding('module_wrap'); const { pathToFileURL, fileURLToPath } = require('internal/url'); -const asyncESM = require('internal/process/esm_loader'); const { ERR_TYPE_MISMATCH, ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes; @@ -80,26 +80,26 @@ function resolve(specifier, parentURL) { type !== TYPE_MODULE ? legacyExtensionFormatMap : extensionFormatMap; let format = extMap[ext]; - if (isMain && asyncESM.typeFlag) { - // Conflict between explicit extension (.mjs, .cjs) and --type - if (ext === '.cjs' && asyncESM.typeFlag === 'module' || - ext === '.mjs' && asyncESM.typeFlag === 'commonjs') { + if (isMain && typeFlag) { + // Conflict between explicit extension (.mjs, .cjs) and --entry-type + if (ext === '.cjs' && typeFlag === 'module' || + ext === '.mjs' && typeFlag === 'commonjs') { throw new ERR_TYPE_MISMATCH( - fileURLToPath(url), ext, asyncESM.typeFlag, 'extension'); + fileURLToPath(url), ext, typeFlag, 'extension'); } - // Conflict between package scope type and --type + // Conflict between package scope type and --entry-type if (ext === '.js') { - if (type === TYPE_MODULE && asyncESM.typeFlag === 'commonjs' || - type === TYPE_COMMONJS && asyncESM.typeFlag === 'module') { + if (type === TYPE_MODULE && typeFlag === 'commonjs' || + type === TYPE_COMMONJS && typeFlag === 'module') { throw new ERR_TYPE_MISMATCH( - fileURLToPath(url), ext, asyncESM.typeFlag, 'scope'); + fileURLToPath(url), ext, typeFlag, 'scope'); } } } if (!format) { - if (isMain && asyncESM.typeFlag) - format = asyncESM.typeFlag; + if (isMain && typeFlag) + format = typeFlag; else if (isMain) format = type === TYPE_MODULE ? 'module' : 'commonjs'; else diff --git a/lib/internal/process/esm_loader.js b/lib/internal/process/esm_loader.js index ed387a1554..6225ea81ab 100644 --- a/lib/internal/process/esm_loader.js +++ b/lib/internal/process/esm_loader.js @@ -4,15 +4,9 @@ const { callbackMap, } = internalBinding('module_wrap'); const { - ERR_INVALID_TYPE_FLAG, ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING, } = require('internal/errors').codes; -const type = require('internal/options').getOptionValue('--type'); -if (type && type !== 'commonjs' && type !== 'module') - throw new ERR_INVALID_TYPE_FLAG(type); -exports.typeFlag = type; - const { Loader } = require('internal/modules/esm/loader'); const { pathToFileURL } = require('internal/url'); const { diff --git a/src/node_options.cc b/src/node_options.cc index fc4eb985e2..bd20b6385d 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -107,8 +107,14 @@ void EnvironmentOptions::CheckOptions(std::vector* errors) { errors->push_back("--loader requires --experimental-modules be enabled"); } - if (!module_type.empty() && !experimental_modules) { - errors->push_back("--type requires --experimental-modules be enabled"); + if (!module_type.empty()) { + if (!experimental_modules) { + errors->push_back("--entry-type requires " + "--experimental-modules to be enabled"); + } + if (module_type != "commonjs" && module_type != "module") { + errors->push_back("--entry-type must \"module\" or \"commonjs\""); + } } if (experimental_json_modules && !experimental_modules) { @@ -332,8 +338,8 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { "show stack traces on process warnings", &EnvironmentOptions::trace_warnings, kAllowedInEnvironment); - AddOption("--type", - "top-level module type name", + AddOption("--entry-type", + "set module type name of the entry point", &EnvironmentOptions::module_type, kAllowedInEnvironment); diff --git a/test/es-module/test-esm-no-extension.js b/test/es-module/test-esm-no-extension.js index 36848eed0e..3e9ffb2bbc 100644 --- a/test/es-module/test-esm-no-extension.js +++ b/test/es-module/test-esm-no-extension.js @@ -12,7 +12,7 @@ const entry = fixtures.path('/es-modules/noext-esm'); const child = spawn(process.execPath, [ '--experimental-modules', - '--type=module', + '--entry-type=module', entry ]); diff --git a/test/es-module/test-esm-type-flag-errors.js b/test/es-module/test-esm-type-flag-errors.js index 4b75eabc0c..bbdd140482 100644 --- a/test/es-module/test-esm-type-flag-errors.js +++ b/test/es-module/test-esm-type-flag-errors.js @@ -19,16 +19,16 @@ expect('', packageTypeModuleMain, 'package-type-module'); expect('', packageTypeCommonJsMain, 'package-type-commonjs'); expect('', packageWithoutTypeMain, 'package-without-type'); -// Check that running with --type and no package.json "type" works -expect('--type=commonjs', packageWithoutTypeMain, 'package-without-type'); -expect('--type=module', packageWithoutTypeMain, 'package-without-type'); +// Check that running with --entry-type and no package.json "type" works +expect('--entry-type=commonjs', packageWithoutTypeMain, 'package-without-type'); +expect('--entry-type=module', packageWithoutTypeMain, 'package-without-type'); -// Check that running with conflicting --type flags throws errors -expect('--type=commonjs', mjsFile, 'ERR_TYPE_MISMATCH', true); -expect('--type=module', cjsFile, 'ERR_TYPE_MISMATCH', true); -expect('--type=commonjs', packageTypeModuleMain, +// Check that running with conflicting --entry-type flags throws errors +expect('--entry-type=commonjs', mjsFile, 'ERR_TYPE_MISMATCH', true); +expect('--entry-type=module', cjsFile, 'ERR_TYPE_MISMATCH', true); +expect('--entry-type=commonjs', packageTypeModuleMain, 'ERR_TYPE_MISMATCH', true); -expect('--type=module', packageTypeCommonJsMain, +expect('--entry-type=module', packageTypeCommonJsMain, 'ERR_TYPE_MISMATCH', true); function expect(opt = '', inputFile, want, wantsError = false) { diff --git a/test/es-module/test-esm-type-flag.mjs b/test/es-module/test-esm-type-flag.mjs index cf91580490..2f5d0b626a 100644 --- a/test/es-module/test-esm-type-flag.mjs +++ b/test/es-module/test-esm-type-flag.mjs @@ -1,4 +1,4 @@ -// Flags: --experimental-modules --type=module +// Flags: --experimental-modules --entry-type=module /* eslint-disable node-core/required-modules */ import cjs from '../fixtures/baz.js'; import '../common/index.mjs'; diff --git a/test/parallel/test-cli-syntax-piped-bad.js b/test/parallel/test-cli-syntax-piped-bad.js index ba93e13038..f342a7834f 100644 --- a/test/parallel/test-cli-syntax-piped-bad.js +++ b/test/parallel/test-cli-syntax-piped-bad.js @@ -34,12 +34,12 @@ syntaxArgs.forEach(function(arg) { assert.strictEqual(c.status, 1); }); -// Check --type=module +// Check --entry-type=module syntaxArgs.forEach(function(arg) { const stdin = 'export var p = 5; var foo bar;'; const c = spawnSync( node, - ['--experimental-modules', '--type=module', '--no-warnings', arg], + ['--experimental-modules', '--entry-type=module', '--no-warnings', arg], { encoding: 'utf8', input: stdin } ); diff --git a/test/parallel/test-cli-syntax-piped-good.js b/test/parallel/test-cli-syntax-piped-good.js index 8ee11226d9..b2b02172cb 100644 --- a/test/parallel/test-cli-syntax-piped-good.js +++ b/test/parallel/test-cli-syntax-piped-good.js @@ -25,12 +25,12 @@ syntaxArgs.forEach(function(arg) { assert.strictEqual(c.status, 0); }); -// Check --type=module +// Check --entry-type=module syntaxArgs.forEach(function(arg) { const stdin = 'export var p = 5; throw new Error("should not get run");'; const c = spawnSync( node, - ['--experimental-modules', '--no-warnings', '--type=module', arg], + ['--experimental-modules', '--no-warnings', '--entry-type=module', arg], { encoding: 'utf8', input: stdin } );