I am trying to write a simple node program that spawns a child process in a separate window (that can be viewed) and then, on program completion, prints the stderr messages. Currently testing on Windows.
I made a parent.js file:
const { spawnSync } = require('child_process')
const child = spawnSync('node child.js', [], { shell: true, detached: true })
console.log(child)
I am using both 'shell' and 'detached' parameters because it was the only way I could find to open and view a new terminal window.
Then, in child.js:
let count = 0
setInterval(() => {
console.log('test', count)
count++
if (count == 4) {
process.stderr.write('an error occurred!')
process.exit(1)
}
}, 1000)
Which is a very simple test program that after 4 seconds writes to stderr and then exits with an error code.
After running, however, I receive this output:
{
status: 1,
signal: null,
output: [ null, <Buffer >, <Buffer > ],
pid: 3500,
stdout: <Buffer >,
stderr: <Buffer >
}
So, while the error code registered correctly, stderr is an empty buffer. What am I doing wrong? What else could I do to achieve what I'm looking for? (run a node child process in a separate window and get errors from parent).
Any help is really appreciated. Thank you!