JavaScript & Node.js · crypto.randomUUID() · Code Reference

Generate a UUID in JavaScript & Node.js

Complete reference for generating UUIDs in the browser and Node.js — covers the native crypto.randomUUID(), the uuid npm package, TypeScript types, and UUID v7 support.

Quick Start

Generate a Random UUID (v4) — No Library Needed

Modern browsers and Node.js 19+ ship a native crypto.randomUUID() function — no npm install required:

JavaScript (Browser & Node.js 19+)
// Works natively in browsers and Node.js 19+ const id = crypto.randomUUID(); console.log(id); // ‘550e8400-e29b-41d4-a716-446655440000’
💡

No import, no package.json entry

crypto.randomUUID() is part of the Web Crypto API and Node.js’s built-in crypto global. It always generates a version 4 (random) UUID and needs zero dependencies.

Node.js < 19 / Older Environments

Using the crypto Module in Older Node.js

If you’re on Node.js 14–18, use the crypto module’s randomUUID() method (added in Node 14.17):

Node.js 14.17+
const { randomUUID } = require(‘crypto’); // or: import { randomUUID } from ‘crypto’; const id = randomUUID(); console.log(id);

npm Package

The uuid npm Package (All Versions v1–v7)

For UUID versions beyond v4 — or to support older Node.js/browser targets — the uuid package is the industry standard, with 100M+ weekly downloads:

Terminal
npm install uuid
JavaScript / TypeScript
import { v4 as uuidv4, v7 as uuidv7, v5 as uuidv5 } from ‘uuid’; uuidv4(); // random UUID uuidv7(); // time-ordered UUID (recommended for DB keys) uuidv5(‘example.com’, uuidv5.DNS); // deterministic UUID
FunctionVersionUse Case
uuidv1()v1Timestamp + node ID. Legacy systems only.
uuidv4()v4Random. General purpose — same as crypto.randomUUID().
uuidv5(name, ns)v5Deterministic from a name + namespace. SHA-1 based.
uuidv7()v7Time-ordered. Recommended for database primary keys in 2026.

Validation

Validate a UUID String in JavaScript

Use a regex to check if a string is a properly formatted UUID:

JavaScript
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; function isValidUUID(str) { return UUID_REGEX.test(str); } isValidUUID(‘550e8400-e29b-41d4-a716-446655440000’); // true isValidUUID(‘not-a-uuid’); // false
With the uuid package
import { validate as uuidValidate, version as uuidVersion } from ‘uuid’; uuidValidate(‘550e8400-e29b-41d4-a716-446655440000’); // true uuidVersion(‘550e8400-e29b-41d4-a716-446655440000’); // 4

TypeScript

TypeScript Types for UUIDs

TypeScript has no built-in UUID type, but you can create a branded type for compile-time safety:

TypeScript
type UUID = `${string}-${string}-${string}-${string}-${string}`; const id: UUID = crypto.randomUUID(); // With zod for runtime validation: import { z } from ‘zod’; const schema = z.string().uuid(); schema.parse(id); // throws if invalid

Frameworks

UUIDs in Express, Prisma & MongoDB

Express.js middleware (request ID)
app.use((req, res, next) => { req.id = crypto.randomUUID(); next(); });
Prisma schema (UUID primary key)
model Order { id String @id @default(uuid()) createdAt DateTime @default(now()) }
MongoDB with native driver
const { randomUUID } = require(‘crypto’); await collection.insertOne({ _id: randomUUID(), name: ‘example’ });

FAQ

JavaScript UUID FAQ

Use crypto.randomUUID(), a built-in function available in all modern browsers and Node.js 14.17+. It requires no import or npm install and returns a cryptographically random UUID v4 as a string. This is the fastest, dependency-free way to generate a UUID in JavaScript.
Yes. crypto.randomUUID() has been available globally in Node.js since version 19, and as require(‘crypto’).randomUUID() since Node.js 14.17. For older Node.js versions or environments needing UUID v1, v5, or v7, use the uuid npm package instead.
The uuid package is the de facto standard, with over 100 million weekly downloads. It supports versions v1, v3, v4, v5, v6, and v7, plus validation and parsing utilities. For pure v4 generation in modern environments, the native crypto.randomUUID() eliminates the need for any package at all.
crypto.randomUUID() only generates v4. For v7 (time-ordered, recommended for database primary keys in 2026), install the uuid package and use uuidv7() from the v7 export. This produces a UUID with a Unix millisecond timestamp prefix, improving B-tree index performance over random v4 keys.

Need to Generate a UUID Right Now?

Skip the code — use our free browser-based generator for all 8 UUID versions.

Open UUID Generator