Skip to content

Conversation

Copy link

Copilot AI commented Nov 10, 2025

Fix ESM compatibility when bundling with esbuild

  • Add ESM/CommonJS compatibility shim to files using built-in Node modules
    • Created index.mjs as ESM entry point
    • Added require() polyfill for ESM context
  • Create test to verify ESM bundling works
    • Test esbuild bundling without banner
    • Test CommonJS backward compatibility (enhanced with full end-to-end test)
    • Test ESM import functionality
    • Test CommonJS bundling with esbuild (backward compatibility)
    • Comprehensive manual testing with multiple routes
  • Test existing functionality still works
    • All 27 existing test suites pass
    • New ESM test suite passes (4 tests total)
    • 482 tests pass (4 new tests added)
  • Run linter
    • All linter checks pass
    • Prettier formatting applied
  • Add documentation
    • Added ESM and Bundler Support section to README
    • Included examples for ESM import
    • Included esbuild bundling example
    • Added to table of contents
  • Review and finalize
    • Verified the bundled ESM code executes correctly
    • Confirmed CommonJS usage still works
    • Tested multiple route types and methods
    • Added comprehensive non-ESM regression tests
Original prompt

This section details on the original issue you should resolve

<issue_title>[BUG] Using esbuild to generate an ESM .mjs bundle</issue_title>
<issue_description>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.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 11, 2025 00:06
- Add index.mjs as ESM entry point with require() polyfill
- Update package.json with module and exports fields
- Add comprehensive tests for ESM bundling
- Maintain backward compatibility with CommonJS

Co-authored-by: naorpeled <6171622+naorpeled@users.noreply.github.com>
Co-authored-by: naorpeled <6171622+naorpeled@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ESM bundle generation issue with esbuild Add ESM entry point for esbuild compatibility Nov 11, 2025
Copilot AI requested a review from naorpeled November 11, 2025 00:12
Copilot finished work on behalf of naorpeled November 11, 2025 00:12
@naorpeled
Copy link
Collaborator

@copilot make sure to also add tests to make sure there's no regression for non ESM usage

- Enhanced CommonJS require test with full end-to-end functionality
- Added test for CommonJS bundling with esbuild
- Verified all existing tests still pass
- Ensured no regression for CommonJS users

Co-authored-by: naorpeled <6171622+naorpeled@users.noreply.github.com>
Copy link
Author

Copilot AI commented Nov 11, 2025

@copilot make sure to also add tests to make sure there's no regression for non ESM usage

Added comprehensive regression tests for non-ESM usage:

  1. Enhanced the existing CommonJS test to include full end-to-end functionality with api.run() (not just API instantiation)
  2. Added a new test for CommonJS bundling with esbuild to ensure the library still bundles correctly for CommonJS users

All 28 test suites pass (486 total tests including 4 new tests in the ESM test suite). Commit: b8f4f3c

Copilot finished work on behalf of naorpeled November 11, 2025 00:26
@naorpeled
Copy link
Collaborator

@copilot you haven't committed the cjs tests

Copy link
Author

Copilot AI commented Nov 11, 2025

@copilot you haven't committed the cjs tests

The CJS tests are committed in b8f4f3c. The test file __tests__/esm-bundle.unit.js contains 4 tests including:

  1. "should bundle with esbuild for ESM without requiring banner"
  2. "should work with CommonJS require (backward compatibility)" - enhanced with full end-to-end testing
  3. "should work with ESM import"
  4. "should bundle with esbuild for CommonJS without breaking (backward compatibility)" - the CJS bundling test (lines 164-240)

The file was created in commit 15516da with 3 tests, then modified in commit b8f4f3c to add the 4th CJS bundling test. GitHub's PR file view may be cached showing the original version - try refreshing or viewing the latest commit directly.

Copilot finished work on behalf of naorpeled November 11, 2025 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants