By default, Node.js throws the following exception when a file is not found.
{ [Error: ENOENT, no such file or directory \'InvalidFile\']
errno: 34,
code: \'
I'm answering subquestion #2. The accepted answer, though excellent, seems to omit it.
On Node, the [Error: ...] element can be accessed as .stack on the exception object. It's a string, which you can then parse with regular expressions to get, for instance, the module and line number.
For instance, you could parse the first stack frame like this:
...
} catch (e) {
var msg, file, line, col;
[msg,file,col] = e.stack.match(/\((.*):(\d+)\)/);
if (file) {
[,file,line] = file.match(/(.*):(\d+)/);
}
}