Sometimes, we want to run a command line binary with Node.js.
In this article, we’ll look at how to run a command line binary with Node.js.
How to run a command line binary with Node.js?
To run a command line binary with Node.js, we can use the child_process
module.
For instance, we write
const {
exec
} = require('child_process');
exec('cat *.js bad_file | wc -l', (err, stdout, stderr) => {
if (err) {
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
to call exec
with the command we want to run and the callback that runs when the command is done.
In the callback we return the standout output content with stdout
.
And we return the standard error output with stderr
.
Conclusion
To run a command line binary with Node.js, we can use the child_process
module.