ChildProcess spawn ignores options param

  • Version: v9.10.1+
  • Platform: Darwin
  • Subsystem: child_process
const cp = require('child_process');

const pwd = cp.spawn('pwd', undefined, { cwd: '/' })

pwd.stdout.on('data', data => console.log(data.toString()));
admin$ pwd
/tmp
admin$ node test.js 
/tmp

I know how to fix it, just pass an empty array as a second argument, or completely skip it.

But that behavior a little bit weird, the second parameter (args) is optional, so I guessed undefined is okay for it.

When using TypeScript some IDEs suggests that undefined value could be passed (cuz param is optional) and that's why in my project I passed undefined to it and got a weird program behavior.

screen shot 2018-12-08 at 10 03 53 pm