@@ -1688,13 +1688,19 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(
16881688 return scope.Escape (fn);
16891689}
16901690
1691+ static std::string GetRequireEsmWarning (Local<String> filename) {
1692+ Isolate* isolate = Isolate::GetCurrent ();
1693+ Utf8Value filename_utf8 (isolate, filename);
1694+
1695+ std::string warning_message =
1696+ " Failed to load the ES module: " + std::string (*filename_utf8) +
1697+ " . Make sure to set \" type\" : \" module\" in the nearest package.json "
1698+ " file "
1699+ " or use the .mjs extension." ;
1700+ return warning_message;
1701+ }
1702+
16911703static bool warned_about_require_esm = false ;
1692- // TODO(joyeecheung): this was copied from the warning previously emitted in the
1693- // JS land, but it's not very helpful. There should be specific information
1694- // about which file or which package.json to update.
1695- const char * require_esm_warning =
1696- " To load an ES module, set \" type\" : \" module\" in the package.json or use "
1697- " the .mjs extension." ;
16981704
16991705static bool ShouldRetryAsESM (Realm* realm,
17001706 Local<String> message,
@@ -1780,8 +1786,9 @@ static void CompileFunctionForCJSLoader(
17801786 // This needs to call process.emit('warning') in JS which can throw if
17811787 // the user listener throws. In that case, don't try to throw the syntax
17821788 // error.
1789+ std::string warning_message = GetRequireEsmWarning (filename);
17831790 should_throw =
1784- ProcessEmitWarningSync (env, require_esm_warning ).IsJust ();
1791+ ProcessEmitWarningSync (env, warning_message. c_str () ).IsJust ();
17851792 }
17861793 if (should_throw) {
17871794 isolate->ThrowException (cjs_exception);
0 commit comments