1

Simply, i can't use the child_process.execFile(). It always contain error in callback.

child.js

console.log('I\'m child');

main.js

var cp = require('child_process');

cp.execFile('./child.js', function (err, stdout,stderr) {
    console.log('Err: ',err);
    console.log('STDerr: ',stderr);
});

The error object in the callback is

{ [Error: spawn Unknown system errno 193]
  code: 'Unknown system errno 193',
  errno: 'Unknown system errno 193',
  syscall: 'spawn' }
Thant Sin Aung
  • 682
  • 2
  • 10
  • 20

1 Answers1

6

The problem is that child.js is not a valid executable program. If you are on Linux or Mac you can fix that by writing this at the very top of child.js:

#!/usr/bin/env node

You will then need to say chmod +x child.js to make the file executable. The line is called a "shebang line" and it tells the system what interpreter to use to run the rest of the file. You'll need to have node in your $PATH for it to work.

If you don't like that, or are not using a Unix-like system, you can do this:

cp.execFile('/some/path/to/node', ['./child.js'])

The first argument must be the full path to your node interpreter program (perhaps node.exe on Windows, I don't know if that matters).

Finally, none of this really makes that much sense if you don't really need to launch a second node interpreter process. You can try including one file in the other--some ideas for that are here: How do I include a JavaScript file in another JavaScript file?

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • @Green: Fixed, Node wants `args` to be an array. Not sure if that changed since I wrote the answer 1.5 years ago, but anyway please try the updated version. – John Zwinck Nov 04 '15 at 21:06