Skip to main content

How to Generate UUID in Node.js

Native crypto API & uuid package

How to Generate UUID in Node.js. Call crypto.randomUUID() to generate a Node.js UUID with zero dependencies. Before serializing to JSON, check the python uuid notes on calling str(), because the result is an object and not a plain string.

Generate UUID in Node.js

crypto.randomUUID()
550e8400-e29b-41d4-a716-446655440000

When you're writing server-side JavaScript, Node.js gives you a way to generate a UUID without installing anything — the crypto module's randomUUID() method returns a random, RFC 4122-compliant identifier in a single call. It's been part of Node's core since v14.17.0, so there's no dependency to audit or keep updated, just a built-in import. Below you'll find copy-paste ready code for generating a Node.js UUID in both CommonJS and ES Modules, validating a UUID string, and reaching for the uuid npm package when you need a version the built-in crypto module doesn't cover.

How to Generate a UUID in Node.js

The standard way to generate a UUID in Node.js is crypto.randomUUID(), which has shipped in Node's core since v14.17.0 (stable without a flag since v16.7.0) and needs no npm install. It returns a random, RFC 4122-compliant UUID as a plain string, ready to use in either module system. With Composer installed, a php uuid object built from random_bytes() implements UuidInterface and converts to text with toString().

commonjs.js
const { randomUUID } = require('crypto');

const id = randomUUID();
console.log(id);
// Output: 550e8400-e29b-41d4-a716-446655440000
esm.mjs
import { randomUUID } from 'node:crypto';

const id = randomUUID();
console.log(id);
// Output: 550e8400-e29b-41d4-a716-446655440000

Explanation

Convert a String to a UUID in Node.js

Node's crypto module has no dedicated UUID type or parser — once randomUUID() returns, a UUID is just a string. What you usually need instead is validation: confirming that a string from a request body, config file, or database row is actually a well-formed UUID before you trust it. On older Node versions a typescript uuid file imports randomUUID from node:crypto, with @types/node supplying the typings.

validate-regex.js
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;

function isValidUuid(value) {
  return UUID_REGEX.test(value);
}

console.log(isValidUuid('550e8400-e29b-41d4-a716-446655440000')); // true
console.log(isValidUuid('not-a-uuid'));                            // false
validate-uuid-package.js
import { validate, version } from 'uuid';

const id = '550e8400-e29b-41d4-a716-446655440000';

console.log(validate(id)); // true
console.log(version(id));  // 4

Explanation

Other Options for Generating UUIDs in Node.js

For any version besides v4, or on older Node runtimes where crypto.randomUUID() isn't available, the uuid npm package is the standard choice. Install it once and it covers generation and validation for v1, v3, v5, and v7 through simple named exports.

terminal
npm install uuid
uuid-versions.js
import { v4 as uuidv4, v7 as uuidv7 } from 'uuid';

// Version 4 - random (same algorithm as crypto.randomUUID())
const id4 = uuidv4();

// Version 7 - Unix timestamp + random, sortable and index-friendly
const id7 = uuidv7();

console.log(id4, id7);

Explanation

About the author

Vinitha Pu

Founder and designer

Vinitha Pu is a graphic designer based in Palakkad, Kerala, who builds free, privacy-friendly browser tools. She studied Textile and Apparel Design at Avinashilingam University and completed a C-Tech in Graphic Design at Image Creative Education in 2025. She designs, builds and maintains the UUID generators, validator, decoder and the language and database UUID guides on UUID Generator.

  • Content author: Vinitha Pu
  • Fact checked by: Vinitha Pu. Every generator is checked against the RFC 4122 and RFC 9562 layouts and every code example is run before it goes live.
  • Published Updated

Vinitha Pu on Behance · Vinitha Pu on LinkedIn

Comments & Feedback

Share your experience or ask questions about this tool

Copied!