In Node.js, I can run a shell command with a program that looks like the following
const { exec } = require("child_process")
exec('/path/to/long-running-commnd', function(err, stdout, stderr) {
// function will get here with a fully populated stdout
// when command
console.log(stdout)
})
That is, the exec
function accepts a shell command, will run it, and when the command finishes running it will call the second callback argument.
This works, but if there's a long running command that prints output to console as it finishes sub-tasks (something like a long running build script), my node program will not print out anything until the entire command is run.
Does Node.js offer an API that would allow me to run a shell process and receive output as that output is written to stdout rather than waiting until the entire command is done?