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 Commented Mar 27, 2014 at 5:29
  • it is the full output Commented Mar 27, 2014 at 5:37
  • Have you tried without the ./ in the path to the .bat file? Commented 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);
});
Sign up to request clarification or add additional context in comments.

5 Comments

This does not work on window. Spawn works only on linux shell env.
I can confirm that this does, in fact, work in Windows to execute .bat files.
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});
I get error "Uncaught ReferenceError: require is not defined". Anyone know why and how to fix that?
getting error as Uncaught Error: spawn when trying to use this
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 Comment

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'
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);
});

Comments

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();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.