How to Generate UUIDs in Node.js

Generate UUIDs in Node.js with crypto.randomUUID() for v4 and the uuid package for v7 when you need sortable identifiers.

Recommendation: use the built-in API for UUID v4 and add a package only when you need UUID v7 or a consistent cross-runtime interface.

How to generate UUID v4 in Node.js

import { randomUUID } from 'node:crypto';

const id = randomUUID();
console.log(id);

How to generate UUID v7 in Node.js

// npm install uuid
import { v7 as uuidv7 } from 'uuid';

const id = uuidv7();
console.log(id);

Native support notes

  • Node.js uses UUID naming consistently across community packages.
  • UUID v4 is native with the crypto module.
  • UUID v7 is still usually package-based.

Practical notes

  • Prefer node:crypto for v4 when you want zero dependency overhead.
  • Keep one package version across workers, APIs, and CLI tools when using UUID v7.
  • See UUID v7 for ordering and database trade-offs.

Frequently Asked Questions

Yes, it is the cleanest choice for UUID v4 in modern Node.js applications.

In most Node.js projects, UUID v7 still comes from a package such as uuid.

Learn more

These articles expand on related concepts, formats and practical considerations.

By using this site, you agree to our Privacy Policy and Terms of Service. You are not permitted to use the GUIDs (also known as UUIDs) generated by this site or use any other content, services and information available if you do not agree to these terms.
Disclaimer: All information is provided for general educational and technical reference only. While we aim to keep the content accurate, current and aligned with published standards, no guarantees are made regarding completeness, correctness or suitability for any specific use case.
GUID specifications, best practices, security guidance, database behavior and ecosystem conventions (including cloud platforms and identifier formats) may change over time or differ by implementation. Examples, recommendations and comparisons are illustrative and may not apply universally.
This content should not be considered legal, security, compliance or architectural advice. Before making critical design, security or production decisions, always consult the latest official standards and vendor-specific documentation.
Always evaluate behavior in your own environment.
Standards Compliance: The GUIDs generated by this site conform to RFC 4122 and RFC 9562 specifications whenever possible, using cryptographically secure random number generation.