Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file location when SyntaxError happens in ESM (fixes #4551) #4557

Merged
merged 3 commits into from
Feb 3, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix issue #4551 by adding file to error message
  • Loading branch information
giltayar authored and juergba committed Feb 3, 2021
commit 22544f9bb39bcc4de05785af87b589426731a6b4
24 changes: 23 additions & 1 deletion lib/esm-utils.js
Original file line number Diff line number Diff line change
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));
juergba marked this conversation as resolved.
Show resolved Hide resolved
} 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