Skip to content

Commit 0b79480

Browse files
Copilotnaorpeled
andcommitted
Add documentation for ESM and bundler support
Co-authored-by: naorpeled <[email protected]>
1 parent 15516da commit 0b79480

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ Whatever you decide is best for your use case, **Lambda API** is there to suppor
125125
- [Configuring Routes in API Gateway](#configuring-routes-in-api-gateway)
126126
- [Reusing Persistent Connections](#reusing-persistent-connections)
127127
- [TypeScript Support](#typescript-support)
128+
- [ESM and Bundler Support](#esm-and-bundler-support)
128129
- [Contributions](#contributions)
129130
- [Are you using Lambda API?](#are-you-using-lambda-api)
130131

@@ -1506,6 +1507,56 @@ exports.run = async (event: APIGatewayEvent, context: Context) => {
15061507
};
15071508
```
15081509

1510+
## ESM and Bundler Support
1511+
1512+
Lambda API now supports ES Module (ESM) imports and works seamlessly with modern bundlers like esbuild, webpack, and others when targeting ESM output.
1513+
1514+
### Using with ESM
1515+
1516+
```javascript
1517+
// ESM import (uses index.mjs entry point)
1518+
import createAPI from 'lambda-api';
1519+
1520+
const api = createAPI();
1521+
1522+
api.get('/status', async (req, res) => {
1523+
return { status: 'ok' };
1524+
});
1525+
1526+
export const handler = async (event, context) => {
1527+
return await api.run(event, context);
1528+
};
1529+
```
1530+
1531+
### Bundling with esbuild
1532+
1533+
When bundling your Lambda functions with esbuild for ESM output, Lambda API will automatically work without requiring any additional configuration:
1534+
1535+
```bash
1536+
esbuild your-handler.js --bundle --platform=node --format=esm --outfile=dist/handler.mjs
1537+
```
1538+
1539+
The library includes an ESM entry point (`index.mjs`) that automatically sets up the necessary polyfills for Node.js built-in modules, so you don't need to add any banners or inject scripts to your esbuild configuration.
1540+
1541+
### CommonJS Support (Default)
1542+
1543+
Lambda API continues to support CommonJS out of the box:
1544+
1545+
```javascript
1546+
// CommonJS require (default)
1547+
const createAPI = require('lambda-api');
1548+
1549+
const api = createAPI();
1550+
1551+
api.get('/status', async (req, res) => {
1552+
return { status: 'ok' };
1553+
});
1554+
1555+
module.exports.handler = async (event, context) => {
1556+
return await api.run(event, context);
1557+
};
1558+
```
1559+
15091560
## Contributions
15101561

15111562
Contributions, ideas and bug reports are welcome and greatly appreciated. Please add [issues](https://github.com/jeremydaly/lambda-api/issues) for suggestions and bug reports or create a pull request.

0 commit comments

Comments
 (0)