Registry / serialization / react-native-uuid

react-native-uuid

JSON →
library2.0.4jsnpmunverified

react-native-uuid is a robust, zero-dependency TypeScript implementation of the RFC4122 standard for generating Universally Unique Identifiers (UUIDs). It is specifically designed and optimized for React Native environments, ensuring compatibility across various JavaScript runtimes, including Node.js and browsers. The library is currently stable at version 2.0.4, with a development focus on performance, security, and standards compliance. A significant rewrite in version 2.0.0 introduced a pure TypeScript codebase and eliminated external dependencies. Recent updates, such as v2.0.4, have focused on improving the random number generation (RNG) by ensuring compatibility with `crypto.getRandomValues()` API, enhancing security and randomness quality. The package includes comprehensive test suites and continuous integration benchmarks for performance and security metrics, distinguishing it from libraries that might rely on less secure `Math.random` implementations for entropy.

npm install react-native-uuid
INSTALL
IMPORT
SIG · REACT-NATIVE-UUID
R
react-native-uuid
serializationjavascriptv2.0.4
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

uuid (default export object)
import uuid from 'react-native-uuid';
import { uuid } from 'react-native-uuid';
The default export is an object containing methods like `v1`, `v3`, `v4`, `v5`, `parse`, `stringify`, etc. Use `uuid.v4()` to generate a version 4 UUID. Since v2.0.1, default exports were explicitly fixed.
v4 (named export)
import { v4 } from 'react-native-uuid';
import uuidV4 from 'react-native-uuid';
Individual UUID version generation functions like `v4` are also available as named exports. This is often preferred for tree-shaking.
CommonJS require
const uuid = require('react-native-uuid');
const { v4 } = require('react-native-uuid');
For CommonJS environments, `require` returns the default export object. Access specific UUID generation methods via properties, e.g., `uuid.v4()`. While some libraries allow destructuring named exports from `require`, `react-native-uuid` primarily exposes its API via the default object for CJS.

Demonstrates generating different UUID versions (v4 and v1) using both default and named imports within a React Native functional component, showing how to update state with new UUIDs.

import uuid, { v4, v1 } from 'react-native-uuid'; import React from 'react'; import { View, Text, Button, StyleSheet } from 'react-native'; const App = () => { const [generatedUuids, setGeneratedUuids] = React.useState<string[]>([]); const generateNewUuids = () => { const newV4Uuid = v4() as string; // Using named export for v4 const newV1Uuid = uuid.v1() as string; // Using default export for v1 const anotherV4Uuid = uuid.v4() as string; // Using default export for v4 setGeneratedUuids(prev => [ `v4 (named): ${newV4Uuid}`, `v1 (default): ${newV1Uuid}`, `v4 (default): ${anotherV4Uuid}`, ...prev.slice(0, 4) // Keep only the latest 5 UUIDs ]); }; return ( <View style={styles.container}> <Text style={styles.title}>Generated UUIDs:</Text> { generatedUuids.length === 0 ? <Text style={styles.noUuids}>Press the button to generate UUIDs.</Text> : generatedUuids.map((id, index) => ( <Text key={index} style={styles.uuidText}>{id}</Text> )) } <Button title="Generate New UUIDs" onPress={generateNewUuids} /> </View> ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', padding: 20, backgroundColor: '#f0f0f0' }, title: { fontSize: 20, fontWeight: 'bold', marginBottom: 10 }, uuidText: { fontSize: 14, marginBottom: 5, fontFamily: 'monospace' }, noUuids: { fontSize: 16, color: '#888', marginBottom: 20 } }); export default App;
Debug
Known issues
breakingVersion 2.0.0 introduced a complete rewrite of react-native-uuid, transitioning to a zero-dependency TypeScript codebase. This major version change likely broke API compatibility with version 1.x, requiring code adjustments for migration.
fix
Review migration guides or package documentation when upgrading from 1.x. Specific fixes for default exports were provided in v2.0.1, so ensure your import statements are aligned with the new API surface.
affects: >=2.0.0 <2.0.1
breakingFollowing the v2.0.0 rewrite, version 2.0.1 specifically addressed and fixed issues related to default exports. Code relying on initial v2.0.0 default export behavior might have broken when upgrading to 2.0.1.
fix
Ensure your import statements correctly handle the default export, which is an object containing UUID generation methods (e.g., `import uuid from 'react-native-uuid'; uuid.v4();`). Review `ARCHITECTURE.md` for correct import patterns.
affects: >=2.0.0 <2.0.1
gotchaPrior to version 2.0.4, `react-native-uuid` primarily relied on `Math.random` for its pseudo-random number generation. While v2.0.4 fixed RNG to use `Uint8Array` for `crypto.getRandomValues()` API compatibility, older React Native environments (especially with Hermes) or older versions of this library might still default to `Math.random` if `crypto.getRandomValues` is unavailable or not polyfilled. `Math.random` is generally not considered cryptographically secure.
fix
Upgrade to `react-native-uuid` version 2.0.4 or newer. For environments where `crypto.getRandomValues()` is still unsupported (e.g., specific React Native versions or debugging with Chrome), consider installing a polyfill like `react-native-get-random-values` and importing it at your app's entry point (`import 'react-native-get-random-values';`).
affects: <2.0.4
Errors
Common errors & fixes
TypeError: uuid.v4 is not a function
Incorrect import: attempting to destructure `uuid` as a named export when it's the name of the default export object, or calling the default import as a function directly.
fix
Use `import uuid from 'react-native-uuid';` and then call `uuid.v4();` OR use the named import `import { v4 } from 'react-native-uuid';` and then call `v4();`.
Error: crypto.getRandomValues() not supported
The JavaScript runtime (e.g., React Native with Hermes, or older browser environments) does not natively provide the `crypto.getRandomValues()` API, which `react-native-uuid` (especially since v2.0.4) attempts to use for stronger randomness.
fix
Ensure you are on `react-native-uuid` version 2.0.4 or newer. If the error persists, install `react-native-get-random-values` (`npm install react-native-get-random-values`) and add `import 'react-native-get-random-values';` at the very top of your application's entry file (e.g., `index.js` or `App.js`) to polyfill the API.
Cannot find module 'react-native-uuid'
The package has not been correctly installed or dependencies are not linked/resolved in your project.
fix
Run `npm install react-native-uuid` or `yarn add react-native-uuid`. If using React Native, ensure your caches are clear (`npx react-native start --reset-cache`) and rebuild your project (`npx react-native run-ios` / `npx react-native run-android`).
Upgrade
Version history
2.0.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
react-native-uuid — npm install react-native-uuid · libregistry