Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(); 
like image 771
divz Avatar asked Nov 04 '13 05:11

divz


People also ask

How do I run an EXE file from node JS?

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.

How do I run a .EXE file?

Double-click an EXE file to run it. EXE files are Windows executable files, and are designed to be run as programs. Double-clicking any EXE file will start it.

Can you make exe files with JavaScript?

Compile one JavaScript file into an executable using nexe In our case, it'll create Windows executable file with the name “executable”. Double-tap on it or right-click and select the run command to start the application.

How do I run a file in terminal node?

You can Run your JavaScript File from your Terminal only if you have installed NodeJs runtime. If you have Installed it then Simply open the terminal and type “node FileName. js”. If you don't have NodeJs runtime environment then go to NodeJs Runtime Environment Download and Download it.


2 Answers

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

like image 21
Basilin Joe Avatar answered Sep 20 '22 09:09

Basilin Joe


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(); 
like image 188
Chirag Jain Avatar answered Sep 18 '22 09:09

Chirag Jain