Description
I'm trying to retrieve the file where an error occurred and all I'm able to catch is a standard JavaScript error with a message but not file.
For example:
index.js
const mdeps = require('module-deps');
const md = mdeps();
md.on('error', (e) => {
console.warn(e);
});
md.end({ file:'./main.js' });
main.js
require('./partial'); // ./partial exists
require('unknown'); // unknown doesn't
The error thrown is:
Error: Cannot find module 'unknown' from '/home/ericmorand/Projects/module-deps-test'
Except by parsing the error message, which is not a serious option - and even this way I would not be able to get the file name, I can't find a way to retrieve the file where the error happened.
I thought that I would be able to catch the correct file by registering to the file
event like this:
const mdeps = require('module-deps');
const md = mdeps();
let currentFile;
md.on('file', (file) => {
currentFile = file;
});
md.on('error', (e) => {
console.warn(currentFile); // will print ./partial
console.warn(e);
});
md.end({ file:'./main.js' });
But the currentFile
contains ./partial.js
because it is the latest file encountered successfully by module-deps. And it is not where the error happens.
Can someone point me to the solution?