使用 node.js 执行 exe 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19762350/
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-09-02 16:10:56  来源:igfitidea点击:

Execute an exe file using node.js

javascriptnode.js

提问by divz

I don't know how to execute an exefile in node.js. Here is the code I am using. It is not working and doesn't print anything. Is there any possible way to execute an exefile using the command line?

我不知道如何exenode.js. 这是我正在使用的代码。它不工作,不打印任何东西。有没有可能exe使用命令行执行文件的方法?

var fun = function() {
  console.log("rrrr");
  exec('CALL hai.exe', function(err, data) {

    console.log(err)
    console.log(data.toString());
  });
}
fun();

回答by Chirag Jain

you can try execFile function of child process modules in node.js

您可以在 node.js 中尝试子进程模块的 execFile 函数

Refer: http://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback

参考:http: //nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback

You code should look something like:

您的代码应该类似于:

var exec = require('child_process').execFile;

var fun =function(){
   console.log("fun() start");
   exec('HelloJithin.exe', function(err, data) {  
        console.log(err)
        console.log(data.toString());                       
    });  
}
fun();

回答by Basilin Joe

If the exethat you want to execute is in some other directory, and your exehas some dependencies to the folder it resides then, try setting the cwdparameter in options

如果exe您要执行的 是在其他目录中,并且您exe对它所在的文件夹有一些依赖性,请尝试cwd在选项中设置参数

var exec = require('child_process').execFile;
/**
 * Function to execute exe
 * @param {string} fileName The name of the executable file to run.
 * @param {string[]} params List of string arguments.
 * @param {string} path Current working directory of the child process.
 */
function execute(fileName, params, path) {
    let promise = new Promise((resolve, reject) => {
        exec(fileName, params, { cwd: path }, (err, data) => {
            if (err) reject(err);
            else resolve(data);
        });

    });
    return promise;
}

Docs

文档