NodeJs script with child_process spawn on Windows, why I need 'shell: true' for ENOENT error?

Viewed 3467

I'm using this code:

const {
  spawn
} = require('child_process');

let info = spawn('npm', ["-v"]);

info.on('close', () => {
  console.log('closed');
}

But I have this error:

events.js:182
      throw er; // Unhandled 'error' event
      ^

Error: spawn npm ENOENT
    at exports._errnoException (util.js:1022:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:189:19)
    at onErrorNT (internal/child_process.js:366:16)
    at _combinedTickCallback (internal/process/next_tick.js:102:11)
    at process._tickCallback (internal/process/next_tick.js:161:9)
    at Function.Module.runMain (module.js:607:11)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:575:3

If I use instead:

let info = spawn('npm', ["-v"], {shell: true});

it works!

But why I need shell: true? I need also to see the stdout of that spawn, so I'm also using this:

let info = spawn('npm', ["-v"], {shell: true, stdio: 'inherit'});

It's correct?

1 Answers
Related