Skip to content

Instantly share code, notes, and snippets.

Last active February 8, 2023 03:23
What would you like to do?
Nodejs generate UUID

UUID Collection

URL browser ASCII code ref


const Str = require('@supercharge/strings')

const random = Str.random()  
// 'zONHF73w_4M3cmv7GZpXG'

const random_WithFiftySymbols = Str.random(50)  
// 'iO3quoYg265hlzq30E8RelQc0LOKle4R0yk6CMbgeHgGNcm_mR'

node crypto

const Crypto = require('crypto')

function randomString(size = 21) {  
  return Crypto
    .slice(0, size)

// '/VuPgyBlk/aZjPADhMqQk'

Be careful when converting the random data to a base64 string because this string may contain characters like the forward-slash which could have side effects in your app.

Pro - native Node.js functionality - customizable the string length - generates a URL-friendly string when using .toString('hex')

Contra - no URL-friendly when using .toString('base64‘) because it may contain chars like a forward slash (/) - using .toString('hex') reduces the alphabet to numbers and the lowercase characters a-e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment