Categories
JavaScript Answers

How to execute an external program from within Node.js?

Spread the love

To execute an external program from within Node.js, we use the exec function.

For instance, we write

const exec = require("child_process").exec;
exec("pwd", (error, stdout, stderr) => {
  // result
});

to call exec with the command we want to run and the callback that’s called when the command is finished running.

We get its output from stdout.

And we get the command’s error output from stderr.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *