aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
3.08k stars 575 forks source link

[TypeError: Cannot read property 'getRandomBase64String' of undefined] ERROR #4680

Closed alyiev closed 2 months ago

alyiev commented 1 year ago

Checkboxes for prior research

Describe the bug

Using "expo": "~48.0.15".

Getting this error:

[TypeError: Cannot read property 'getRandomBase64String' of undefined] ERROR

SDK version number

@aws-sdk/client-location ^3.321.1

Which JavaScript Runtime is this issue in?

React Native

Details of the browser/Node.js/ReactNative version

node: 16.20.0

Reproduction Steps

const locationClient = new LocationClient({
  credentials,
  region: 'ap-south-1',
});

const params = {
  IndexName: 'placesAPI',
  Position: [-122.33, 47.6],
};

const command = new SearchPlaceIndexForPositionCommand(params);

const data = await locationClient.send(command);

Observed Behavior

When I'm trying to send a command, getting the error.

Connected these libs:

import 'react-native-get-random-values'; import 'react-native-url-polyfill/auto'; import 'url-polyfill';

Didn't help

Expected Behavior

Returned the location object from the service.

Possible Solution

No response

Additional Information/Context

No response

RanVaknin commented 1 year ago

Hi @fruity4pie ,

Since you are using expo and react, can you please include a github repo with the necassary steps needed to run your code and observe the error youre running into?

Thanks, Ran~

alyiev commented 1 year ago

@RanVaknin just do npx create-expo-app --template install aws-location ctrl+c -> ctrl+v the code above -> check result.

phatmann commented 1 year ago

I ran into this bug and I solved it by switching to the JSC engine for iOS:

{
  "expo": {
    "jsEngine": "hermes",
    "ios": {
      "jsEngine": "jsc"
    }
  }
}
alyiev commented 1 year ago

@phatmann this isn't solution, because hermes works faster and produces less code. I guess jsc will be deprecated in the next expo versions.

The main question how to solve it in hermes? 😄

phatmann commented 1 year ago

@fruity4pie For me using Hermes is not important, and I figure there are other people in the same situation as me that just want to get the builds working again. That is why I posted the workaround. This said, I want to use Hermes if I can, so I hope someone can provide a solution!

phatmann commented 1 year ago

Here is a related issue.

LinusU commented 1 year ago

For reference, this is the code that react-native-get-random-values runs:

global.ExpoModules.ExpoRandom.getRandomBase64String(10)

And here is an Expo maintainer confirming that it works in a clean Expo project:

https://github.com/expo/expo/issues/17270#issuecomment-1498774816

So something more is needed to trigger this, maybe the Hermes thing 🤔

I think that the best way to solve this would be to provide a minimum viable test case which just calls global.ExpoModules.ExpoRandom.getRandomBase64String and submit that to the Expo team.

alyiev commented 1 year ago

It is very strange.

I did clean install and was able to run aws. But in my prev setup was expo-router. Maybe it breaks smth. Gonna check tomorrow clean setup + expo-router.

Also noticed that in expo 49 expo-random will be deprecated.

phatmann commented 1 year ago

For me the problem is that the segment module includes uuid.js which imports react-native-get-random-values. The segment module lists react-native-get-random-values as a peer dependency, but I had never installed it. Perhaps another module was depending on it so I did not notice. Once I added react-native-get-random-values as dependency to my project, then I get the "getRandomBase64String" error.

berlinbra commented 1 year ago

For me the problem is that the segment module includes uuid.js which imports react-native-get-random-values. The segment module lists react-native-get-random-values as a peer dependency, but I had never installed it. Perhaps another module was depending on it so I did not notice. Once I added react-native-get-random-values as dependency to my project, then I get the "getRandomBase64String" error.

Doing this backwards fixed the issue for me; removing the dependency from the project and letting the peer dependency handle it. I was experiencing this error through the react-native-gifted-chat library which uses uuid -> react-native-get-random-values. Oddly, I had this dependency manually added for a while and didn't see any issues until recently, but removing it has resolved the issue

phatmann commented 1 year ago

If I remove the dependency, then uuid.js in the segment module fails to build. Glad it worked for you!

aleqsio commented 1 year ago

Yeah, If someone can capture the reproduction in which this call global.ExpoModules.ExpoRandom.getRandomBase64String(10) fails I'd be happy to take a look.

In general though I'd suggest to use expo-crypto as this provides new fast cryptographic functions using new Expo modules API and typed arrays without any conversion in JSland :)

There's a web polyfill as well: https://github.com/expo/expo/tree/main/packages/expo-standard-web-crypto

@LinusU Perhaps you'd be up to moving to the expo-crypto package in https://github.com/LinusU/react-native-get-random-values as well?

RanVaknin commented 1 year ago

Based on the discussion in this thread, this doesn't seem related to the SDK. The AWS SDK cannot guarantee compatibility with all 3rd party tools such as Hermes.

Unless you can provide a minimal repo that can consistently raise this issue (ideally a project with only the absolute necessary imports) , I think that this question is more appropriate to be directed at the Expo / Hermes maintainers.

Thanks, Ran~

phatmann commented 1 year ago

I 100% agree @RanVaknin. I am not even using AWS SDK, but I ran into this issue and cannot find it raised anywhere else. It is specific to my Mac dev environment, as other developers from my team don't seem to be running into it! Furthermore, it happens on iOS and Android builds.

LinusU commented 1 year ago

@LinusU Perhaps you'd be up to moving to the expo-crypto package in https://github.com/LinusU/react-native-get-random-values as well?

@aleqsio Will do, would you mind reviewing this for me? https://github.com/LinusU/react-native-get-random-values/pull/45 🙏

phatmann commented 1 year ago

@LinusU the fix works! Thanks so much!

nickdep217 commented 10 months ago

The original error I got was: "TypeError: Cannot read property 'getRandomBase64String' of undefined, js engine: hermes ERROR Invariant Violation: "main" has not been registered. This can happen if: Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project. A module failed to load due to an error and AppRegistry.registerComponent wasn't called., js engine: hermes"

I was running a development build from expo. I was installing segment analytics.

I ran the following two commands which fixed my problems:

Found this at: https://docs.statsig.com/client/reactNativeExpoSDK

webjay commented 10 months ago

My fix was:

npx expo install react-native-get-random-values

In src/index.js:

import 'react-native-get-random-values';
import 'expo-router/entry';

In package.json: "main": "src/index.js"

RanVaknin commented 2 months ago

Since the issue is not related to the SDK, and there's a workaround posted here, I'm going to go ahead and close this.

Thanks, Ran~

github-actions[bot] commented 1 month 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.