Execute an exe file using node.js

前端 未结 2 2035
北荒
北荒 2020-11-27 13:40

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 possi

相关标签:
2条回答
  • 2020-11-27 13:49

    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();
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题