UUID v4 Generator

2.0.3 · active · verified Sun Apr 19

The `uuid4` package provides a lightweight and focused Node.js module specifically for generating and validating Version 4 UUIDs (Universally Unique Identifiers). As of its current stable version, 2.0.3, the library has shifted its compatibility focus to modern JavaScript environments, notably discontinuing support for legacy browsers that was available in its 1.x release line. This change means version 2.x and above are primarily designed for Node.js and modern browser environments that fully support ECMAScript Modules (ESM). While an explicit release cadence isn't detailed, the semantic versioning indicates a pattern of incremental updates and patch fixes. A key characteristic of `uuid4` is its simplicity for V4 generation. However, for users within the Deno ecosystem, the project itself recommends utilizing Deno's standard library UUID module for better native integration, highlighting a pragmatic approach to platform-specific best practices. The library offers a straightforward API for both creating new UUIDs and confirming the validity of existing ones against the RFC 4122 V4 specification.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to generate a new V4 UUID and validate both generated and arbitrary UUID strings.

import uuid4 from "uuid4";

// Generate a new Version 4 UUID. These UUIDs are statistically unique
// and widely used for various identifiers where collision avoidance
// is crucial, such as unique database keys, session tokens, or transaction IDs.
const newId = uuid4();
console.log("Generated UUID:", newId); // Example: 'a1b2c3d4-e5f6-4789-0abc-1234567890ab'

// Validate a UUID to ensure it conforms to the V4 specification (RFC 4122).
// The validation is case-insensitive, making it robust for inputs from different sources.
// It checks for the correct format pattern (e.g., 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx').
const isValid = uuid4.valid(newId);
console.log("Is the generated UUID valid?", isValid); // Should always be true for newly generated IDs

// Example of validating an invalid UUID string.
const invalidId = "not-a-proper-uuid-format";
const isInvalidValid = uuid4.valid(invalidId);
console.log("Is 'not-a-proper-uuid-format' valid?", isInvalidValid); // Will be false

const anotherInvalid = "01234567-89ab-cdef-0123-456789abcdef"; // Not V4 (3rd block starts with 'c')
console.log("Is '" + anotherInvalid + "' valid?", uuid4.valid(anotherInvalid)); // Will be false (not V4 spec)

view raw JSON →