Generate UUID in JavaScript & Node.js
Use the native crypto.randomUUID() API — zero dependencies, works in every modern browser and Node.js 14.17+.
Need a UUID right now? Generate one instantly in your browser.
Open UUID v4 Generator →The Quick Answer
Modern JavaScript doesn't need a library to generate UUIDs. The crypto.randomUUID() method is built into the browser and Node.js.
const id = crypto.randomUUID();
console.log(id);
// → "f47ac10b-58cc-4372-a567-0e02b2c3d479"
import { randomUUID } from 'crypto'; // ESM
// const { randomUUID } = require('crypto'); // CommonJS
const id = randomUUID();
console.log(id);
// → "f47ac10b-58cc-4372-a567-0e02b2c3d479"
Using the uuid npm Package
If you need UUID v1, v3, or v5, or need to support older browsers/Node versions, use the uuid package.
npm install uuid
import { v1 as uuidv1, v4 as uuidv4, v5 as uuidv5 } from 'uuid';
// v4 — random
const id = uuidv4();
// → "f47ac10b-58cc-4372-a567-0e02b2c3d479"
// v1 — timestamp-based
const tsId = uuidv1();
// v5 — name-based (deterministic)
const MY_NAMESPACE = uuidv4(); // generate once and store
const nameId = uuidv5('hello.world', MY_NAMESPACE);
React, Vue, and Framework Patterns
React — generate UUID for a key or ID
import { useId } from 'react'; // React 18+ — for accessibility IDs
// For data IDs (rows, items), generate once outside the component
// or on user action — not on every render.
const newItem = { id: crypto.randomUUID(), name: 'Widget' };
function ItemList({ items }) {
return (
<ul>
{items.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
}
TypeScript
// No import needed — crypto.randomUUID() is globally available
// Return type is `\`${string}-${string}-${string}-${string}-${string}\``
const id: string = crypto.randomUUID();
// For Node.js with TypeScript:
import { randomUUID } from 'crypto';
const nodeId: string = randomUUID();
Native vs npm — Which to Use?
| Approach | Bundle size | Browser support | UUID versions | Recommended when |
|---|---|---|---|---|
crypto.randomUUID() |
0 bytes | Chrome 92+, Firefox 95+, Safari 15.4+, Node 14.17+ | v4 only | You only need v4 and target modern runtimes |
uuid npm package |
~5 KB | All modern + IE 11 with polyfill | v1, v3, v4, v5 | You need v1/v3/v5 or must support older environments |
Frequently Asked Questions
Can I generate a UUID in JavaScript without any npm package?
Yes. All modern browsers and Node.js 14.17+ include crypto.randomUUID() natively. Call it with no arguments: const id = crypto.randomUUID(). No import or npm install needed in the browser. In Node.js use: import { randomUUID } from 'crypto'.
Is crypto.randomUUID() cryptographically secure?
Yes. Both the browser Web Crypto API and Node.js crypto module draw randomness from the operating system's CSPRNG — the same source used to generate TLS session keys. It is safe to use for security-sensitive identifiers.
What is the difference between crypto.randomUUID() and the uuid npm package?
crypto.randomUUID() is native to modern browsers and Node.js — zero dependencies, smallest possible footprint. The uuid npm package offers additional UUID versions (v1, v3, v5) and broader browser compatibility for older targets. For v4 specifically, prefer the native API.
Why shouldn't I generate UUIDs with Math.random()?
Math.random() is not cryptographically secure — its output is predictable if an attacker can observe enough values. UUID v4 requires 122 bits of secure randomness. Always use crypto.randomUUID() or crypto.getRandomValues() when generating identifiers.