Skip to content

Bug: importing Tracer from powertools v2 causes SyntaxError #2163

Closed
@webdeveric

Description

@webdeveric

Expected Behaviour

No error thrown

Current Behaviour

When running my ESM Lambda's locally, just importing Tracer will cause the following error any time I hit one of my routes.

Runtime.UserCodeSyntaxError: SyntaxError: The requested module 'aws-xray-sdk-core' does not provide an export named 'Segment'

Code snippet

It looks like the issue is how Segment and/or Subsegment are being imported from aws-xray-sdk-core in the transpiled js code.

node -e "import { Tracer } from '@aws-lambda-powertools/tracer';" --input-type=module 

Steps to Reproduce

Create tracer.mjs and put this in it.

import { Tracer } from '@aws-lambda-powertools/tracer';

Run node tracer.mjs

You should see something like this:

[...]node_modules/@aws-lambda-powertools/tracer/lib/esm/provider/ProviderService.js:1
import { captureAWS, captureAWSClient, captureAWSv3Client, captureAsyncFunc, captureFunc, captureHTTPsGlobal, getNamespace, getSegment, setSegment, Segment, setContextMissingStrategy, setDaemonAddress, setLogger, } from 'aws-xray-sdk-core';
                                                                                                                                                    ^^^^^^^
SyntaxError: Named export 'Segment' not found. The requested module 'aws-xray-sdk-core' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'aws-xray-sdk-core';
const { captureAWS, captureAWSClient, captureAWSv3Client, captureAsyncFunc, captureFunc, captureHTTPsGlobal, getNamespace, getSegment, setSegment, Segment, setContextMissingStrategy, setDaemonAddress, setLogger, } = pkg;

Possible Solution

import pkg from 'aws-xray-sdk-core';
const { Segment } = pkg;

Powertools for AWS Lambda (TypeScript) version

2.0.1

AWS Lambda function runtime

20.x

Packaging format used

npm

Execution logs

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingcompletedThis item is complete and has been merged/shippedtracerThis item relates to the Tracer Utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions