Until recently, I achieved this easily by installing something like uuid on npm.
But we can do this with the built-in
Crypto web API.
crypto.randomUUID() in your code, and it will generate you a unique UUID.
// logs a new UUID each time you run the script console.log(crypto.randomUUID()) // Example output: '14ae278c-e736-49f5-aa3f-f921cc77bc64'
All your modern browsers and Node v19+ support it.
Try it out and get rid of another module from your bundle. 🙈
Read more about the
Crypto API here.
Happy coding! ✨
🚨 Want to make friends and learn from peers? You can join our free web developer community here. 🎉