diff --git a/doc/api/errors.md b/doc/api/errors.md index 06ca45c16c0908..49dbcb224c7d7f 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1231,12 +1231,6 @@ provided. Encoding provided to `TextDecoder()` API was not one of the [WHATWG Supported Encodings][]. - - -### `ERR_EVAL_ESM_CANNOT_PRINT` - -`--print` cannot be used with ESM input. - ### `ERR_EVENT_RECURSION` @@ -3696,6 +3690,17 @@ removed: v15.0.0 The native call from `process.cpuUsage` could not be processed. + + +### `ERR_EVAL_ESM_CANNOT_PRINT` + + + +`--print` cannot be used with ESM input. + [ES Module]: esm.md [ICU]: intl.md#internationalization-support [JSON Web Key Elliptic Curve Registry]: https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve diff --git a/lib/internal/errors.js b/lib/internal/errors.js index ef4295f9eaaaef..910ac072d36c86 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1194,7 +1194,6 @@ E('ERR_ENCODING_INVALID_ENCODED_DATA', function(encoding, ret) { }, TypeError); E('ERR_ENCODING_NOT_SUPPORTED', 'The "%s" encoding is not supported', RangeError); -E('ERR_EVAL_ESM_CANNOT_PRINT', '--print cannot be used with ESM input', Error); E('ERR_EVENT_RECURSION', 'The event "%s" is already being dispatched', Error); E('ERR_FALSY_VALUE_REJECTION', function(reason) { this.reason = reason; diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index e69add7394e60f..7ad8595a818bd9 100644 --- a/lib/internal/process/execution.js +++ b/lib/internal/process/execution.js @@ -12,7 +12,6 @@ const { codes: { ERR_INVALID_ARG_TYPE, ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET, - ERR_EVAL_ESM_CANNOT_PRINT, }, } = require('internal/errors'); const { pathToFileURL } = require('internal/url'); @@ -61,7 +60,7 @@ function getEvalModuleUrl() { */ function evalModuleEntryPoint(source, print) { if (print) { - throw new ERR_EVAL_ESM_CANNOT_PRINT(); + source = `console.log((${source}))`; } RegExpPrototypeExec(/^/, ''); // Necessary to reset RegExp statics before user code runs. return require('internal/modules/run_main').runEntryPointWithESMLoader( diff --git a/test/parallel/test-cli-eval.js b/test/parallel/test-cli-eval.js index b993dd474149e9..f154b596ad95cd 100644 --- a/test/parallel/test-cli-eval.js +++ b/test/parallel/test-cli-eval.js @@ -32,6 +32,7 @@ const assert = require('assert'); const child = require('child_process'); const path = require('path'); const fixtures = require('../common/fixtures'); +const { spawnSyncAndAssert } = require('../common/child_process'); const nodejs = `"${process.execPath}"`; if (process.argv.length > 2) { @@ -224,30 +225,31 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`, // Assert that "42\n" is written to stdout on module eval. -const execOptions = '--input-type module'; -child.exec( - `${nodejs} ${execOptions} --eval "console.log(42)"`, - common.mustSucceed((stdout) => { - assert.strictEqual(stdout, '42\n'); - })); +const execOptions = '--input-type=module'; +spawnSyncAndAssert(process.execPath, [execOptions, '--eval', 'console.log(42)'], { + stdout: '42\n', + stderr: '', +}); // Assert that "42\n" is written to stdout with print option. -child.exec( - `${nodejs} ${execOptions} --print --eval "42"`, - common.mustCall((err, stdout, stderr) => { - assert.ok(err); - assert.strictEqual(stdout, ''); - assert.ok(stderr.includes('--print cannot be used with ESM input')); - })); +spawnSyncAndAssert(process.execPath, [execOptions, '--print', '--eval', 42], { + stdout: '42\n', + stderr: '', +}); // Assert that error is written to stderr on invalid input. -child.exec( - `${nodejs} ${execOptions} --eval "!!!!"`, - common.mustCall((err, stdout, stderr) => { - assert.ok(err); - assert.strictEqual(stdout, ''); - assert.ok(stderr.indexOf('SyntaxError: Unexpected end of input') > 0); - })); +spawnSyncAndAssert(process.execPath, [execOptions, '--print', '--eval', '!!!!'], { + status: 1, + stdout: '', + stderr: /SyntaxError: Unexpected token '\)'\n/, +}); + +// Assert that error is written to stderr on unsupported input. +spawnSyncAndAssert(process.execPath, [execOptions, '-p', 'import "node:os"'], { + status: 1, + stdout: '', + stderr: /SyntaxError: Unexpected string\n/, +}); // Assert that require is undefined in ESM support child.exec(