Node.js child process not running in node webkit

I have made a small application with node webkit. Pre-packaging, it works great. But after I pinned it and added to Contents / Resources in node-webkit.app, I get an error when starting the application. It opens fine, but the task it is doing is related to child processes and I get this error:

Failed node.js Error Error: Call ENOENT.

I'm guessing it might be related to the issue raised in this question: Node-Webkit Child Process Exec

because my child processes are calling pdftk, a separate command line program. Ultimately, I would like to install pdftk as part of my application - I haven't been able to figure out how to do that. I tried to enable it as one of the things that needed to be zipped with the rest of the application, but that caused the application to crash right after launch (it would open a window with the correct title, but no content that would close immediately).

So the main question is, how do I install pdftk as part of a packaged node-webkit application so that the application can be launched simply by double-clicking the icon, rather than using the command line? Thanks for any help.

+3


source to share


2 answers


I am assuming your code in question is being executed through node - the main node-webkit entry point: https://github.com/rogerwang/node-webkit/wiki/Node-main

If any exception (there) that is not caught in your application will crash .

Unfortunately, the breakpad function for receiving alarms does not work on OSX at the moment: https://github.com/rogerwang/node-webkit/issues/2518

How to prevent node-webkit from crashing immediately

Wrap the code in try / catch to prevent the crash and get information about why the crash occurred.

try {

    the_child_process = child_process.spawn(pathToBin, args);

} catch (err) {

    global.console.log( "Error while trying to start child process: " + JSON.stringify(err) );
}

      



This is a general tip for a situation like you to identify the real cause of the problem.

How to include binary with your node-webkit application

There are several things.

  • Including binaries inside your application. nw

    This should be self-explanatory - but there is one caveat that caused me some problems: Make sure the binary is marked executable via chmod 755

    . If you are using grunt, you might like grunt-chmod. Your binaries are now part of your application package and you can execute them knowing the absolute path.

  • Resolve the path to the binary at runtime, even if packed. The following code snippet is my solution for choosing the correct binary for the current platform, assuming your tool is multi-platform. It also assumes that your binaries are organized in a specific folder structure. Alternatively select the correct binary during the build process and always use the same path.

    var arch = process.arch;
    var platform = process.platform;
    // this will return the root path of your app-package at runtime
    var rootDir = process.cwd(); 
    var isWin = false;
    
    var execPath = rootDir;
    
    // some base path is appended
    execPath = path.join(execPath, 'path', 'to', 'bin');
    
    // select folder for current platform
    switch (platform) {
        case 'darwin':
            execPath = path.join(execPath, 'mac');
            break;
        case 'linux':
            execPath = path.join(execPath, 'lin');
            break;
        case 'win32':
            execPath = path.join(execPath, 'win');
            isWin = true;
            break;
        default:
            global.console.log("unsupported platform: " + platform);
            return null;
    }
    
    // select folder for current processor architecture
    switch (arch) {
        case 'ia32':
            execPath = path.join(execPath, 'x86');
            break;
        case 'x64':
            execPath = path.join(execPath, 'x64');
            break;
        default:
            global.console.log("unsupported architecture: " + arch);
            return null;
    }
    
    // add executable filename
    execPath = path.join(execPath, 'node');
    
    if (isWin) {
        execPath = execPath + ".exe";
    }
    
    global.console.log("Path to your binary: " + execPath);
    
    return execPath;
    
          

  • Eliminate paths that will end up being passed to your binary. It was also a little confusing because all paths were treated as the relative root path of the application package. My node-master file is in a folder in my application, so I thought I should link to files relatively from there, but that was not the case.

    app package root
    |--- package.json     <- node-webkit package.json
    |
    |--- client           <- here my sources for the frontend reside
    |
    |--- server           
    |----|--- node_modules  <- server runtime dependencies
    |----|--- src           <- server source code
    |----|----|--- server.js  <- this is my node server file to execute via node
    |
    |--- node-webkit      <- node webkit code and dependencies
    |----|--- bin           <- a directory with my deployed binaries
    |----|--- node-main.js  <- this is my node main file
    
          

To call the node binary with my server file, the following line succeeded:

    child_process.spawn(absPathToNodeBin, "server/src/server.js");

      

+4


source


This is my experience:

var child_process = nw.require('child_process');

      

//exec
var exec = child_process.exec;
var ret = exec('cat *');

      




//spawn
var sp = child_process.spawn;
var sr = sp('cat *');

      

0


source







All Articles