Javascript 如何从传递一些参数的 node.js 执行 .bat 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36601121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:16:12  来源:igfitidea点击:

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

javascriptnode.jsbatch-file

提问by GibboK

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

我使用 node.js v4.4.4,我需要.bat从 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):

从我的节点应用程序的 js 文件的位置,.bat 可以使用带有以下路径的命令行运行(Window 平台):

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

回答by

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

回答by GibboK

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);
    });
    
  • 转换为对 .bat 文件的绝对路径引用。

  • 使用数组将参数传递给 .bat。

    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_asynchronous_process_creation

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

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

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