Reputation: 7957
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();
Upvotes: 58
Views: 124642
Reputation: 382502
child_process.execFileSync
You can also use this version of execFile
if you don't want to wait for the callback, documented at: https://nodejs.org/docs/latest-v22.x/api/child_process.html#child_processexecfilesyncfile-args-options
const child_process = require('child_process')
let stdout
stdout = child_process.execFileSync('hai.exe', ['arg0', 'arg1'])
console.log(stdout.toString())
try {
stdout = child_process.execFileSync('hai.exe', ['arg0', 'badarg'])
} catch(e) {
console.log(e.status)
console.log(e.stdout.toString())
console.log(e.stderr.toString())
}
Unfortunately the interface is a bit different than execFile
and slightly worse:
But if those don't matter to you, it can be convenient. Otherwise you can also use the slightly more general child_process.spawnSync
.
Upvotes: 0
Reputation: 498
If you are using Node.js or any front-end framework that supports Node.js (React or Vue.js)
const { execFile } = require('child_process');
const child = execFile('chrome.exe', [], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
If the .exe file is located somewhere in the machine, replace chrome.exe with the path to the application you want to execute
e.g "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
const child = execFile('C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', [], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
Upvotes: 6
Reputation: 2418
You can try the execFile function of the child process modules in Node.js.
Reference: child_process.execFile(file[, args][, options][, callback])
Your 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();
Upvotes: 75
Reputation: 11
Did you ever think about using Batch file in this process? I mean start a .bat file using Node.js which will start an .exe file at the same time?
Just using answers in the top, I got this:
Creating a .bat file in exe file's directory
Type in bat file
START <full file name like E:\\Your folder\\Your file.exe>
Type in your .js file:
const shell = require('shelljs')
shell.exec('E:\\Your folder\\Your bat file.bat')
Upvotes: 0
Reputation: 702
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;
}
Upvotes: 18