49

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();
LyzandeR
  • 35,731
  • 12
  • 70
  • 82
divz
  • 7,597
  • 23
  • 52
  • 74

4 Answers4

67

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();
Andrei Surdu
  • 2,196
  • 3
  • 21
  • 31
Chirag Jain
  • 2,218
  • 3
  • 20
  • 22
  • How can I run exce inside some folder like I want to run imagemagik command by using folder structure exec('C:\Program Files\ImageMagick-6.9.1-Q16-HDRI/convert.exe convert -version', function(){ }) – Umashankar Aug 31 '18 at 10:18
  • To add two arguments to the command for example '/t' and '600' add `exec('HelloJithin.exe', ['/t', '600'], function(err, data) { console.log(err) console.log(data.toString()); });` – CyberProdigy Oct 27 '18 at 14:26
11

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

Basilin Joe
  • 592
  • 9
  • 23
0

If you are using Node Js or any front end framework that supports Node JS (React or Vue)

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);
});
Obot Ernest
  • 155
  • 4
  • 11
-1

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 in the same time?

just using answers in the top i got this:

  1. Creating a .bat file in exe file's directory

  2. Type in bat file START <full file name like E:\\Your folder\\Your file.exe>

  3. Type in your .js file: const shell = require('shelljs') shell.exec('E:\\Your folder\\Your bat file.bat')