aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
3.05k stars 574 forks source link

`fromCognitoIdentityPool` function is called, browser throw `Error: Requiring unknown module "838"` #6207

Closed hexinatgithub closed 3 months ago

hexinatgithub commented 3 months ago

Checkboxes for prior research

Describe the bug

Use AWS SDK in browser, when fromCognitoIdentityPool is called, browser throw error.

SDK version number

@aws-sdk/credential-providers@^3.600.0

Which JavaScript Runtime is this issue in?

Browser

Details of the browser/Node.js/ReactNative version

Edge/v20.14.0/^0.74.2

Reproduction Steps

import * as s3 from '@aws-sdk/client-s3';
import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers";
import outputs from '../backend-stack-outputs.json';

const region = outputs.InterviewBackendStack.Region;
const identityPoolId = outputs.InterviewBackendStack.IdentityPoolID;

const s3Client = new s3.S3Client({
    region: region,
    credentials: fromCognitoIdentityPool({
        clientConfig: { region },
        identityPoolId,
    }),
});

export const uploader = async (bucketName: string, fileName: string, file: Blob) => {
    try {
        const putObjectCommand = new s3.PutObjectCommand({
            Bucket: bucketName,
            Key: fileName,
            Body: file,
        });
        await s3Client.send(putObjectCommand);
        console.log(`uploader succeed: ${bucketName}/${fileName}`);
    } catch (err) {
        console.log('uploader error: ', err);
    }
}

I have code like above, when uploader function is called, error throwed.

Observed Behavior

browser throw error:

uploader error:  Error: Requiring unknown module "838". If you are sure the module exists, try restarting Metro. You may also want to run `yarn` or `npm install`.
    at unknownModuleError (require.js:318:10)
    at loadModuleImplementation (require.js:240:11)
    at guardedLoadModule (require.js:180:12)
    at metroRequire (require.js:89:7)
    at Function.metroImportAll [as importAll] (require.js:131:19)
    at asyncRequireImpl (asyncRequire.js:18:20)
    at asyncRequire (asyncRequire.js:23:10)
    at provider (fromCognitoIdentityPool.js:11:57)
    at fromCognitoIdentityPool.js:34:18
    at coalesceProvider (memoize.js:8:23)

Goto the node_modules/@aws-sdk/credential-provider-cognito-identity/dist-es/fromCognitoIdentityPool.js file at line 11, vscode also show problem:

line 11:    const { GetIdCommand, CognitoIdentityClient } = await import("./loadCognitoIdentity");
Could not find a declaration file for module './loadCognitoIdentity'.

Expected Behavior

Expect upload file to S3 bucket with credentials.

Possible Solution

It seems related to TypeScript dynamic module Import. ./loadCognitoIdentity module not static bundle.

Additional Information/Context

package.json

{
  "name": "fovus-interview",
  "version": "0.0.1",
  "main": "expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web"
  },
  "dependencies": {
    "@aws-sdk/client-cognito-identity": "^3.600.0",
    "@aws-sdk/client-dynamodb": "^3.600.0",
    "@aws-sdk/client-s3": "^3.600.0",
    "@aws-sdk/credential-providers": "^3.600.0",
    "@aws-sdk/lib-dynamodb": "^3.600.0",
    "@babel/types": "^7.24.7",
    "@expo/metro-runtime": "~3.2.1",
    "@react-native-async-storage/async-storage": "^1.23.1",
    "@react-native-community/netinfo": "^11.3.2",
    "expo": "~51.0.14",
    "expo-status-bar": "~1.12.1",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-native": "^0.74.2",
    "react-native-get-random-values": "^1.11.0",
    "react-native-safe-area-context": "^4.10.4",
    "react-native-url-polyfill": "^2.0.0",
    "react-native-web": "~0.19.10"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@types/react": "~18.2.45",
    "esbuild": "^0.21.5",
    "tsx": "^4.15.4",
    "typescript": "^5.4.5"
  },
  "private": true
}
hexinatgithub commented 3 months ago

Inject runtime solve the problem.

github-actions[bot] commented 2 months 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.