Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,9 @@ changes:
Evaluate the following argument as JavaScript. The modules which are
predefined in the REPL can also be used in `script`.

If `script` starts with `-`, pass it using `=` (for example,
`node --print --eval=-42`) so it is parsed as the value of `--eval`.

On Windows, using `cmd.exe` a single quote will not work correctly because it
only recognizes double `"` for quoting. In Powershell or Git bash, both `'`
and `"` are usable.
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-cli-eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ child.exec(...common.escapePOSIXShell`"${process.execPath}" -p "\\-42"`, common.
assert.strictEqual(stderr, '');
}));

// Eval expressions that start with '-' can be passed with '='.
child.exec(...common.escapePOSIXShell`"${process.execPath}" --print --eval=-42`, common.mustSucceed((stdout, stderr) => {
assert.strictEqual(stdout, '-42\n');
assert.strictEqual(stderr, '');
}));

// Edge case: negative zero should preserve its sign when printed.
child.exec(...common.escapePOSIXShell`"${process.execPath}" --print --eval=-0`, common.mustSucceed((stdout, stderr) => {
assert.strictEqual(stdout, '-0\n');
assert.strictEqual(stderr, '');
}));

// Nearby-path safety: option-looking values should still be rejected with -e.
child.exec(...common.escapePOSIXShell`"${process.execPath}" -e -p`, common.mustCall((err, stdout, stderr) => {
assert.strictEqual(err.code, 9);
assert.strictEqual(stdout, '');
assert.strictEqual(stderr.trim(),
`${process.execPath}: -e requires an argument`);
}));

// Long output should not be truncated.
child.exec(...common.escapePOSIXShell`"${process.execPath}" -p "'1'.repeat(1e5)"`, common.mustSucceed((stdout, stderr) => {
assert.strictEqual(stdout, `${'1'.repeat(1e5)}\n`);
Expand Down