From 24f809cbe957e954fa8c7f1d768d4a514aea3bc5 Mon Sep 17 00:00:00 2001 From: Richard J <106497642+richard-stafflink@users.noreply.github.com> Date: Fri, 11 Jul 2025 00:04:34 +0000 Subject: [PATCH] fix(api-server): lambdaLoader: improve handler import logic... For ESModule and CommonJS compatibility. When using a bundler like Rollup, the handler function is exported as default. --- .../api-server/src/plugins/lambdaLoader.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/api-server/src/plugins/lambdaLoader.ts b/packages/api-server/src/plugins/lambdaLoader.ts index a554dab536ab..2233e2e39355 100644 --- a/packages/api-server/src/plugins/lambdaLoader.ts +++ b/packages/api-server/src/plugins/lambdaLoader.ts @@ -28,7 +28,23 @@ export const setLambdaFunctions = async (foundFunctions: string[]) => { const ts = Date.now() const routeName = path.basename(fnPath).replace('.js', '') - const { handler } = await import(`file://${fnPath}`) + const fnImport = await import(`file://${fnPath}`) + const handler: Handler = (() => { + if ('handler' in fnImport) { + // ESModule export of handler - when using `export const handler = ...` - most common case + return fnImport.handler + } + if ('default' in fnImport) { + if ('handler' in fnImport.default) { + // CommonJS export of handler - when using `module.exports.handler = ...` or `export default { handler: ... }` + // This is less common, but required for bundling tools that export a default object, like esbuild or rollup + return fnImport.default.handler + } + // Default export is not expected, so skip it + } + // If no handler is found, return undefined - we do not want to throw an error + })() + LAMBDA_FUNCTIONS[routeName] = handler if (!handler) { console.warn(