Skip to content

[BUG] Using esbuild to generate an ESM .mjs bundle #295

@Mystogab

Description

@Mystogab

when trying to run this library as an ESM, for instance using esbuild to generate a .mjs file. It fails tu run on AWS lambda with this error:

2025-11-10T21:22:36.250Z	undefined	ERROR	Uncaught Exception 	
{
    "errorType": "Error",
    "errorMessage": "Dynamic require of \"querystring\" is not supported",
    "stack": [
        "Error: Dynamic require of \"querystring\" is not supported",
        "    at file:///var/task/api.mjs:1:388",
        "    at <anonymous> (/var/node_modules/lambda-api/lib/request.js:8:12)",
        "    at file:///var/task/api.mjs:1:499",
        "    at <anonymous> (/var/node_modules/lambda-api/index.js:8:17)",
        "    at file:///var/task/api.mjs:1:499",
        "    at <anonymous> (/var/apps/api/shells/input/api-gateway-shell.ts:3:23)",
        "    at ModuleJob.run (node:internal/modules/esm/module_job:343:25)",
        "    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:647:26)",
        "    at async _tryAwaitImport (file:///var/runtime/index.mjs:1098:16)",
        "    at async _tryRequire (file:///var/runtime/index.mjs:1156:86)"
    ]
}

By adding this

banner: {
    js: "import { createRequire } from 'module'; const require = createRequire(import.meta.url);",
  },

to your esbuild build configuration, or manually adding that line at the top of the output bundled file it works on AWS Lambda using ESM and an MJS entrypoint file.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions