react-native-get-random-values
react-native-get-random-values copied to clipboard
SyntaxError: Cannot use import statement outside a module
seems that this lib causes the following error
import typeof AccessibilityInfo from './Libraries/Components/AccessibilityInfo/AccessibilityInfo';
when commented out, the app works fine.
it's probably worth noting that this happens when running detox
as seen in the above screen.
I'm getting the exact same thing, which is supposedly used to fix a uuid issue here https://github.com/uuidjs/uuid#getrandomvalues-not-supported
I fixed this issue by creating a new file called GetRandomValues.ts
that looks like this:
export {};
declare let global: any;
global.crypto = {
getRandomValues: (array: any[]) =>
array.map(() => Math.floor(Math.random() * 256)),
};
Then in the original file where we are importing the uuid package:
import './GetRandomValues';
import { v4 as uuidv4 } from 'uuid';
Found the fix here https://github.com/uuidjs/uuid/issues/416#issuecomment-635844939
This doesn't solve the original issue with this library, but it's a workaround so that I'm not forced to encounter this bug when doing what's suggested in the uuid readme here https://github.com/uuidjs/uuid#getrandomvalues-not-supported
@Joelasaur I would really not recommend using Math.random()
to populate the values in getRandomValues
since it's not cryptographically secure.
@a-eid The stack trace points to inside react-native
, and not this module. Could you try to import something from react-native
in your test, and not import react-native-get-random-values
to see if this is an issue in this module, or an issue in React Native or Jest?