Tiny and Fast Unique ID Generator
The `uid` package provides a highly optimized and compact utility for generating fixed-length random unique IDs in JavaScript environments, supporting both Node.js and browsers. The current stable version is 2.0.2, with releases focusing on performance, bug fixes, and TypeScript compatibility, indicating an active maintenance and development cadence. Key differentiators include its extremely small footprint (130B to 205B gzipped) and high performance, with benchmarks showing it to be significantly faster than alternatives like `nanoid` in its default non-secure mode. It offers three distinct modes: `uid` (fast, `Math.random`-based, non-secure), `uid/secure` (cryptographically secure via `crypto.getRandomValues`), and `uid/single` (non-secure, no internal cache, ideal for short-lived environments). This flexibility allows developers to choose the appropriate balance of speed, security, and bundle size for their specific use case.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'length')
cause Attempting to call `uid()` directly after an incorrect `import uid from 'uid';` statement in v2.0.0+ ESM environments, where `uid` is `undefined` because it's no longer the default export.fixChange the import statement to `import { uid } from 'uid';`. -
TypeError: uid is not a function
cause In CommonJS environments, attempting to call `require('uid')()` directly after the v2.0.0 breaking change. `require('uid')` now returns an object `{ uid: Function }`, not the function itself.fixDestructure the `uid` function: `const { uid } = require('uid');`.
Warnings
- breaking The `uid` package changed its default export to a named export in v2.0.0. Code relying on `import uid from 'uid';` will break.
- gotcha The default `uid` export and `uid/single` mode rely on `Math.random` and are NOT cryptographically secure. They should not be used for security-sensitive applications where unpredictability is paramount.
- gotcha The `uid/secure` mode requires the environment's `crypto` module (specifically `crypto.getRandomValues`). In older or constrained environments without this API, `uid/secure` will not function correctly.
- gotcha While `uid` aims for uniqueness, the risk of collisions increases with shorter lengths and a higher number of generated IDs. The default length of 11 uses a hexadecimal alphabet.
Install
-
npm install uid -
yarn add uid -
pnpm add uid
Imports
- uid
import uid from 'uid';
import { uid } from 'uid'; - uid (secure)
import uidSecure from 'uid/secure';
import { uid } from 'uid/secure'; - uid (single)
const { uid } = require('uid/single');import { uid } from 'uid/single'; - CommonJS require (secure)
const uidSecure = require('uid/secure').default;const { uid } = require('uid/secure');
Quickstart
import { uid } from 'uid';
import { uid as secureUid } from 'uid/secure';
import { uid as singleUid } from 'uid/single';
// Generate a default non-secure ID (length 11, hexadecimal alphabet)
const id1 = uid();
console.log(`Default non-secure ID: ${id1}`); // e.g., 'c92054d1dd6'
// Generate a non-secure ID with custom length
const id2 = uid(16);
console.log(`Non-secure ID (length 16): ${id2}`); // e.g., '8234dbf9a7dcec3b'
// Generate a cryptographically secure ID (length 11, hexadecimal alphabet)
// Requires crypto.getRandomValues() support (Node.js & modern browsers)
const secureId = secureUid();
console.log(`Secure ID: ${secureId}`); // e.g., 'f8a4b6c1e9d0a2b'
// Generate a single-use ID (length 11, alphanumeric alphabet)
// Does not maintain an internal buffer, good for ephemeral contexts
const singleUseId = singleUid();
console.log(`Single-use ID: ${singleUseId}`); // e.g., 'aG5jK7lP2qR'
// Example of generating multiple IDs within a loop
for (let i = 0; i < 3; i++) {
console.log(`Loop ID ${i}: ${uid(8)}`);
}