简体   繁体   中英

How to run exe file from node.js script?

I have an exe and I want to run it from node.js, and pass an argument, except that it doesn't work..

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

It definitely exists as it prints exists, and I can run it from a cmd prompt giving manual as an argument. But through node.js it is not working. It comes back with

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'
}

Does anyone know?

Thanks

Arguments should be passed as an array of strings as the second argument, like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM