最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node - child process spawn path - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Jul 30, 2017 at 22:12 BolteBolte 1413 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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.

发布评论

评论列表(0)

  1. 暂无评论