-
Notifications
You must be signed in to change notification settings - Fork 154
Bug: Dynamic require error with ESM functions #2290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for opening your first issue here! We'll come back to you as soon as we can. |
Hi @lukehedger, unfortunately this is expected and due to the fact that In order to still use it with your ESM functions you'll have to include a banner in the code that creates a polyfill for the import { Stack, type StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { NodejsFunction, OutputFormat } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
export class MyStack extends Stack {
public constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const handler = new NodejsFunction(this, 'helloWorldFunction', {
architecture: Architecture.ARM_64,
bundling: {
format: OutputFormat.ESM,
banner:
"import { createRequire } from 'module';const require = createRequire(import.meta.url);",
},
runtime: Runtime.NODEJS_20_X,
tracing: Tracing.ACTIVE,
});
}
} This is covered in the migration guide and the release notes for v2. Let me know if adding it fixes the issue. |
Thanks Andrea - missed this in the docs, works fine now! |
This issue is now closed. Please be mindful that future comments are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so. |
Expected Behaviour
ESM Lambda function with Tracer enabled should be invoked without error
Current Behaviour
Lambda invocation error received:
Code snippet
Lambda function with following configuration in CDK app (some options removed for brevity):
Steps to Reproduce
Possible Solution
No response
Powertools for AWS Lambda (TypeScript) version
latest
AWS Lambda function runtime
20.x
Packaging format used
npm
Execution logs
No response
The text was updated successfully, but these errors were encountered: