From 21f757c2e8268c11efd3169c753b8a56e27808b5 Mon Sep 17 00:00:00 2001 From: Remy Date: Tue, 1 Apr 2025 14:32:09 +0200 Subject: [PATCH 1/2] fix(functions,emulators): loadTriggers() with top level await Context: ESM functions with firestore triggers The expected behavior of `loadTriggers()` is to try and `require()` the function entrypoint, falling back to dynamic `import()` when node raises a ERR_REQUIRE_ESM error. But node may also raise an ERR_REQUIRE_ASYNC_MODULE error when the module has top level awaits This commit simply makes it so ERR_REQUIRE_ASYNC_MODULE also falls back to `import()` --- src/deploy/functions/runtimes/node/triggerParser.js | 2 +- src/emulator/functionsEmulatorRuntime.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/deploy/functions/runtimes/node/triggerParser.js b/src/deploy/functions/runtimes/node/triggerParser.js index d05b72f842f..6773049d0bf 100644 --- a/src/deploy/functions/runtimes/node/triggerParser.js +++ b/src/deploy/functions/runtimes/node/triggerParser.js @@ -21,7 +21,7 @@ async function loadModule(packageDir) { try { return require(packageDir); } catch (e) { - if (e.code === "ERR_REQUIRE_ESM") { + if (e.code === "ERR_REQUIRE_ESM" && e.code !== "ERR_REQUIRE_ASYNC_MODULE") { const modulePath = require.resolve(packageDir); // Resolve module path to file:// URL. Required for windows support. const moduleURL = url.pathToFileURL(modulePath).href; diff --git a/src/emulator/functionsEmulatorRuntime.ts b/src/emulator/functionsEmulatorRuntime.ts index 8720c3b08ab..bfb1732a8cc 100644 --- a/src/emulator/functionsEmulatorRuntime.ts +++ b/src/emulator/functionsEmulatorRuntime.ts @@ -909,7 +909,7 @@ async function loadTriggers(): Promise { try { triggerModule = require(process.cwd()); } catch (err: any) { - if (err.code !== "ERR_REQUIRE_ESM") { + if (err.code !== "ERR_REQUIRE_ESM" && err.code !== "ERR_REQUIRE_ASYNC_MODULE") { // Try to run diagnostics to see what could've gone wrong before rethrowing the error. await moduleResolutionDetective(err); throw err; From d77f276b82524001274087d64a5e7b932f37c1b3 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Tue, 20 May 2025 11:36:19 -0700 Subject: [PATCH 2/2] Add changelog. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e69de29bb2d..1da268b8f25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -0,0 +1 @@ +- Fixed bug where functions packaged as ESM doesn't load on emulator for some versions of Node.js 22. (#8394)