API Reference

uuid

RFC 9562 compliant UUID generation for JavaScript, from random v4 to time-ordered v7 and namespace-based v5 identifiers.

Official Documentation ↗
CompatibilityThis documentation covers version v14.0.3. Check the official docs for the latest API surface.

Installation

npmnpm install uuid
pnpmpnpm add uuid

Methods

RFC 9562 compliant UUID generation for JavaScript, from random v4 to time-ordered v7 and namespace-based v5 identifiers.

Overview

The uuid package implements the UUID standard for JavaScript across its practical variants. v4 remains the default random identifier, v7 prefixes randomness with a timestamp for index-friendly ordering, and v5 derives reproducible IDs from names in a namespace.

Every generator accepts a random or msecs override so tests can pin outputs, and validate, parse, and stringify round-trip between the dashed string and the 16 raw bytes. Version constants for the RFC namespaces are exported alongside.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • v4 — Generates a random UUID using crypto-grade randomness; the default when collision odds simply do not matter.
  • v7 — Generates a UUID whose leading bits encode a Unix timestamp, giving IDs that sort chronologically in indexes.
  • v5 — Derives a deterministic UUID from a name inside a namespace, so the same input always maps to the same identifier.
  • validate / parse — Checks the canonical 8-4-4-4-12 hex format and converts between string form and the 16 underlying bytes.

Usage Notes

  • Store UUIDs in a native UUID or 16-byte column where the database offers one; text columns inflate storage and slow comparisons.
  • Pass a fixed random array in tests to make generated IDs deterministic assertions.
  • Treat v7 ordering as a locality hint, not a clock: two IDs alone cannot establish happened-before semantics.

Common Pitfalls

  • v1 leaks the machine MAC address and clock value; avoid it in security-sensitive contexts.
  • Comparing UUID strings for ordering is meaningless for v4 and only chronological for v7 within the same timestamp source.
  • upgrading from old versions, the deep default import changed to named exports; CommonJS deep requires break.

FAQ

Should I use this package or crypto.randomUUID?
crypto.randomUUID in Node 19+ and modern browsers returns v4 without a dependency. Install the uuid package when you need other versions (v7, v5, v1), non-standard randomness injection, or byte-level parse and stringify utilities.
Why pick v7 over v4 for database keys?
v4 scatters randomly across the keyspace, which fragments B-tree index pages on inserts. v7 embeds a millisecond timestamp in the high bits so new keys append near the right edge, improving locality while remaining a standard UUID.
Are v5 UUIDs secret?
No. v5 is a SHA-1 derivation of a public namespace and name: anyone who knows both can reproduce the UUID. Use it for stable identity, never as a capability token or proof of knowledge.