Generate random UUID in Node.JS
Quick and easy thanks to crypto
library.
In a new .js
file paste the following code:
const crypto = require('crypto');
console.log(crypto.randomUUID());
console.log(crypto.randomUUID());
console.log(crypto.randomUUID());
console.log(crypto.randomUUID());
console.log(crypto.randomUUID());
console.log(crypto.randomUUID());
console.log(crypto.randomUUID());
console.log(crypto.randomUUID());
console.log(crypto.randomUUID());
console.log(crypto.randomUUID());
console.log(crypto.randomUUID());
console.log(crypto.randomUUID());
console.log(crypto.randomUUID());
console.log(crypto.randomUUID());
console.log(crypto.randomUUID());
console.log(crypto.randomUUID());
Run the program. Your output may look similar to this:
[terminator@skynet njs-uuid]$ node crypto-random-uuid.js
eac2cabb-9c94-41d9-9d8d-c8e86e3e0708
6a9a57a4-cb6f-4ad1-a9fe-c418940ca48e
51defe5c-f3eb-4f78-8ff0-10ec894f8f8f
21a29f7b-1554-464d-aec6-bcaabb38f347
062c2cc7-2732-4568-a9a7-1f0b6cf3c3e5
77e2d0ba-76e6-415b-9025-0a16d18b652c
f43d98bd-13fa-46b5-9be9-693570108c6e
8315f9c3-1ff2-43c1-9b47-d24a042acc65
e53fd407-debb-4d0a-8d98-e04874cea681
3d329654-167d-4601-acce-8c569874eaa9
129425ae-0a31-4f66-8f71-c9b5d606d867
466a034a-763e-412b-a41c-7f47a0b5c1ef
58146300-44e8-41ed-a244-69b090c3692d
5ed60bcb-5600-484a-8485-0cdf77859454
9c920afb-6e08-4518-a592-6851757c675f
6047f729-0bf8-41a3-8dd1-56acd56b1334
You can also do that from the console as well in one line:
console.log(require('crypto').randomUUID());
Here's an example:
[terminator@skynet ~]$ node
Welcome to Node.js v16.14.2.
Type ".help" for more information.
> console.log(require('crypto').randomUUID());
f5bb485e-3915-44bd-9a4a-e59961291d89
undefined