Skip to content

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

Closed
lukehedger opened this issue Mar 27, 2024 · 4 comments
Closed

Bug: Dynamic require error with ESM functions #2290

lukehedger opened this issue Mar 27, 2024 · 4 comments
Assignees
Labels
need-response This item requires a response from a customer and will considered stale after 2 weeks not-a-bug New and existing bug reports incorrectly submitted as bug tracer This item relates to the Tracer Utility

Comments

@lukehedger
Copy link

Expected Behaviour

ESM Lambda function with Tracer enabled should be invoked without error

Current Behaviour

Lambda invocation error received:

{
  "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:1:382",
    "    at <anonymous> (/node_modules/cls-hooked/context.js:4:14)",
    "    at file:///var/task/index.mjs:1:458",
    "    at <anonymous> (/node_modules/aws-xray-sdk-core/dist/lib/context_utils.js:5:11)",
    "    at file:///var/task/index.mjs:1:458",
    "    at <anonymous> (/node_modules/aws-xray-sdk-core/dist/lib/aws-xray.js:2:20)",
    "    at file:///var/task/index.mjs:1:458",
    "    at <anonymous> (/node_modules/aws-xray-sdk-core/dist/lib/index.js:3:18)",
    "    at file:///var/task/index.mjs:1:458",
    "    at <anonymous> (/node_modules/@aws-lambda-powertools/tracer/lib/esm/provider/ProviderService.js:1:21)"
  ]
}

Code snippet

Lambda function with following configuration in CDK app (some options removed for brevity):

new NodejsFunction(this, "MyFunction", {
  architecture: Architecture.ARM_64,
  bundling: {
    format: OutputFormat.ESM,
  },
  runtime: Runtime.NODEJS_20_X,
  tracing: Tracing.ACTIVE,
})

Steps to Reproduce

  1. Deploy an ESM Lambda function with Tracer
  2. Invoke function
  3. Observe error

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

@lukehedger lukehedger added triage This item has not been triaged by a maintainer, please wait bug Something isn't working labels Mar 27, 2024
Copy link

boring-cyborg bot commented Mar 27, 2024

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #typescript channel on our Powertools for AWS Lambda Discord: Invite link

@dreamorosi dreamorosi added tracer This item relates to the Tracer Utility not-a-bug New and existing bug reports incorrectly submitted as bug need-response This item requires a response from a customer and will considered stale after 2 weeks and removed bug Something isn't working triage This item has not been triaged by a maintainer, please wait labels Mar 28, 2024
@dreamorosi dreamorosi self-assigned this Mar 28, 2024
@dreamorosi
Copy link
Contributor

Hi @lukehedger, unfortunately this is expected and due to the fact that aws-xray-sdk-core - which we depend on - is built as a CommonJS module.

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 require keyword:

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.

@lukehedger
Copy link
Author

Thanks Andrea - missed this in the docs, works fine now!

Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need-response This item requires a response from a customer and will considered stale after 2 weeks not-a-bug New and existing bug reports incorrectly submitted as bug tracer This item relates to the Tracer Utility
Projects
Development

No branches or pull requests

2 participants