Categories
Node.js Quick Tips Quick Tips

Generate v4 UUIDs without needing the uuid library

< 1 min read

This blog post is part of the series What’s new in Node.js core?

If you want to generate v4 UUIDs in Node.js e.g. d2c14405-a3dd-4fcc-b13b-bc649fe1a0bf, you no longer need to add the uuid library as a dependency. You can use crypto.randomUUID() instead.

Example

const crypto = require('crypto');

const itemId = crypto.randomUUID();

console.log(itemId);
// 'c11903dc-181d-4cce-996f-5f5ebb4034f3'

Support

Support in Node.js versions:

  • Added: v15.6.0
  • Backported to: 14.x (v14.17.0)
  • Also available in Chrome + Edge browsers

Related links

Leave a Reply

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