aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
2.96k stars 557 forks source link

Error when importing @aws-sdk/client-s3 #6114

Closed pkrucz00 closed 3 weeks ago

pkrucz00 commented 1 month ago

Checkboxes for prior research

Describe the bug

When importing the @aws-sdk/client-s3 package:

import {
  ListObjectsCommandOutput,
  ListObjectsV2Output,
  PutObjectCommandInput,
  S3 as S3Client,
} from '@aws-sdk/client-s3';

there terminal throws an error TypeError: Cannot read properties of undefined (reading 'randomUUID'). This makes some of tests in our application fail.

SDK version number

@aws-sdk/client-s3 3.577.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v20.11.0

Reproduction Steps

import {
  ListObjectsCommandOutput,
  ListObjectsV2Output,
  PutObjectCommandInput,
  S3 as S3Client,
} from '@aws-sdk/client-s3';

Observed Behavior

Test suite failed to run

    TypeError: Cannot read properties of undefined (reading 'randomUUID')

      1 | import * as fs from 'fs';
      2 | import * as zlib from 'zlib';
    > 3 | import {
        | ^
      4 |   ListObjectsCommandOutput,
      5 |   ListObjectsV2Output,
      6 |   PutObjectCommandInput,

      at Object.<anonymous> (../../node_modules/@smithy/middleware-retry/node_modules/uuid/dist/native.js:13:31)
      at Object.<anonymous> (../../node_modules/@smithy/middleware-retry/node_modules/uuid/dist/v4.js:8:38)
      at Object.<anonymous> (../../node_modules/@smithy/middleware-retry/node_modules/uuid/dist/index.js:65:34)
      at Object.<anonymous> (../../node_modules/@smithy/middleware-retry/dist-cjs/index.js:51:19)
      at Object.<anonymous> (../../node_modules/@aws-sdk/client-s3/dist-cjs/index.js:270:31)
      at Object.<anonymous> (src/utils/S3.ts:3:1)
      at Object.<anonymous> (src/utils/index.ts:3:1)
      at Object.<anonymous> (src/controllers/root/middleware/validateWebhookRequest.ts:5:1)
      at Object.<anonymous> (src/controllers/root/middleware/__tests__/validateWebhookRequest.test.ts:2:1)

Expected Behavior

The client should have been imported without problems.

Possible Solution

No response

Additional Information/Context

No response

kuhe commented 1 month ago

Does your Node.js environment have the crypto package?

require('crypto').randomUUID

This is used via uuid.v4 for generating random idempotency tokens.

pkrucz00 commented 1 month ago

Hi thank you for responding fast!

My node.js environment has the crypto package. A simple test on top of the script proved it's correct:

    try {
        require('crypto');
    } catch {
        console.log("crypto not found, exiting...");
        process.exit(1);
    }
    const crypto = require('crypto');
    const { randomUUID } = crypto;
    console.log(randomUUID());

This logged:

    4bd5df87-42db-4c84-a035-9319ba8d5476

and did not exit with 1. So it looks like the environment is ok. Do you have more questions that could solve the issue?

aBurmeseDev commented 1 month ago

Hi @pkrucz00 - thanks for reaching out and your patience here.

I think we could use some more detailed steps on reproducing the error. Correct me if I'm wrong but it seems like you're using Jest to test and as I did more digging, this may be more of Jest related rather than SDK. Some of the suggestions I've found are:

Those are just my assumptions and if you could share step-by-step to reproduce, I'd attempt to replicate it on my end.

Hope it helps, John

pkrucz00 commented 3 weeks ago

Hi! Thank you for the research. It showed me a direction toward finding and fixing the bug. It was an error related with a jest mock of the crypto package. The mock was meant to resolve all the package methods as they are implemented. Probably there was a change on how do the packages load, so the mock did not work properly. The problem is gone now.

Thank you for your answers.

Regards, Pawel

github-actions[bot] commented 1 week ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.