aws-sdk-js-v3
aws-sdk-js-v3 copied to clipboard
Unable to init in an expo api route
Checkboxes for prior research
- [X] I've gone through Developer Guide and API reference
- [X] I've checked AWS Forums and StackOverflow.
- [X] I've searched for previous similar issues and didn't find any solution.
Describe the bug
In an expo project initializing the dynamodb client gives an error
SDK version number
@aws-sdk/[email protected]
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
Hi @assertnotnull ,
Can you please update a small github repository with all the files needed to reproduce this behavior?
Thanks, Ran~
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.
Ok yeah I will do a small repo
Repo is here https://github.com/assertnotnull/expo-aws-sdk-bug
@assertnotnull did you ever sort this out?
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.