Skip to content Skip to sidebar Skip to footer

How To Execute A .bat File From Node.js Passing Some Parameters?

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 (W

Solution 1:

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


Post a Comment for "How To Execute A .bat File From Node.js Passing Some Parameters?"