Post

UUID in NodeJS

UUID (Universally Unique IDentifier)

crypto - Module was added from Node.js 14.17.0

1
2
const  crypto  =  require("crypto");
console.log(crypto.randomUUID());

uuid module:

1
2
3
const  uuid  =  require("uuid");
console.log(`Here is a test v1 uuid: ${uuid.v1()}`);
console.log(`Here is a test v4 uuid: ${uuid.v4()}`);

nanoid module:

1
2
3
4
5
6
7
const  Nanoid  =  require("nanoid");
const  NanoidAsync  =  require("nanoid/async");
console.log(`UUID with Nano ID sync: ${Nanoid.nanoid()}`);
(async  function  ()  {
const  nanoId  =  await  NanoidAsync.nanoid();
console.log(`UUID with Nano ID async: ${nanoId}`);
})();

👉 Demo

This post is licensed under CC BY 4.0 by the author.