Node.js UUID Generator
Generate UUIDs for your Node.js applications. Create random UUIDs instantly, then use the code examples for native crypto.randomUUID(), the uuid npm package, and database integration. All generation is client-side.
How to generate UUIDs in Node.js
Native (Node.js 19+): const id = crypto.randomUUID() — no package needed, cryptographically secure UUID v4. With the uuid package: npm install uuid; const { v4: uuidv4 } = require('uuid'); const id = uuidv4(). For older Node.js: require('crypto').randomBytes(16) and manually format as UUID. In browsers and Deno, crypto.randomUUID() is also available globally. The native method is fastest and has zero dependencies.
// Node.js — generate UUIDs
import { randomUUID } from "crypto";
// Built-in (Node.js 14.17+) — UUID v4
const id = randomUUID();
console.log(id); // "f47ac10b-58cc-4372-a567-0e02b2c3d479"
// Browser — crypto.randomUUID()
const browserId = crypto.randomUUID();
// With uuid package (for v1, v3, v5)
import { v4, v5, validate } from "uuid";
console.log(v4()); // random UUID
console.log(v5("example.com", v5.DNS)); // deterministic
console.log(validate("not-a-uuid")); // falseUUID patterns in Node.js applications
Express route with UUID validation: app.get('/users/:id', (req, res) => { if (!/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(req.params.id)) return res.status(400).json({ error: 'Invalid UUID' }); }). Prisma: model User { id String @id @default(uuid()) }. Sequelize: { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4 } }. For MongoDB, ObjectId is typically preferred over UUID, but UUID works with Binary type for cross-database compatibility.
UUID vs other ID formats in Node.js
UUID v4 (36 chars): Universal standard, no coordination needed, but large for URLs and indexes. nanoid (npm install nanoid): Shorter IDs (21 chars default), URL-safe, cryptographically secure. CUID2 (npm install @paralleldrive/cuid2): Collision-resistant, sortable, secure. ULID: UUID-compatible but time-sortable, better for database indexes. For most Node.js APIs and databases, UUID v4 via crypto.randomUUID() is the safest default — universally understood and supported by every database.
Frequently Asked Questions
Should I use crypto.randomUUID() or the uuid npm package?
Use crypto.randomUUID() if you're on Node.js 19+ or targeting modern browsers/Deno — it's native, fast, and zero dependencies. Use the uuid package if you need UUID v1, v3, or v5, or need to support older Node.js versions.
How do I validate a UUID in Node.js?
Regex: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(id). With the uuid package: const { validate } = require('uuid'); validate(id) returns true/false. For strict v4 validation, check that the 13th character is '4' and the 17th is '8', '9', 'a', or 'b'.
Are UUIDs good for database primary keys in Node.js?
UUIDs work well but have trade-offs. Pros: globally unique, no coordination needed, safe to expose in APIs. Cons: larger than integers (16 bytes vs 4), random UUIDs fragment B-tree indexes. For PostgreSQL, use the native uuid type. Consider UUIDv7 or ULID for time-sortable IDs that reduce index fragmentation.
Related Generate Tools
Favicon Generator
Generate favicons from text or emoji — download PNGs, SVG, Apple Touch Icon, and HTML tags
Tailwind CSS Generator
Build Tailwind CSS utility classes visually with live preview and component presets
Privacy Policy Generator
Generate a customized privacy policy with GDPR, CCPA, cookies, analytics, and payment sections
JSON Mock Data Generator
Generate realistic fake JSON data for API testing with 30+ field types, preset templates, and schema builder