23

I use node.js v4.4.4 and I need to run a .bat file from node.js.

From the location of the js file for my node app the .bat is runnable using command line with the following path (Window platform):

'../src/util/buildscripts/build.bat --profile ../profiles/app.profile.js'

But when using node I cannot run it, no specific error are thrown.

What am I doing wrong here?


    var ls = spawn('cmd.exe', ['../src/util/buildscripts', 'build.bat', '--profile ../profiles/app.profile.js']);

    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);
    });
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • Related: https://nodejs.org/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows – GibboK Apr 13 '16 at 19:17
  • Related: https://medium.com/@graeme_boy/how-to-optimize-cpu-intensive-work-in-node-js-cdc09099ed41#.zbzjytwzw – GibboK Apr 13 '16 at 19:35

2 Answers2

27

You should be able to run a command like this:

var child_process = require('child_process');

child_process.exec('path_to_your_executables', function(error, stdout, stderr) {
    console.log(stdout);
});
  • May I ask you... why do you use .exec instead of spawn? – GibboK Apr 13 '16 at 17:52
  • 1
    Using spawn with shell options set is the same as exec. Documentation [here](https://nodejs.org/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows) –  Apr 14 '16 at 01:21
  • Are you sure? Accordingly to this docs they are different, and I need to use spawn if possible insted of exec. You could please add a spawn solution on your answer? http://www.hacksparrow.com/difference-between-spawn-and-exec-of-node-js-child_process.html – GibboK Apr 14 '16 at 06:04
  • 1
    Taken from the documentation: "When running on Windows, .bat and .cmd files can be invoked using child_process.spawn() with the shell option set, with child_process.exec(), or by spawning cmd.exe and passing the .bat or .cmd file as an argument (which is what the shell option and child_process.exec() do)" followed by an example right after that sentence –  Apr 14 '16 at 06:06
  • Thanks for your comment, unfortunately I am not able to make it run as I need to pass the following parameters --profile ../profiles/app.profile.js.. are you able to modify your answer with an example? I would be glad to accept it. Thanks for your time. – GibboK Apr 14 '16 at 07:37
  • Try `child_process.exec("../src/util/buildscripts/build.bat --profile ../profiles/app.profile.js"), function....` –  Apr 14 '16 at 07:52
  • Thanks, but does not work, paths must be resolved to absolute first for my understanding. – GibboK Apr 14 '16 at 07:55
  • Use `path` module then. You should be able to resolve to that file using `path.join` and `__dirname` variable –  Apr 14 '16 at 07:57
  • I was able to solve my issue as posted below. Thanks anyways for your time on this issue :). – GibboK Apr 14 '16 at 08:06
  • 1
    No problem. Glad to help –  Apr 14 '16 at 08:07
19

The following script solved my issue, basically I had to:

  • Converting to absolute path reference to .bat file.

  • Passing arguments to .bat using an array.

    var bat = require.resolve('../src/util/buildscripts/build.bat');
    var profile = require.resolve('../profiles/app.profile.js');
    var ls = spawn(bat, ['--profile', profile]);
    
    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);
    });
    

Below a list of useful relevant articles:

https://nodejs.org/api/child_process.html#child_process_asynchronous_process_creation

https://nodejs.org/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows

http://www.informit.com/articles/article.aspx?p=2266928

beac0n
  • 50
  • 1
  • 6
GibboK
  • 71,848
  • 143
  • 435
  • 658