aws-sdk-js-v3 icon indicating copy to clipboard operation
aws-sdk-js-v3 copied to clipboard

Unable to init in an expo api route

Open assertnotnull opened this issue 1 year ago • 6 comments

Checkboxes for prior research

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

assertnotnull avatar Feb 20 '24 16:02 assertnotnull

Hi @assertnotnull ,

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

Thanks, Ran~

RanVaknin avatar Mar 19 '24 19:03 RanVaknin

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.

github-actions[bot] avatar Mar 30 '24 00:03 github-actions[bot]

Ok yeah I will do a small repo

assertnotnull avatar Mar 30 '24 19:03 assertnotnull

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

assertnotnull avatar Apr 02 '24 02:04 assertnotnull

@assertnotnull did you ever sort this out?

jawndiego avatar Jul 01 '24 19:07 jawndiego

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.

kuhe avatar Aug 06 '24 15:08 kuhe