Bolte
Bolte

Reputation: 141

Node - child process spawn path

I would like to run an exe in Windows from my Node Webkit app.

I am trying the below code but it is not working.

document.getElementById('play').onclick = function()
{
    var spawn = require('child_process').spawn;
    var child = spawn(__dirname + '/core.exe', ['/arg1']);

    var snd = new Audio("min.wav"); 
    snd.play();
    win.minimize();

    child.stdout.on('data', function (data) {
        console.log('stdout: ' + data);
    });

    child.stderr.on('data', function (data) {
        console.log('stderr: ' + data);
    });

    child.on('close', function (code) {
        console.log('child process exited with code ' + code);
        var snd = new Audio("restore.wav"); 
        snd.play();
        win.restore();
    });
}

Am I getting the path wrong? I need it to be current directory and run the exe with that name and the example arg.

The output SHOULD be a messagebox, but nothing loads.

Upvotes: 1

Views: 6590

Answers (1)

Bolte
Bolte

Reputation: 141

Managed to figure it out, it wasn't defined because I was using it in browser context. I didn't get the nw.js SDK version for some reason, found that __DIRNAME was undefined. Came up with this solution instead.

    var path = require('path');
    var nwDir = path.dirname(process.execPath);
    var spawn = require('child_process').spawn;
    var child = spawn(nwDir + '/app/core.exe', ['/arg1']);

Now working as intended.

Upvotes: 4

Related Questions