Skip to main content

How to Generate a UUID in Node.js with crypto.randomUUID

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

crypto.randomUUID() returns a new random UUID every time you call it, a 36-character ID such as 18e93793-d3e4-4beb-8724-35f4a0532080. It's built into Node.js, so it needs no package and no import.

Syntax​

Syntax
const <id> = crypto.randomUUID();
PartWhat it does
cryptoNode.js's built-in Web Crypto object. It's available in every file without an import.
randomUUID()Makes a new random UUID and returns it as a string.
<id>The variable that holds the UUID.

The Node.js documentation for crypto.randomUUID(): it "Generates a random RFC 4122 version 4 UUID. The UUID is generated using a cryptographic pseudorandom number generator."

The Example​

The example makes a UUID, prints it and its length, and then makes a second one to show that each call returns a different value.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which stores crypto.randomUUID() in id, logs the UUID and its length, then logs a second crypto.randomUUID().

main.js
const id = crypto.randomUUID();
console.log(id);
console.log(id.length);
console.log(crypto.randomUUID());

Step 2: Run It​

Terminal
node main.js
Output
18e93793-d3e4-4beb-8724-35f4a0532080
36
e0e405b8-aeb4-4770-8a2b-df542cb9be00

A zsh terminal where node main.js prints a UUID, the number 36, and a second, different UUID.

The command is the same in zsh, bash and PowerShell. Your UUIDs will differ, because they're random.

What Happened​

  • Each call made a new UUID. The two values share nothing, and there's no counter or clock behind them: version 4 UUIDs are random.
  • The UUID is 36 characters long: 32 hexadecimal digits in five groups, joined by four dashes.
  • The third group starts with a 4, in both UUIDs. That digit is the UUID's version, and version 4 means random.

A version 4 UUID carries 122 random bits, as RFC 9562 defines it, which makes a repeated value practically impossible. That's why UUIDs work well as IDs that many machines create on their own without asking each other, such as the jti claim of a JSON Web Token.

Common Mistakes​

MistakeWhat happensDo this instead
Calling randomUUID() on its ownReferenceError: randomUUID is not definedCall it on crypto: crypto.randomUUID()
Building an ID from Math.random()MDN warns that it "does not provide cryptographically secure random numbers"Use crypto.randomUUID(), which uses a cryptographic random number generator
Expecting UUIDs to sort by creation timeVersion 4 UUIDs are random, so their order means nothingStore a timestamp next to the ID, or use a version 7 UUID

Troubleshooting​

SymptomLikely causeFix
ReferenceError: randomUUID is not definedThe call is missing crypto.Write crypto.randomUUID()
TypeError: crypto.randomUUIDv7 is not a functionrandomUUIDv7 isn't on the global crypto objectImport it from node:crypto, as the FAQ shows

FAQ​

Do I need a package to make UUIDs in Node.js?​

No. randomUUID() has been part of Node.js's crypto module since version 14.17, and the global crypto object that holds it has been available without an import or a flag since version 19.

Can Node.js make a version 7 UUID?​

Yes, since Node.js 26.1.0, but only from the node:crypto module: randomUUIDv7 isn't on the global crypto object, and calling it there fails with TypeError: crypto.randomUUIDv7 is not a function. After import { randomUUIDv7 } from 'node:crypto';, calling randomUUIDv7() returned 01a22d82-a48d-78cf-b858-5fe6bddfa53e. A version 7 UUID starts with the current time, so newer IDs sort after older ones.

Is a UUID secret?​

No. It's unguessable, because it's random, but it isn't a password: anyone who sees it can copy it. Use it to identify something, not to prove who someone is.

Conclusion​

crypto.randomUUID() gives you a new random version 4 UUID on every call, with no package and no import. Use it wherever you need an ID that's practically never repeated.

Additional Resources​