A simple UUID generator library for Node.js, NestJS, and Next.js
npm install uuid-corecrypto module with no external dependencies.
UUIDGenerator.v4().
crypto for cryptographically secure random values and SHA-1 for v5.
bash
npm install uuid-core
`
Requirements:
- Node.js >= 14.17.0 (for crypto.randomUUID support)
- TypeScript >= 5.4.5 (if using TypeScript)
Usage
$3
For ESM:
`javascript
import { UUIDGenerator } from "uuid-core";
`
For CommonJS:
`javascript
const { UUIDGenerator } = require("uuid-core");
`
$3
`javascript
// Generate a time-based UUID (v1)
console.log(UUIDGenerator.v1());
// Example output: "a1b2c3d4-1234-11ed-89ab-1234567890ab"
// Generate a random UUID (v4)
console.log(UUIDGenerator.v4());
// Example output: "123e4567-e89b-12d3-a456-426614174000"
// Generate a namespace-based UUID (v5)
const namespace = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
console.log(UUIDGenerator.v5("test", namespace));
// Example output: "b1c4a89e-4905-5e3c-b57f-dc92627d011e"
// Get a nil UUID
console.log(UUIDGenerator.empty());
// Output: "00000000-0000-0000-0000-000000000000"
// Validate a UUID
console.log(UUIDGenerator.validate("123e4567-e89b-12d3-a456-426614174000"));
// Output: true
`
API Reference
$3
Generates a time-based UUID (version 1) using the current timestamp, clock sequence, and a random node ID.
$3
Generates a random UUID (version 4) using Node.js's crypto.randomUUID.
$3
Generates a namespace-based UUID (version 5) using SHA-1 hashing of the provided name and namespace.
$3
Checks if a string is a valid UUID (versions 1-5) per RFC9562.
$3
Returns a nil UUID (00000000-0000-0000-0000-000000000000).
$3
- UUID: A string in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
- Namespace`: A UUID used as a namespace for v5 generation.