@@ -1688,13 +1688,19 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(
1688
1688
return scope.Escape (fn);
1689
1689
}
1690
1690
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
+
1691
1703
static 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." ;
1698
1704
1699
1705
static bool ShouldRetryAsESM (Realm* realm,
1700
1706
Local<String> message,
@@ -1780,8 +1786,9 @@ static void CompileFunctionForCJSLoader(
1780
1786
// This needs to call process.emit('warning') in JS which can throw if
1781
1787
// the user listener throws. In that case, don't try to throw the syntax
1782
1788
// error.
1789
+ std::string warning_message = GetRequireEsmWarning (filename);
1783
1790
should_throw =
1784
- ProcessEmitWarningSync (env, require_esm_warning ).IsJust ();
1791
+ ProcessEmitWarningSync (env, warning_message. c_str () ).IsJust ();
1785
1792
}
1786
1793
if (should_throw) {
1787
1794
isolate->ThrowException (cjs_exception);
0 commit comments