react-native-get-random-values icon indicating copy to clipboard operation
react-native-get-random-values copied to clipboard

SyntaxError: Cannot use import statement outside a module

Open a-eid opened this issue 4 years ago • 4 comments

seems that this lib causes the following error

import typeof AccessibilityInfo from './Libraries/Components/AccessibilityInfo/AccessibilityInfo';

Screen Shot 2020-10-23 at 11 33 15 AM

when commented out, the app works fine.

a-eid avatar Oct 23 '20 09:10 a-eid

it's probably worth noting that this happens when running detox as seen in the above screen.

a-eid avatar Oct 23 '20 12:10 a-eid

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

Joelasaur avatar Oct 26 '22 16:10 Joelasaur

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 avatar Oct 27 '22 14:10 Joelasaur

@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?

LinusU avatar Oct 31 '22 14:10 LinusU