How to execute a .bat file from node.js passing some parameters?

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

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