Search code examples
node.jswebkitexenode-webkit

How to run external exe in Node Webkit?


I'm using Node Webkit for my web app and I am really a newbie in working with node webkit. I want to run my exe in my application but I can not even open simple notepad using 'child_process'. I have seen some examples on websites and still I am finding it hard to run notepad.exe, please help and thank you very much in advance.

var execFile = require 
('child_process').execFile, child;

child = execFile('C:\Windows\notepad.exe',
function(error,stdout,stderr) { 
if (error) {
            console.log(error.stack); 
            console.log('Error code: '+ error.code); 
            console.log('Signal received: '+ 
            error.signal);
           } 
console.log('Child Process stdout: '+ stdout);
console.log('Child Process stderr: '+ stderr);
 }); 
child.on('exit', function (code) { 
console.log('Child process exited '+
'with exit code '+ code);
});

Also i tried to run exe using meadco-neptune plugin and to add plugin I put code in package.json file but it shows that plug-in cannot be loaded. My package.json file is like this

 {
   "name": "sample",
   "version": "1.0.0",
   "description": "",
   "main": "index.html",
   "window": {
   "toolbar": false,
   "frame": false,
   "resizable": false,
   "show": true,

   "title": " example"
             },
   "webkit": {
   "plugin": true
             },
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
             },
    "author": "xyz",
    "license": "ISC"
 }

Solution

  • In node.js there are two methods to launch an external program using standard module child_process: exec and spawn.

    When using exec you get stdout and stderror information upon the external program exit. Data is returned to node.js only then, as Mi Ke Bu correctly noted in comments.

    But if you want receieve data from the external program interactively (I suspect you are not really going to launch notepad.exe), you should use another method - spawn.

    Consider the example:

    var spawn = require('child_process').spawn,
        child    = spawn('C:\\windows\\notepad.exe', ["C:/Windows/System32/Drivers/etc/hosts"]);
    
    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);
    });
    

    Also you need to use double backward slashes in path name: C:\\Windows\\notepad.exe, otherwise your path is evaluated as
    C:windows notepad.exe (with line return) which of course does not exist.

    Or you could just use forward slashes, as in command-line arguments in the example.