55

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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
divz
  • 7,847
  • 23
  • 55
  • 78

5 Answers5

74

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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chirag Jain
  • 2,378
  • 3
  • 21
  • 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
  • 1
    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
16

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
  • 672
  • 1
  • 10
  • 23
4

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);
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Obot Ernest
  • 412
  • 8
  • 19
0

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:

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
var3n1k
  • 11
0

I have tried this. It works.

const { exec } = require('child_process');

// Replace 'path/to/your/file.exe' with the actual path to your .exe file
const filePath = 'path/to/your/file.exe';

// Execute the .exe file
exec(filePath, (error, stdout, stderr) => {
  if (error) {
    console.error(`Error executing the .exe file: ${error}`);
    return;
  }

  // Process the output or error messages if needed
  if (stdout) {
    console.log(`Standard output: ${stdout}`);
  }

  if (stderr) {
    console.error(`Standard error: ${stderr}`);
  }
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Many of your answers here, including several which you've made Community Wikis for some reason, appear likely to have been entirely or partially written by AI (e.g., ChatGPT). Please be aware that [posting AI-generated content is not allowed here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. – NotTheDr01ds Jul 03 '23 at 00:31
  • **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. The moderation team can use your help to identify quality issues. – NotTheDr01ds Jul 03 '23 at 00:31
  • An attempt to obfuscate it by only using the generated code and by using broken English? – Peter Mortensen Jul 03 '23 at 14:15