如何从node.js脚本运行exe文件?

我有一个exe,我想从node.js中运行它,并传递一个参数,除了它不起作用外。.

var exec = require('child_process').execfile;
const fs = require('fs');

try {
  if (fs.existsSync("./program/bin/Release/program.exe")) {
    console.log("Exists")
  } else {
    console.log("Not Exists")
  }
} catch(err) {
  console.log(err)
}

setTimeout(function() {
    exec('./program/bin/Release/program.exe manual',function(err,data) {  
        console.log(err)
        console.log(data.toString());                       
    });  
},0);

它在打印时肯定存在,我可以从cmd提示符运行manual作为参数来运行它。但是通过node.js,它不起作用。回来了

Error: spawn ./program/bin/Release/program.exe manual ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:264:19)
    at onErrorNT (internal/child_process.js:456:16)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  errno: 'ENOENT',code: 'ENOENT',syscall: 'spawn ./program/bin/Release/program.exe manual',path: './program/bin/Release/program.exe manual',spawnargs: [],cmd: './program/bin/Release/program.exe manual'
}

有人知道吗?

谢谢

snaketigger 回答:如何从node.js脚本运行exe文件?

参数应作为第二个参数作为字符串数组传递,如下所示:

exec('./program/bin/Release/program.exe',['manual'],function(err,data) {  
    console.log(err)
    console.log(data.toString());                       
});  

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

本文链接:https://www.f2er.com/3163903.html

大家都在问