How do I catch .fork() errors that call files that don't exist?
var cp = require('child_process');
var fk = cp.fork("missing-file.js");
spews out
module.js:340
throw err;
^
Error: Cannot find module 'path-to-here/missing-file.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
I've tried
try {
var cp = require('child_process');
var fk = cp.fork("missing-file.js");
} catch(err) {
console.log("async error doesn't get caught here.");
}
fk.on('error', function(err) {
console.log("this never gets called");
})
process.on('uncaughtException', function(err) {
console.log("this never gets called either");
});
but none of those catch the error.
Joyent's documentation says an error event should be emitted when:
- The process could not be spawned, or
- The process could not be killed, or
- Sending a message to the child process failed for whatever reason.
But this appears to happen prior to #1.
I looked at Handle errors thrown by require() module in node.js but the solution there doesn't work.
How do I catch this error?