From 174a19264b8b653facc43317ad9ca24be926b66a Mon Sep 17 00:00:00 2001
From: Ed Pelc <ed@freightchick.com>
Date: Tue, 31 Dec 2024 10:57:25 -0500
Subject: [PATCH] use import() if ERR_REQUIRE_ASYNC_MODULE   - supports node
 v22.12.0 and later when using top level await

---
 lib/shared/require-or-import.js | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/shared/require-or-import.js b/lib/shared/require-or-import.js
index 4cf5877e..af98aac5 100644
--- a/lib/shared/require-or-import.js
+++ b/lib/shared/require-or-import.js
@@ -23,7 +23,8 @@ function requireOrImport(path, callback) {
     if (pathToFileURL && importESM) {
       // Because e.code is undefined on nyc process.
       /* istanbul ignore else */
-      if (e.code === 'ERR_REQUIRE_ESM' || process.env.NYC_CONFIG) {
+      // Check 'ERR_REQUIRE_ASYNC_MODULE' if on node v22.12.0 or later to allow importing from files using top level await.
+      if (e.code === 'ERR_REQUIRE_ESM' || process.env.NYC_CONFIG || e.code === 'ERR_REQUIRE_ASYNC_MODULE') {
         // This is needed on Windows, because import() fails if providing a Windows file path.
         var url = pathToFileURL(path);
         importESM(url).then(function(esm) { callback(null, esm); }, callback);