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

fix: add suggestion how to fix importing CJS module #21764

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
16 changes: 16 additions & 0 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,28 @@ fn exit_with_message(message: &str, code: i32) -> ! {
std::process::exit(code);
}

fn maybe_format_commonjs_error(e: &JsError) -> Option<String> {
if e.name == Some("ReferenceError".to_string()) {
if let Some(msg) = &e.message {
if msg.contains("module is not defined") {
return Some(format!("{} Deno doesn't support CommonJS modules, change problematic module into an ES module.", colors::cyan("hint:")));
}
}
}

None
}

fn exit_for_error(error: AnyError) -> ! {
let mut error_string = format!("{error:?}");
let mut error_code = 1;

if let Some(e) = error.downcast_ref::<JsError>() {
error_string = format_js_error(e);
// TODO(bartlomieju): there must be a smarter way to add hint to `JsError`.
if let Some(commonjs_error) = maybe_format_commonjs_error(e) {
error_string = format!("{}\n\n {}", error_string, commonjs_error);
}
} else if let Some(args::LockfileError::IntegrityCheckFailed(e)) =
error.downcast_ref::<args::LockfileError>()
{
Expand Down