Search code examples
javascriptnode.jsexenode-webkit

How to exec .exe file from Node.js


im using NWJS to create a simple desktop app. I need to connect console c++ app when I do a click event in a button on html5. I heard it's possible using 'child_process' internal module from Nodejs. I didn't get to exec the exe file when I click in the button.

I have next code:

const exec = require('child_process').execFile;
var cmd = 'Test.exe 1';

exec(cmd, function(error, stdout, stderr) {
    // command output is in stdout
    console.log('stdout:', stdout);
    console.log('stderr:', stderr);
    if (error !== null) {
        console.log('exec error:', error);
    }
});

The .exe file has a input parameter (a number) and it returns a simple text with the introduced number. Anyone can help me?

Thanks!


Solution

  • I think you should give this a try

    child_process.execFile(file[, args][, options][, callback])
    

    file <String> The name or path of the executable file to run
    args <Array> List of string arguments
    options <Object>
    
        cwd <String> Current working directory of the child process
        env <Object> Environment key-value pairs
        encoding <String> (Default: 'utf8')
        timeout <Number> (Default: 0)
        maxBuffer <Number> largest amount of data (in bytes) allowed on stdout or stderr - if exceeded child process is killed (Default: 200\*1024)
        killSignal <String> (Default: 'SIGTERM')
        uid <Number> Sets the user identity of the process. (See setuid(2).)
        gid <Number> Sets the group identity of the process. (See setgid(2).)
    
    callback <Function> called with the output when process terminates
    
        error <Error>
        stdout <String> | <Buffer>
        stderr <String> | <Buffer>