58

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();
1
  • On Windows, presumably? Commented Jul 3, 2023 at 14:28

5 Answers 5

75

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();
Sign up to request clarification or add additional context in comments.

2 Comments

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(){ })
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()); });
19

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

Comments

6

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

Comments

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

Comments

0

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:

  • you can't get stderr, or it is harder
  • it throws in case of exit status error rather than just returning you the exit status. But you can inspect exit status, stdout and stderr in that case from the exception.

But if those don't matter to you, it can be convenient. Otherwise you can also use the slightly more general child_process.spawnSync.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.