aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
3.04k stars 569 forks source link

Unable to init in an expo api route #5815

Open assertnotnull opened 6 months ago

assertnotnull commented 6 months ago

Checkboxes for prior research

Describe the bug

In an expo project initializing the dynamodb client gives an error

SDK version number

@aws-sdk/client-dynamodb@3.515.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

node 20, expo v50, react-native 0.73.2

Reproduction Steps

write an api route and call it from.

/app/api/messages+api.ts

import { DynamoDBClient, ListTablesCommand } from "@aws-sdk/client-dynamodb";
import { ExpoRequest, ExpoResponse } from "expo-router/server";

const client = new DynamoDBClient({
  apiVersion: "2012-08-10",
  region: "us-east-1",
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
  },
});

export async function GET(request: ExpoRequest) {
  console.log("GET /api/messages");
  console.log(client);
}

Observed Behavior

Running the project through expo start the client initialization gives an error:

TypeError: (0 , import_slurpFile.slurpFile) is not a function
    at /code/project/node_modules/@smithy/shared-ini-file-loader/dist-cjs/index.js:134:35

Expected Behavior

No error

Possible Solution

No response

Additional Information/Context

No response

RanVaknin commented 6 months ago

Hi @assertnotnull ,

Can you please update a small github repository with all the files needed to reproduce this behavior?

Thanks, Ran~

github-actions[bot] commented 5 months ago

This issue has not received a response in 1 week. If you still think there is a problem, please leave a comment to avoid the issue from automatically closing.

assertnotnull commented 5 months ago

Ok yeah I will do a small repo

assertnotnull commented 5 months ago

Repo is here https://github.com/assertnotnull/expo-aws-sdk-bug

jawndiego commented 2 months ago

@assertnotnull did you ever sort this out?

kuhe commented 1 month ago

In our CJS distribution for @aws-sdk/client-dynamodb, which is what we expect react-native to use for reasons originating in the metro bundler, we have the following platform specific files

/dist-cjs/runtimeConfig.js
/dist-cjs/runtimeConfig.native.js

When operating in react-native, the SDK relies on the behavior described here https://reactnative.dev/docs/platform-specific-code#native-specific-extensions-ie-sharing-code-with-nodejs-and-web that automatically loads the .native.js file instead of the regular file.

In the code sample, expo has not loaded the right file, and therefore the SDK is calling Node.js filesystem module APIs like readFile.

I don't know how to configure this on the expo/metro side, but the solution will involve loading the .native.js file.


One workaround I can think of is to create a react-native bundle containing the AWS SDK using only the metro bundler, and then loading that in the expo application instead of using expo itself to bundle.