I would recommend using standard JS promises built into the language over an additional library dependency like Bluebird.
If you’re using Node 10+, the Node.js docs recommend using util.promisify
which returns a Promise<{ stdout, stderr }>
object. See an example below:
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function lsExample() {
try {
const { stdout, stderr } = await exec('ls');
console.log('stdout:', stdout);
console.log('stderr:', stderr);
} catch (e) {
console.error(e); // should contain code (exit code) and signal (that caused the termination).
}
}
lsExample()
Handle errors first from stderr
.