node.js – TypeError: Converting circular structure to JSON – while reading json file data from s3 in Node 18

I am getting TypeError: Converting circular structure to JSON error while reading json file content from s3 using node 18. I am using S3Client, GetObjectCommand of @aws-sdk/client-s3.

TypeError: Converting circular structure to JSON
--> starting at object with constructor 'TLSSocket'
| property 'parser' -> object with constructor 'HTTPParser'
--- property 'socket' closes the circle
at JSON.stringify ()
at responseHook (/opt/wrapper.js:147:24)
at /opt/nodejs/node_modules/@opentelemetry/instrumentation-aws-sdk/build/src/aws-sdk.js:149:61
at safeExecuteInTheMiddle (/opt/nodejs/node_modules/@opentelemetry/instrumentation/build/src/utils.js:28:18)
at AwsInstrumentation._callUserResponseHook (/opt/nodejs/node_modules/@opentelemetry/instrumentation-aws-sdk/build/src/aws-sdk.js:149:54)
at /opt/nodejs/node_modules/@opentelemetry/instrumentation-aws-sdk/build/src/aws-sdk.js:269:34
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

I am transforming s3 content into string using .transformToString('utf-8') method. Most probably transformToString() is throwing this error. Here is the code snippet.

const client = new S3Client({
            region
        });
const params = {
            Bucket: bucket,
            Key: key
        };
const response = await client.send(new GetObjectCommand(params));
const body = await response.Body.transformToString('utf-8');

error screenshot

Read more here: Source link