@@ -1510,6 +1510,13 @@ const char* require_esm_warning =
1510
1510
" or the module should use the .mjs extension.\n "
1511
1511
" - If it's loaded using require(), use --experimental-require-module" ;
1512
1512
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
+
1513
1520
static void CompileFunctionForCJSLoader (
1514
1521
const FunctionCallbackInfo<Value>& args) {
1515
1522
CHECK (args[0 ]->IsString ());
@@ -1553,6 +1560,7 @@ static void CompileFunctionForCJSLoader(
1553
1560
1554
1561
std::vector<Local<String>> params = GetCJSParameters (env->isolate_data ());
1555
1562
1563
+ bool retry_as_esm = false ;
1556
1564
Local<Function> fn;
1557
1565
1558
1566
{
@@ -1597,13 +1605,29 @@ static void CompileFunctionForCJSLoader(
1597
1605
return ;
1598
1606
}
1599
1607
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 ;
1603
1616
}
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 ;
1607
1631
}
1608
1632
}
1609
1633
}
@@ -1623,11 +1647,14 @@ static void CompileFunctionForCJSLoader(
1623
1647
env->cached_data_rejected_string (),
1624
1648
env->source_map_url_string (),
1625
1649
env->function_string (),
1650
+ FIXED_ONE_BYTE_STRING (isolate, " retryAsESM" ),
1626
1651
};
1627
1652
std::vector<Local<Value>> values = {
1628
1653
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),
1631
1658
};
1632
1659
Local<Object> result = Object::New (
1633
1660
isolate, v8::Null (isolate), names.data (), values.data (), names.size ());
0 commit comments