Skip to content

Feature request:child_process.exec for powershell #21905

Closed
@tkamenoko

Description

@tkamenoko
  • Version:v10.7.0
  • Platform:Windows 10 64bit
  • Subsystem:child_process

On Windows, child_process.exec (or other methods) supports only cmd.exe as the shell, so running the following shows error messages.

const ChildProcess = require("child_process")

// trying to use powershell core(pwsh).
ChildProcess.exec("echo foo", { shell: "pwsh" }, (error, stdout, stderr) => {
    if (error) {
        console.error("exec error: ", error);
        return;
    }
    console.log("stdout: ", stdout);
    console.log("stderr: ", stderr);
})

result:

exec error:  { Error: Command failed: echo foo
The argument '/d' is not recognized as the name of a script file. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

    at ChildProcess.exithandler (child_process.js:291:12)
    at ChildProcess.emit (events.js:182:13)
    at maybeClose (internal/child_process.js:961:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:248:5) killed: false, code: 64, signal: null, cmd: 'echo foo' }

However, there are alternate shells like powershell, git-bash, etc. So I suggest to fix the behavior.

I suppose the cause is this code(a part of child_process.normalizeSpawnArguments):

node/lib/child_process.js

Lines 469 to 479 in c3f8dd6

if (options.shell) {
const command = [file].concat(args).join(' ');
if (process.platform === 'win32') {
if (typeof options.shell === 'string')
file = options.shell;
else
file = process.env.comspec || 'cmd.exe';
args = ['/d', '/s', '/c', `"${command}"`];
options.windowsVerbatimArguments = true;
} else {

On Windows, it sets /d /s /c for args even if options.shell!=="cmd.exe". Powershell can receive -c option like unix shells, so /d /s /c should be used for only cmd.exe.

Metadata

Metadata

Assignees

No one assigned

    Labels

    child_processIssues and PRs related to the child_process subsystem.feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions