Skip to content
Related Articles

Related Articles

How to run a batch file in Node.js with input and get an output ?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 19 May, 2022

In this article, we are going to see how can you run a batch file with input and get an output file in Node.js. A batch file is a script file that stores command to be executed in a serial order.

Node.js is asynchronous in nature, it doesn’t wait for the batch file to finish its execution. Instead, it calls functions that define when the batch file is executed, and hence it tries to print the data even when it does not fetch yet.

Solution: The solution is to use a Node.js module named Child Process. Child Process contains a method ‘spawn’ that spawns the child process asynchronously, without blocking the Node.js.

Let’s see the complete process step-by-step.

Step 1: Create a folder, inside this folder create the app.js file, input.txt file, and bash.sh file. Inside input.txt file and bash.sh file write the code given below.

input.txt

Hello Geeks!

bash.sh

#!/bin/bash
input=`cat -`
echo "Input: $input"

Step 2: Locate this folder into the terminal & type the command

npm init -y

It initializes our node.js application.

Step 3: Install Child Process modules inside the project using the following command

node install child_process

Step 4: Inside the ‘app.js’ file, require the Child Process module, and create a constant ‘bash_run’ for executing the bash file.

const childProcess = require("child_process");

const bash_run = childProcess.spawn(
    '/bin/bash',["test.sh"],{env: process.env});

Step 5: Now, let’s use the ‘stdout’ method to print the result and ‘stderr’ to print the error. 

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

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

Step 6: Now, use File System Module to create Read and Write Stream for Input and Output files respectively. After that, we will send the get result of the input text file to the output text file using the bash_run method we created earlier.

const fs = require("fs");

const input = fs.createReadStream("input.txt");
const output = fs.createWriteStream("output.txt");

bash_run.stdout.pipe(output);
input.pipe(bash_run.stdin);

Complete Code:

app.js




const childProcess = require("child_process");
  
const bash_run = childProcess.spawn('/bin/bash'
    ["bash.sh"], { env: process.env });
  
bash_run.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
});
bash_run.stderr.on('data', function (data) {
    console.log('stderr: ' + data);
});
  
const fs = require("fs");
  
const output = fs.createWriteStream("output.txt");
const input = fs.createReadStream("input.txt");
  
bash_run.stdout.pipe(output);
  
input.pipe(bash_run.stdin);

Step to run the application: Open the terminal and type the following command.

node app.js

Output:

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!