Unable to use SDK with ESM bundling in AWS Lambda / Nodejs 14.x
- Dominant language
- JavaScript
- Stars
- 280
- Forks
- 157
- PR merge metrics
- No merged PRs in 30d
Description
A couple of weeks ago AWS announced [support for ESM modules](https://aws.amazon.com/about-aws/whats-new/2022/01/aws-lambda-es-modules-top-level-await-node-js-14/) in AWS Lambda for `node.js >=14.x` runtimes. To quote the announcement:
> ECMAScript (ES) modules are a packaging format for JavaScript code, used to publish JavaScript code libraries so they can be imported and re-used in other applications. Until now, Lambda’s Node.js runtimes only supported code using the earlier CommonJS packaging format. With this release, customers can use the ES module format for both their function handler and any code they import.
When trying to bundle a function as ESM module with `aws-xray-sdk-node` as dependency, simply importing the SDK in AWS Lambda an error is thrown:
```
{
"errorType": "Error",
"errorMessage": "Dynamic require of \"util\" is not supported",
"trace": [
"Error: Dynamic require of \"util\" is not supported",
" at file:///var/task/index.mjs:13:9",
" at lib/node_modules/cls-hooked/context.js (file:///var/task/index.mjs:278:16)",
" at __require2 (file:///var/task/index.mjs:16:50)",
" at lib/node_modules/aws-xray-sdk-core/dist/lib/context_utils.js (file:///var/task/index.mjs:1701:15)",
" at __require2 (file:///var/task/index.mjs:16:50)",
" at lib/node_modules/aws-xray-sdk-core/dist/lib/aws-xray.js (file:///var/task/index.mjs:5860:24)",
" at __require2 (file:///var/task/index.mjs:16:50)",
" at lib/node_modules/aws-xray-sdk-core/dist/lib/index.js (file:///var/task/index.mjs:5950:22)",
" at __require2 (file:///var/task/index.mjs:16:50)",
" at lib/node_modules/aws-xray-sdk/lib/index.js (file:///var/task/index.mjs:6364:19)"
]
}
```
We have received [a bug report](https://github.com/awslabs/aws-lambda-powertools-typescript/issues/490) from a Customer using `@aws-lambda-powertools/tracer`, an utility that depends on this SDK.
I was able to reproduce the error using CDK & `esbuild` with ESM output format enabled.
Below a reproduction example with `"aws-cdk-lib": "^2.8.0"` & `"esbuild": "^0.14.11"`
```typescript
import { Stack, StackProps, Duration } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Tracing, Runtime } from 'aws-cdk-lib/aws-lambda';
import { NodejsFunction, OutputFormat } from 'aws-cdk-lib/aws-lambda-nodejs';
export class DemoStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
new NodejsFunction(this, 'HelloWorldFunction', {
entry: './lib/main.ts',
handler: 'handler',
runtime: Runtime.NODEJS_14_X,
tracing: Tracing.ACTIVE,
timeout: Duration.minutes(2),
bundling: {
format: OutputFormat.ESM // <-- ENABLING THIS CAUSES THE ISSUE
}
});
}
}
```
Content of `lib/main.ts`, function has a single dependency of `"aws-xray-sdk": "^3.3.4"`
```typescript
import { getSegment } from 'aws-xray-sdk';
export const handler = () => {
const segment = getSegment();
try {
throw new Error('hello');
} catch (error) {
console.log(error);
}
}
```
Please don't hesitate to ping me if you need additional details.
Contributor guide
Assessment
This issue has not been assessed yet.