Execute an exe file using node.js
I don't know how to execute an exe
file 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 exe
file using the command line?
var fun = function() {
console.log("rrrr");
exec('CALL hai.exe', function(err, data) {
console.log(err)
console.log(data.toString());
});
}
fun();
you can try execFile function of child process modules in node.js
Refer: 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();
If the exe
that you want to execute is in some other directory, and your exe
has some dependencies to the folder it resides then, try setting the cwd
parameter in options
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