From 40b1c42138c47e89eefa859cd36a9e3d62541e7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 16 Oct 2024 00:25:24 +0100 Subject: fix: improve suggestions and hints when using CommonJS modules (#26287) Ref https://github.com/denoland/deno/issues/26225 --- runtime/fmt_errors.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'runtime/fmt_errors.rs') diff --git a/runtime/fmt_errors.rs b/runtime/fmt_errors.rs index 2d9d09a29..8476aab28 100644 --- a/runtime/fmt_errors.rs +++ b/runtime/fmt_errors.rs @@ -287,14 +287,20 @@ fn get_suggestions_for_terminal_errors(e: &JsError) -> Vec { if let Some(msg) = &e.message { if msg.contains("module is not defined") || msg.contains("exports is not defined") + || msg.contains("require is not defined") { return vec![ - FixSuggestion::info( - "Deno does not support CommonJS modules without `.cjs` extension.", - ), - FixSuggestion::hint( - "Rewrite this module to ESM or change the file extension to `.cjs`.", - ), + FixSuggestion::info_multiline(&[ + cstr!("Deno supports CommonJS modules in .cjs files, or when there's a package.json"), + cstr!("with \"type\": \"commonjs\" option and --unstable-detect-cjs flag is used.") + ]), + FixSuggestion::hint_multiline(&[ + "Rewrite this module to ESM,", + cstr!("or change the file extension to .cjs,"), + cstr!("or add package.json next to the file with \"type\": \"commonjs\" option"), + cstr!("and pass --unstable-detect-cjs flag."), + ]), + FixSuggestion::hint("See https://docs.deno.com/go/commonjs for details"), ]; } else if msg.contains("openKv is not a function") { return vec![ -- cgit v1.2.3