diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index dd96321093ab..d96c9fa90fd0 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -879,7 +879,10 @@ function defaultResolve(specifier, context = kEmptyObject) { // input, to avoid user confusion over how expansive the effect of the // flag should be (i.e. entry point only, package scope surrounding the // entry point, etc.). - if (getOptionValue('--input-type')) { throw new ERR_INPUT_TYPE_NOT_ALLOWED(); } + // Workers inherit --input-type for eval code, so only the main thread's + // entry point is checked. + const { isMainThread } = internalBinding('worker'); + if (isMainThread && getOptionValue('--input-type')) { throw new ERR_INPUT_TYPE_NOT_ALLOWED(); } } conditions = getConditionsSet(conditions); diff --git a/test/parallel/test-worker-cli-options.js b/test/parallel/test-worker-cli-options.js index 3e6ab46db6ea..85039ac8283b 100644 --- a/test/parallel/test-worker-cli-options.js +++ b/test/parallel/test-worker-cli-options.js @@ -1,6 +1,7 @@ // Flags: --expose-internals --expose-gc 'use strict'; const common = require('../common'); +const fixtures = require('../common/fixtures'); const { Worker } = require('worker_threads'); const assert = require('assert'); @@ -37,3 +38,13 @@ new Worker('export {}', { eval: true, execArgv: ['--input-type=commonjs'] }) new Worker('export {}', { eval: true, execArgv: ['--experimental-detect-module'] }); new Worker('export {}', { eval: true, execArgv: ['--no-experimental-detect-module'] }) .once('error', common.expectsError({ name: 'SyntaxError' })); + +// Test file entry points with --input-type +for (const [file, execArgv] of [ + ['es-modules/mjs-file.mjs', ['--input-type=module']], + ['es-modules/mjs-file.mjs', ['--input-type=commonjs']], + ['es-modules/cjs-file.cjs', ['--input-type=commonjs', '--import', 'data:text/javascript,']], +]) { + new Worker(fixtures.path(file), { execArgv }) + .on('exit', common.mustCall((code) => assert.strictEqual(code, 0))); +}