Run a batch file in NodeJS

NodeJS is an open-source, cross-platform runtime environment used for multiple tasks, it can interact with files, implement all types of databases, perform various operations, etc. NodeJS can also run the bash files asynchronously without blocking the other block of code.

This tutorial provides a complete guide to running Batch Files in NodeJS.

Introduction to Batch Files

A bash file can contain various commands to be executed line-by-line. It is helpful when the requirement is to run many commands multiple times, in the bash file group a series of commands together, and then instead of running them separately just run a single bash file.

There are various ways to run a batch file such as using cmd, terminal, third-party applications, etc but let’s learn to execute it using NodeJS.

Running Batch Files in NodeJS

For running batch files in NodeJS we can execute the command to run the bash file on a bash shell as a child process which can be done by using the Child Process Module. Child Process Module is a built-in module in NodeJS that has multiple methods to start a child process, one of them is the spawn() method which can be used to initiate processes that can run individually on a shell and then terminate once the execution is completed.

We have covered the entire concept of the child process and its method in another tutorial NodeJS Child Process – A Complete Guide if you want to read it.

Using the spawn() Method to Run Batch Files

This method takes a command, multiple arguments, and options to start a new process. The options can take an object {shell: true} which instructs to run the command inside a shell. By default, it is false. For running the command to execute a batch file it is not required to pass any option, so it is completely optional.

Syntax:

spawn(command[,arguments][,options])

where

  • command is a string to be executed,
  • arguments can be multiple arguments, by default it is an empty array,
  • options is an optional argument that can contain properties like stdio, uid, gid, shell, etc.

Steps to Run a Batch File in NodeJS

1. Import Child Process Module.

It is required to import Child Process Module for using the spawn() method.

const childProcess = require("child_process");

2. Use spawn() Method

For running a bash file, pass the “/bin/bash”  as a command and the file path of the bash file as an argument to spawn() method.

const bash_run = childProcess.spawn('/bin/bash', [filePath]);

3. Get the Result

Then use the stdout to get the data received and the stderr to get the error if it occurs.

bash_run.stdout.on('data', function(data) {
  console.log('Output: ' + data);
});
bash_run.stderr.on('data', function(data) {
  console.log('Error: ' + data);
});

If you find the above code confusing, consider reading our tutorial Ultimate Guide to Understanding Streams in Nodejs, where we have described these terms.

Example of Executing a Batch File in NodeJS

Here we have a bash file “bash.sh” having the below script. 

#!/bin/bash
echo "Hello World!";

It starts with a she-bang(#!) and /bin/bash, indicating that the file is executed as a bash program, and echo prints the string “Hello World!” as an output.

Below is the code to execute a bash file and print the output in the console.

const childProcess = require("child_process");

const bash_run = childProcess.spawn('/bin/bash', ["bash.sh"]);

bash_run.stdout.on('data', function(data) {
  console.log('Output: ' + data);
});
bash_run.stderr.on('data', function(data) {
  console.log('Error: ' + data);
});

Output:

Output: Hello World!

Summary

NodeJS is an open-source, cross-platform runtime environment that uses JavaScript for creating fast, data-intensive, and secure server-side applications. It can also run the bash files directly. A bash file is a file containing the script to run various commands line-by-line. A bash file can execute as a child process. For running a child process, a child process module can use that has various methods to start a child process, one of the easy-to-implement methods is the spawn() method. Hope this tutorial guide you to run the batch files in NodeJS.

Reference

https://stackoverflow.com/questions/22678933/run-a-windows-batch-file-from-node-js

Aditya Gupta
Aditya Gupta
Articles: 109