I am Building a command line tool in node, and as part of this tool I need to execute commands that I have in my system. The problem is that I don't get the piped output from the child process that I spawn in the program
The code that executed the child process:
import * as Child_process from "child_process";
export function asyncExec(command: string): Promise<string> {
return new Promise((resolve, reject) => {
const child = Child_process.spawn(command, { shell: true });
child.stdout?.pipe(process.stdout);
child.on("error", (err) => {
reject(err);
});
child.on("exit", (code) => {
if (code === 0) {
resolve(code.toString());
} else {
reject(new Error(`Process exited with code ${code}`));
}
});
});
}
I have had another version of this code to try and make it work that uses exec
import * as Child_process from "child_process";
export function asyncExec(command: string): Promise<string> {
return new Promise((resolve, reject) => {
const child = Child_process.exec(command, (err, stdout, stderr) => {
if (err) {
reject(new Error(stderr || err.message));
} else {
resolve(stdout);
}
});
child.stdout?.pipe(process.stdout);
});
}
The stream of the stdout of the child process is not being printed, it is only printed after the promise has resolved
Why ? what can I do to fix this ?
The fix to the problem was doing the following:
const child = Child_process.spawn(command, { shell: true, stdio: "inherit" });