JavaScript Answers

How to run shell script file using Node?

Spread the love

To run shell script file using Node, we use the exec function.

For instance, we write

const { exec } = require("child_process");
const yourscript = exec("sh", (error, stdout, stderr) => {
  if (error !== null) {
    console.log(`exec error: ${error}`);

to call exec to rub the sh command.

We get the output from the stdout parameter in the callback.

And we get error output from the stderr parameter in the callback.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.