How to run shell script file or command using Nodejs?

If you ever wanted to run some automation script or file in your Unix/Linux machine using nodejs?
It may be to build your binaries from source code or for some tooling in your dev workflow. Nodejs Provides well-matured APIs for doing these operations and there are plenty of npm modules to ease the pain creating shell or terminal based cli’s using nodejs. We will explore both options one by one.

Article Contents

Run Shell or bash command using Nodejs

We will be using Node.js inbuilt module  child_process for doing this operation.

For buffered, non-stream formatted output:

const { exec } = require('child_process');
exec('ls | grep js', (err, stdout, stderr) => {
  if (err) {
    //some err occurred
    console.error(err)
  } else {
   // the *entire* stdout and stderr (buffered)
   console.log(`stdout: ${stdout}`);
   console.log(`stderr: ${stderr}`);
  }
});

If you want  to use promises,

const util = require('util');
const exec = util.promisify(require('child_process').exec);

async function lsWithGrep() {
  try {
      const { stdout, stderr } = await exec('ls | grep js');
      console.log('stdout:', stdout);
      console.log('stderr:', stderr);
  }catch (err)=>{
     console.error(err);
  };
};

lsWithGrep();

If you want to use output stream,

const { spawn } = require('child_process');
const child = spawn('ls', ['-la', '/myproject']);

// use child.stdout.setEncoding('utf8'); if you want text chunks
child.stdout.on('data', (chunk) => {
  // data from the standard output is here as buffers
});

// since these are streams, you can pipe them elsewhere
child.stderr.pipe(dest);

child.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

if you want to execute shell script synchronously,

Then check out following APIs:

const { execSync } = require('child_process');
// stderr is sent to stdout of parent process
// you can set options.stdio if you want it to go elsewhere
const stdout = execSync('ls');


const { spawnSync} = require('child_process');
const child = spawnSync('ls', ['-la', '/myproject']);

console.error('error', child.error);
console.log('stdout ', child.stdout);
console.error('stderr ', child.stderr);

 

See also  Find the host's cloud provider using Node.Js

Run Shell or bash file using Nodejs

if you want to execute whole shell script file, instead of commands, Then see the following code,
You can use any of the above methods to achieve this functionality.

const exec = require('child_process').exec, child;
const myShellScript = exec('sh doSomething.sh /myDir');

myShellScript.stdout.on('data', (data)=>{
    console.log(data); 
    // do whatever you want here with data
});

myShellScript.stderr.on('data', (data)=>{
    console.error(data);
});

Nodejs Documentation for this can found here.

you can also use child_process.execFile method also to executing file.

If you want to  use, third-party npm modules, then check out:

You can find many more in npmjs.com

Now you can do some scripting Nodejs. Happy scripting. These Methods come pretty handily when you’re building custom dev workflow or you want to do some automation when in the workflow.

Leave a Comment