Skip to content

Bug: require() command in ES module, stopping lambda from running #2464

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
Jimmy89 opened this issue May 1, 2024 · 5 comments
Closed

Bug: require() command in ES module, stopping lambda from running #2464

Jimmy89 opened this issue May 1, 2024 · 5 comments
Labels
not-a-bug New and existing bug reports incorrectly submitted as bug tracer This item relates to the Tracer Utility

Comments

@Jimmy89
Copy link

Jimmy89 commented May 1, 2024

Expected Behavior

If captureHTTPsRequests is set to true that it works within an ES Modules project.

Current Behavior

        "ReferenceError: require is not defined in ES module scope, you can use import instead",

Code snippet

Within the function ProviderService.captureHTTPsGlobal a require statement in included, which halts ES modules.

File @aws-lambda-powertools/tracer/lib/esm/provider/ProviderService.js:27:9

captureHTTPsGlobal() {
        // eslint-disable-next-line @typescript-eslint/no-var-requires
        captureHTTPsGlobal(require('http'));
        // eslint-disable-next-line @typescript-eslint/no-var-requires
        captureHTTPsGlobal(require('https'));
    }

Steps to Reproduce

  1. Generate any ES module NodeJS project, with the index file named as index.mjs, with a simple tracer config, as long as captureHTTPsRequests: true is set.
  2. Ensure that the project is using ES modules.
  3. Run as lambda, get a crash.

Possible Solution

I've not explored the solution, but you could try to use dynamic import instead of requires for ES Modules projects.

Powertools for AWS Lambda (TypeScript) version

2.1.0

AWS Lambda function runtime

20.x

Packaging format used

npm

Execution logs

{
    "errorType": "ReferenceError",
    "errorMessage": "require is not defined in ES module scope, you can use import instead",
    "stack": [
        "ReferenceError: require is not defined in ES module scope, you can use import instead",
        "    at ProviderService.captureHTTPsGlobal (file:///var/task/node_modules/@aws-lambda-powertools/tracer/lib/esm/provider/ProviderService.js:27:9)",
        "    at new Tracer (file:///var/task/node_modules/@aws-lambda-powertools/tracer/lib/esm/Tracer.js:122:27)",
        "    at new Trace (file:///var/task/service/logging/trace.mjs:8:19)",
        "    at retrieveTrace (file:///var/task/service/logging/trace.mjs:82:13)",
        "    at connectToMemoryDb (file:///var/task/service/memorydb/config/store.mjs:82:17)",
        "    at file:///var/task/index.mjs:22:7",
        "    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)",
        "    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)",
        "    at async _tryAwaitImport (file:///var/runtime/index.mjs:1008:16)",
        "    at async _tryRequire (file:///var/runtime/index.mjs:1057:86)"
    ]
}
@Jimmy89 Jimmy89 added bug Something isn't working triage This item has not been triaged by a maintainer, please wait labels May 1, 2024
@dreamorosi
Copy link
Contributor

Hi, this is a known limitation of AWS X-Ray SDK that doesn't support ESM fully and requires monkey patching the http and https modules.

Are you by chance bundling your code? If so, you can insert a banner to polyfill the require keyword as shown in our v2 upgrade guide.

The example shows how to do this with esbuild, but if you're using another bundler it will work the same.

@Jimmy89
Copy link
Author

Jimmy89 commented May 1, 2024

Thank you @dreamorosi for your quick response. I forget about the upgrade guide. At this time we are not using a bundler (yet).

I will close this ticket then, hopefully the X-Ray SDK starts to support ESM as well :)

Copy link
Contributor

github-actions bot commented May 1, 2024

⚠️ 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.

@dreamorosi dreamorosi closed this as not planned Won't fix, can't repro, duplicate, stale May 1, 2024
@dreamorosi dreamorosi added tracer This item relates to the Tracer Utility not-a-bug New and existing bug reports incorrectly submitted as bug and removed bug Something isn't working triage This item has not been triaged by a maintainer, please wait labels May 1, 2024
@dreamorosi dreamorosi moved this from Coming soon to Closed in Powertools for AWS Lambda (TypeScript) May 1, 2024
Copy link
Contributor

github-actions bot commented May 1, 2024

⚠️ 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.

@HaaLeo
Copy link
Contributor

HaaLeo commented May 15, 2024

I faced the same issue using webpack as my bundler. I used its Banner Plugin to solve this:

new webpack.BannerPlugin({
        banner: "import { createRequire } from 'module';const require = createRequire(import.meta.url);",
        raw: true
    })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

3 participants