Skip to content

Commit

Permalink
fix issue mochajs#4551 by adding file to error message
Browse files Browse the repository at this point in the history
  • Loading branch information
giltayar authored and juergba committed Feb 3, 2021
1 parent 84d0c96 commit 22544f9
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/esm-utils.js
Expand Up @@ -3,7 +3,29 @@ const url = require('url');

const formattedImport = async file => {
if (path.isAbsolute(file)) {
return import(url.pathToFileURL(file));
try {
return await import(url.pathToFileURL(file));
} catch (err) {
// This is a hack created because ESM in Node.js (at least in Node v15.5.1) does not emit
// the location of the syntax error in the error thrown.
// This is problematic because the user can't see what file has the problem,
// so we add the file location to the error.
// This `if` should be removed once Node.js fixes the problem.
if (
err instanceof SyntaxError &&
err.message &&
err.stack &&
!err.stack.includes(file)
) {
const newErrorWithFilename = new SyntaxError(err.message);
newErrorWithFilename.stack = err.stack.replace(
/^SyntaxError/,
`SyntaxError[ @${file} ]`
);
throw newErrorWithFilename;
}
throw err;
}
}
return import(file);
};
Expand Down

0 comments on commit 22544f9

Please sign in to comment.