In order to create a unique identifier, it's common to use a UUID (Universally Unique Identifier). This package generates a version 4 UUID compliant with RFC 4122 standards using pure JavaScript. The codebase for this project is inspired by the collective wisdom of the StackOverflow community.
UUIDs use the Web Crypto API by default. If secure randomness is unavailable, generation throws instead of silently falling back to predictable values. Passing false to uuid(false) opts into Math.random() and should only be used for non-security-sensitive identifiers.
Node.js 20 or later is required. In browsers, the Web Crypto API must be available.
Check out the working demo on CodeSandbox:
Install the package using npm or pnpm:
npm install @smakss/uuid
# or
pnpm add @smakss/uuidFor CommonJS modules, include it like this:
const { default: uuid } = require('@smakss/uuid');For ECMAScript modules, import it as follows:
import uuid from '@smakss/uuid';Then, you can use it within your application like this:
// Generate a new UUID using cryptographic randomness
const uniqueID = uuid();
console.log(uniqueID); // Outputs: '123e4567-e89b-12d3-a456-426614174000' (example output)NOTE: Each call to uuid() generates a new UUID. Secure randomness requires the Web Crypto API.
Interested in contributing to this project? Please see CONTRIBUTING.md for guidelines and details.
To ensure a welcoming and positive experience for all contributors and users, we adhere to a Code of Conduct. Your cooperation is appreciated.
This repository includes a Vite playground linked to the package through the pnpm workspace. Try the current local build without publishing a package version:
pnpm install
pnpm run dev:playgroundTo build the playground app, run this from the package root:
pnpm run build:playground