17

am trying to run a test.bat file inside node.js

here is the code

var exec = require('child_process').execFile;

case '/start':
    req.on('data', function (chunk) {});
    req.on('end', function () {
      console.log("INSIDE--------------------------------:");      
       exec('./uli.bat', function (err, data) {
        console.log(err);
        console.log(data);
        res.end(data);
      });
    });
    break;

while running this node.js file am getting

INSIDE--------------------------------:
{ [Error: Command failed: '.' is not recognized as an internal or ext
nd,
operable program or batch file.
] killed: false, code: 1, signal: null }
3
  • 1
    It would be better if you posted the full output, instead of an oddly cropped portion :). Also, that ./ path looks very unixy
    – salezica
    Mar 27, 2014 at 5:29
  • it is the full output
    – Sush
    Mar 27, 2014 at 5:37
  • Have you tried without the ./ in the path to the .bat file?
    – fiskeben
    Mar 27, 2014 at 6:26

4 Answers 4

17

I have found the solution for it.. and its works fine for me. This opens up a new command window and runs my main node JS in child process. You need not give full path of cmd.exe. I was making that mistake.

var spawn = require('child_process').spawn,
ls    = spawn('cmd.exe', ['/c', 'my.bat']);

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

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

ls.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
5
  • This does not work on window. Spawn works only on linux shell env.
    – Sohan
    Nov 27, 2014 at 11:45
  • 11
    I can confirm that this does, in fact, work in Windows to execute .bat files.
    – davidmdem
    Oct 21, 2015 at 14:32
  • Under windows, in case you want to use relative path for the batch file; use template strings like so: spawn('cmd.exe',["/c", `..\\BINARIES\\t1.bat`],{env: process.env});
    – user3526
    Jan 27, 2018 at 10:34
  • I get error "Uncaught ReferenceError: require is not defined". Anyone know why and how to fix that?
    – Sonalk
    Jun 30, 2020 at 15:17
  • getting error as Uncaught Error: spawn when trying to use this Jul 30, 2021 at 11:32
5

The easiest way I know for execute that is following code :

require('child_process').exec("path/to/your/file.bat", function (err, stdout, stderr) {
    if (err) {
        // Ooops.
        // console.log(stderr);
        return console.log(err);
    }

    // Done.
    console.log(stdout);
});

You could replace "path/to/your/file.bat" by __dirname + "/file.bat" if your file is in the directory of your current script for example.

1
  • No matter how i try to do this, i get the same error: Error: Command failed: E:/devProj/instager/start.bat ERROR: Input redirection is not supported, exiting the process immediately. at ChildProcess.exithandler (child_process.js:217:12) at emitTwo (events.js:106:13) at ChildProcess.emit (events.js:194:7) at maybeClose (internal/child_process.js:899:16) at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5) killed: false, code: 1, signal: null, cmd: 'E:/devProj/instager/start.bat' Feb 4, 2018 at 16:20
5

In Windows, I don't prefer spawn as it creates a new cmd.exe and we have to pass the .bat or .cmd file as an argument. exec is a better option. Example below:

Please note that in Windows you need to pass the path with double backslashes. E.g. C:\\path\\batfilename.bat

const { exec } = require('child_process');
exec("path", (err, stdout, stderr) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(stdout);
});
4

An easier way I know for executing that is the following code :

function Process() {
    const process = require('child_process');
    var ls = process.spawn('script.bat');
    ls.stdout.on('data', function (data) {
        console.log(data);
    });
    ls.stderr.on('data', function (data) {
        console.log(data);
    });
    ls.on('close', function (code) {
        if (code == 0)
            console.log('Stop');
        else
            console.log('Start');
    });
};

Process();

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.