The following will never exit
var child_process = require(\'child_process\');
var ps = child_process.spawn(\'.\\\\node_modules\\\\.bin\\\\babel.cmd\', [\'in
You are spawning child process cmd.exe by using babel.cmd. This process then starts another grandchild process node and runs babel script. When you do ps.kill() it only kills the cmd.exe but not the child processes created by it.
Although cmd.exe is closed, the stdio stream of the parent process is still shared with the other processes in the tree. So the parent process is waiting for the stream to be closed. Using stdio: 'ignore' will only stop sharing the stdio stream with other processes in the tree but those grandchild processes will still continue running.
Update:
After discussing with OP, I tested with all three stdio options: pipe(default), inherit, ignore in iojs 3.3.0, Windows 7 (32bit). None of them kills the grandchild process.
With inherit and ignore, both parent process and child process(cmd.exe) are killed but the grand child process still floats around and doesn't belong to any process tree.
With pipe, only the child process is terminated but both parent and grand child process continue running. The reason that parent process is not exiting is most likely because the stdio of parent node.exe is still shared with the other processes as mentioned in original answer.
child.unref() doesn't have any effect on killing grand child process. It only removes the child process from parent's event loop so that the parent process can exit normally.
I could think of couple of solutions:
Invoke the babel.js directly without that cmd script:
var ps = child_process.spawn('node', ['.\\node_modules\\babel\\bin\\babel.js', 'input.js', '--out-file', 'output.js', '--watch'])
Use windows taskkill command with \T flag to kill the process and all the child processes started by it:
os = require('os');
if(os.platform() === 'win32'){
child_process.exec('taskkill /pid ' + ps.pid + ' /T /F')
}else{
ps.kill();
}
There are some npm modules that can handle killing child processes in both Unix and Windows e.g. tree-kill. For windows it uses taskkill or tasklist.