Oliver Jumpertz

Secure UUIDs In Node

Category: Node.js

Share this snippet to:

const { randomUUID } = require("crypto");


You can use the function as follows:

const uuid = randomUUID();


You could theoretically also create UUIDs yourself. There is only one issue, however: UUIDs tend to collide when not created with the correct approach.

So, if you don’t want UUID collisions, you should rely on the crypto module to create UUIDs for you.


If you don’t care whether your UUIDs often collide, you can use the following code:

function randomUUID() {
const head = Date.now().toString(36);
const tail = Math.random().toString(36).substring(2);
return `${head}${tail}`;

Share this snippet to: