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

I am using node.js v4.4.4 and I need to run the .bat file from node.js.

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

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

But when using node I can not start it, there will not be any special errors.

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); }); 
+6
source share
2 answers

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

  • Converting an absolute path to a .bat file to a link.

  • 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 is a list of useful articles:

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

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

+8
source

You should be able to run this command:

 var child_process = require('child_process'); child_process.exec('path_to_your_executables', function(error, stdout, stderr) { console.log(stdout); }); 
+10
source

All Articles