How to read NodeJS child-process.exec stdout/stderr using async/await Promises
You want to run a command like file my.pdf using NodeJS child-process.exec and get its stdout after it’s finished.
Solution
TL;DR: (await exec('file my.pdf')).stdout
We’re using child-process-promise here in order to simplify our implementation. Install it using npm i --save child-process-promise !
exec_stdout_example.js
const { exec } = require('child-process-promise');
async function run () {
const ret = await exec(`file my.pdf`);
return ret.stdout;
}
run().then(console.log).catch(console.error);You can also use .stderr instead of .stdout to get the stderr output as a string
Check out similar posts by category:
Javascript, NodeJS
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow