Skip to main content

How to Generate UUID in Node.js

Native crypto API & uuid package

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. See the javascript uuid page for UUID generation in React, Vue, Angular, and Express applications.

Generate UUID in Node.js

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

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. The python uuid generator page covers uuid4(), uuid5(), and all format conversions — str, bytes, int, and hex.

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. See the php uuid page for Symfony, Laravel, and Doctrine UUID primary key integration patterns.

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

Comments & Feedback

Share your experience or ask questions about this tool

Copied!