Skip to content

Generate UUID in Node JS

There are multiple ways to generate random UUID in Node JS. The fastest possible way to create random 32-char string in Node is by using native crypto module. Let’s see one by one.

Universally Unique IDentifiers transcend many constraints of traditional incremental integer IDs, especially in distributed systems. In UUID version 4, we essentially generate a random 128-bit value.

Using native crypto module

const crypto = require('crypto');
const UUIDGeneratorNode = () =>
  ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
    (c ^ (crypto.randomBytes(1)[0] & (15 >> (c / 4)))).toString(16)


Using uuid-random package

uuid-random is an npm package for simpler and faster generation of RFC4122 UUIDS.

npm install uuid-random
var uuid = require('uuid-random');
uuid(); // '0b99b82f-62cf-4275-88b3-de039020f14e'

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.