Skip to content

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

Closed as not planned
@Jimmy89

Description

@Jimmy89

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)"
    ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    not-a-bugNew and existing bug reports incorrectly submitted as bugtracerThis item relates to the Tracer Utility

    Type

    No type

    Projects

    Status

    Closed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions