Skip to content

Bug: ReferenceError: require is not defined in ES module scope when using @aws-lambda-powertools/tracer in ES Module #2605

@william-mens

Description

@william-mens

Expected Behavior

The package should work seamlessly with ES module syntax without throwing a require error.

Current Behavior

I'm encountering an issue when trying to use the @aws-lambda-powertools/tracer package in a TypeScript project configured as an ES module. The error message suggests that there is an internal use of require which is not supported in ES module scope.

Code snippet

//route.ts file

import { APIGatewayProxyResult, Context } from 'aws-lambda';
import { Tracer } from '@aws-lambda-powertools/tracer';
import { AccountController } from './controllers/AccountController.js';
import { loadConfig } from './config.js';

const tracer = new Tracer({ serviceName: 'transflowAccountService' });

export const updateAccountHandler = async (event: any, context: Context): Promise => {
tracer.getSegment();

const container = await loadConfig();
const account = new AccountController(container);
const response = await account.suspend(event.body as any);
return response;

};

Steps to Reproduce

  1. Configure package.json with "type": "module".
  2. Install @aws-lambda-powertools/tracer and use it in a TypeScript file with ES module syntax.
  3. tsConfig.json
    {
    "compilerOptions": {
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "declaration": true,
    "declarationMap": true,
    "forceConsistentCasingInFileNames": true,
    "noEmitOnError": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "removeComments": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "resolveJsonModule": true,
    "pretty": true,
    "sourceMap": true,
    "strict": true,
    "module": "esnext",
    "moduleResolution": "node",
    "target": "es2019",
    "outDir": "build",
    "rootDir": "src",
    "lib": ["es2019", "DOM"]
    },
    "ts-node": {
    "esm": true,
    "experimentalSpecifierResolution": "node",
    },
    "exclude": ["node_modules", "built"],
    // "include": ["src/.ts", "src/**/.ts"]
    "include": ["src/**/*"],
    }

Possible Solution

No response

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

20.x

Packaging format used

npm

Execution logs

2024-06-05T09:08:00.072Z undefined ERROR Uncaught Exception 
{
    "errorType": "ReferenceError",
    "errorMessage": "require is not defined in ES module scope, you can use import instead\nThis file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.",
    "stack": [
        "ReferenceError: require is not defined in ES module scope, you can use import instead",
        "This file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.",
        "    at ProviderService.captureHTTPsGlobal (file:///var/task/node_modules/@aws-lambda-powertools/tracer/lib/esm/provider/ProviderService.js:33:9)",
        "    at new Tracer (file:///var/task/node_modules/@aws-lambda-powertools/tracer/lib/esm/Tracer.js:122:27)",
        "    at file:///var/task/build/routes.js:11:16",
        "    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)",
        "    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)",
        "    at async _tryAwaitImport (file:///var/runtime/index.mjs:1008:16)",
        "    at async _tryRequire (file:///var/runtime/index.mjs:1057:37)",
        "    at async _loadUserApp (file:///var/runtime/index.mjs:1081:16)",
        "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)",
        "    at async start (file:///var/runtime/index.mjs:1282:23)"
    ]
}

Metadata

Metadata

Assignees

Labels

bug-upstreamThis item is related to a bug caused by upstream dependencytracerThis 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