uuid
uuid generates and validates RFC 9562 UUIDs in JavaScript. It covers every UUID version the spec defines: random v4, timestamp-based v1, name-hashed v3/v5, and the newer sortable v6/v7. It runs in Node, browsers, and React Native with the same import, ships zero dependencies, is tree-shakable, and includes helpers to parse, stringify, and validate UUIDs plus a small CLI. It sits at the bottom of dependency trees across most of the npm registry.
The default when you actually need spec-compliant UUIDs, especially v5 and v7. If all you ever call is v4, use the platform's crypto.randomUUID() and skip the dependency entirely.
Use it if
- You need time-sortable v7 UUIDs for database keys or name-based deterministic v5 UUIDs for idempotency, which crypto.randomUUID() does not cover
- You want validation, version detection, and byte-level parse/stringify helpers alongside generation, with one API across Node and browsers
- You are on an older Node or browser target where crypto.randomUUID() is not available but crypto.getRandomValues() is
- You only need random v4 IDs: crypto.randomUUID() is built into Node and every modern browser, so the dependency buys you nothing
- Your codebase still uses require(): uuid dropped CommonJS support in v12, so you either pin uuid@11 forever or migrate to ESM
- You want short, dense, URL-friendly IDs: a 36-character UUID is the wrong shape and nanoid produces smaller IDs with less overhead
Setup reality
npm install uuid and you are done on Node and modern bundlers, with zero config and zero dependencies. The catches: since v12 the package is ESM-only, so require('uuid') in a CommonJS project fails and you must pin uuid@11 or switch to import. React Native and Expo lack crypto.getRandomValues(), so you must install react-native-get-random-values and import it before uuid or every call throws. Fourteen major versions also means old Stack Overflow answers reference APIs that moved.
Patterns
Generate a random v4 UUIDgenerate-random-uuid
import { v4 as uuidv4 } from 'uuid';
const id = uuidv4();
// 'b18794e8-5d0d-417c-b361-ba38e78411b4'If v4 is all you use, crypto.randomUUID() does the same thing with no dependency.
Generate a time-sortable v7 UUIDgenerate-sortable-uuid
import { v7 as uuidv7 } from 'uuid';
const id = uuidv7();
// '01941f29-7c00-73e4-a310-744d2167fc5b'v7 IDs sort by creation time, which makes them better database primary keys than random v4.
Derive a deterministic v5 UUID from a namedeterministic-uuid
import { v5 as uuidv5 } from 'uuid';
const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
uuidv5('Hello, World!', MY_NAMESPACE);
// always '630eb68f-e0fa-5ecc-887a-7c7a62614681'
// RFC namespaces are built in:
uuidv5('https://www.w3.org/', uuidv5.URL);Same name plus namespace always yields the same UUID; the RFC recommends v5 over v3 unless you need MD5 compatibility.
Validate a UUID and check its versionvalidate-uuid
import { validate, version } from 'uuid';
function isV4(id) {
return validate(id) && version(id) === 4;
}
validate('not a UUID'); // falsevalidate() returns true for the NIL and MAX UUIDs too, so check version() when you need a specific one.
Convert between UUID strings and bytesuuid-to-bytes
import { parse, stringify } from 'uuid';
const bytes = parse('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b');
// Uint8Array(16)
const str = stringify(bytes);
// '6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'Byte order follows the left-to-right hex pairs of the string form; both functions throw TypeError on invalid input.
Produce predictable UUIDs in testsdeterministic-test-ids
import { v4 as uuidv4 } from 'uuid';
const id = uuidv4({
random: Uint8Array.of(
0x10, 0x91, 0x56, 0xbe, 0xc4, 0xfb, 0xc1, 0xea,
0x71, 0xb4, 0xef, 0xe1, 0x67, 0x1c, 0x58, 0x36
),
});
// '109156be-c4fb-41ea-b1b4-efe1671c5836'Passing options to v1/v6/v7 disables the internal uniqueness state, so keep this trick inside tests.
Make uuid work in React Native / Exporeact-native-setup
// index.js: this import MUST come first
import 'react-native-get-random-values';
import { v4 as uuidv4 } from 'uuid';
uuidv4();Without the polyfill imported before uuid (including via transitive deps), every call throws 'getRandomValues() not supported'.
Generate UUIDs from the command linecli-generate
npx uuid # v4 by default
npx uuid v7 # time-sortable
npx uuid v5 example.com DNSThe v5/v3 forms accept the literal strings URL or DNS as the namespace argument.
Convert between v1 and v6 UUIDsconvert-v1-v6
import { v1ToV6, v6ToV1 } from 'uuid';
v1ToV6('92f62d9e-22c4-11ef-97e9-325096b39f47');
// '1ef22c49-2f62-6d9e-97e9-325096b39f47'v6 is a reordered v1 that sorts chronologically; the conversion is lossless in both directions.
Use the NIL and MAX sentinel UUIDsnil-max-constants
import { NIL, MAX } from 'uuid';
NIL; // '00000000-0000-0000-0000-000000000000'
MAX; // 'ffffffff-ffff-ffff-ffff-ffffffffffff'version() returns 0 for NIL and 15 for MAX, not a normal 1-7 value.