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
- Import it explicitly from
node:cryptorather than reaching for the global: on Node the function lives in the module, and thenode:prefix makes it unambiguous that this is a built-in, not a dependency.randomUUID()returns a v4 value from the operating system's CSPRNG. - For time-ordered keys — the case where a million rows a day start fragmenting a B-tree index — Node has nothing built in. Add the
uuidpackage and call itsv7(), or let PostgreSQL 18 generate the value withuuidv7()on insert.
How to generate
- Press Generate UUID above to mint a value in this browser — nothing is sent anywhere.
- Copy one value with Copy, or set How many and use Copy all for a fixture list.
- Switch Version if your code needs v7 instead of the v4 default, and turn on Formatting to match your stack's conventions.
Use Cases
- Request and trace IDs — one per HTTP call, echoed in the response header.
- Idempotency keys for payment or webhook retries where the same logical request must be recognised twice.
- Seed scripts — build a batch here, then pipe it into a database seeder.
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
UUID v1 GeneratorUUID v3 GeneratorUUID v4 GeneratorUUID v5 GeneratorUUID v6 GeneratorUUID v7 GeneratorUUID v8 GeneratorBulk UUID GeneratorShort UUID GeneratorUUID ValidatorUUID FormatterJavaScript UUID GeneratorTypeScript UUID GeneratorNode.js UUID GeneratorReact UUID GeneratorPython UUID GeneratorJava UUID GeneratorC# UUID GeneratorGo UUID GeneratorPHP UUID GeneratorRuby UUID GeneratorRust UUID GeneratorKotlin UUID GeneratorSwift UUID GeneratorSQL UUID Generator