@@ -1510,6 +1510,13 @@ const char* require_esm_warning =
15101510    " or the module should use the .mjs extension.\n " 
15111511    " - If it's loaded using require(), use --experimental-require-module"  ;
15121512
1513+ static  bool  warned_about_require_esm_detection = false ;
1514+ const  char * require_esm_warning_without_detection =
1515+     " The module being require()d looks like an ES module, but it is not " 
1516+     " explicitly marked with \" type\" : \" module\"  in the package.json or " 
1517+     " with a .mjs extention. To enable automatic detection of module syntax " 
1518+     " in require(), use --experimental-require-module-with-detection."  ;
1519+ 
15131520static  void  CompileFunctionForCJSLoader (
15141521    const  FunctionCallbackInfo<Value>& args) {
15151522  CHECK (args[0 ]->IsString ());
@@ -1553,6 +1560,7 @@ static void CompileFunctionForCJSLoader(
15531560
15541561  std::vector<Local<String>> params = GetCJSParameters (env->isolate_data ());
15551562
1563+   bool  retry_as_esm = false ;
15561564  Local<Function> fn;
15571565
15581566  {
@@ -1597,13 +1605,29 @@ static void CompileFunctionForCJSLoader(
15971605          return ;
15981606        }
15991607
1600-         if  (!warned_about_require_esm) {
1601-           USE (ProcessEmitWarningSync (env, require_esm_warning));
1602-           warned_about_require_esm = true ;
1608+         if  (!env->options ()->require_module ) {
1609+           if  (!warned_about_require_esm) {
1610+             USE (ProcessEmitWarningSync (env, require_esm_warning));
1611+             warned_about_require_esm = true ;
1612+           }
1613+           errors::DecorateErrorStack (env, try_catch);
1614+           try_catch.ReThrow ();
1615+           return ;
16031616        }
1604-         errors::DecorateErrorStack (env, try_catch);
1605-         try_catch.ReThrow ();
1606-         return ;
1617+ 
1618+         if  (!env->options ()->require_module_with_detection ) {
1619+           if  (!warned_about_require_esm_detection) {
1620+             USE (ProcessEmitWarningSync (env,
1621+                                        require_esm_warning_without_detection));
1622+             warned_about_require_esm_detection = true ;
1623+           }
1624+           errors::DecorateErrorStack (env, try_catch);
1625+           try_catch.ReThrow ();
1626+           return ;
1627+         }
1628+ 
1629+         //  The file being compiled is likely ESM.
1630+         retry_as_esm = true ;
16071631      }
16081632    }
16091633  }
@@ -1623,11 +1647,14 @@ static void CompileFunctionForCJSLoader(
16231647      env->cached_data_rejected_string (),
16241648      env->source_map_url_string (),
16251649      env->function_string (),
1650+       FIXED_ONE_BYTE_STRING (isolate, " retryAsESM"  ),
16261651  };
16271652  std::vector<Local<Value>> values = {
16281653      Boolean::New (isolate, cache_rejected),
1629-       fn->GetScriptOrigin ().SourceMapUrl (),
1630-       fn.As <Value>(),
1654+       retry_as_esm ? v8::Undefined (isolate).As <Value>()
1655+                    : fn->GetScriptOrigin ().SourceMapUrl (),
1656+       retry_as_esm ? v8::Undefined (isolate).As <Value>() : fn.As <Value>(),
1657+       Boolean::New (isolate, retry_as_esm),
16311658  };
16321659  Local<Object> result = Object::New (
16331660      isolate, v8::Null (isolate), names.data (), values.data (), names.size ());
0 commit comments