UUID in Node.js

Node.js UUID Generator

On the server there is no reason to hand-roll: node:crypto is built in and randomUUID() has been stable since Node 14.17.

Built in since 14.17No npm installv4 (v7 via package)

Generated locally with the Web Crypto API — nothing leaves your browser.

Generating UUIDs in Node.js

How to generate

  1. Press Generate UUID above to mint a value in this browser — nothing is sent anywhere.
  2. Copy one value with Copy, or set How many and use Copy all for a fixture list.
  3. Switch Version if your code needs v7 instead of the v4 default, and turn on Formatting to match your stack's conventions.

Use Cases

How Node.js UUID Generator Compares

randomUUID()Built in, v4, CSPRNG-backed
uuid v7()One dependency, time-ordered for index locality
Math.random()Not cryptographically secure — never use for IDs you trust

Code Examples

Node.js
import { randomUUID } from 'node:crypto';

const id = randomUUID(); // v4

// A batch for a seeder
const rows = Array.from({ length: 500 }, (_v, i) => ({
  id: randomUUID(),
  position: i,
}));
With uuid (v7)
import { v7 as uuidv7 } from 'uuid';

const id = uuidv7(); // time-ordered, sorts by creation
// '0198c2f1-6d8a-7cc3-9b21-4a7f0e5d3c11'

Frequently Asked Questions

Which Node version has crypto.randomUUID()?
It was added in Node 14.17 and 15.6, and is stable from Node 19. Anything currently in support has it.
Should I use randomUUID or Math.random for IDs?
Math.random() is not a CSPRNG and has a predictable internal state — never build an identifier people rely on from it. Use randomUUID(), or randomBytes() if you need a custom length.
How do I generate a UUID v7 in Node?
There is no built-in. Use the uuid package (import { v7 } from 'uuid') on the application side, or generate in PostgreSQL 18 with uuidv7() as a column default.

Related tools