nanoid
Tiny, secure, URL-friendly unique string ID generator — 130 bytes, no dependencies, cryptographically strong.
nanoid
General JSTiny, secure, URL-friendly unique string ID generator — 130 bytes, no dependencies, cryptographically strong.
Fit
Bundle (gzip)
517 B
853 B raw
Install size includes transitive dependencies. Bundle size is gzipped browser payload.
Freshness
The Problem
Math.random() is not cryptographically secure and UUID v4 generates long, hyphenated strings that are bulky in URLs and databases. You need short, secure, URL-safe IDs.
What It Does
Nanoid uses the crypto module (Node) or crypto.getRandomValues (browser) to generate cryptographically random IDs. The default 21-character alphabet (A-Za-z0-9_-) produces IDs with the same collision probability as UUID v4 at 1/3rd the length. Custom alphabets and sizes are supported.
Installation
npm install nanoidUsage Example
import { nanoid, customAlphabet } from 'nanoid';
// Default: 21 character URL-safe ID
const id = nanoid(); // 'V1StGXR8_Z5jdHi6B-myT'
// Custom size
const shortId = nanoid(10); // 'IRFa-VaY2b'
// Custom alphabet (e.g., digits only)
const numericId = customAlphabet('1234567890', 8);
numericId(); // '39124817'
// Use in database inserts
const user = await db.insert(users).values({ id: nanoid(), name: 'Alice' });Related packages
Unified TypeScript SDK for building AI-powered apps — switch between 20+ LLM providers without rewriting code.
animejsGeneral JSLightweight JavaScript animation library with a tiny footprint and a powerful, expressive API.
AOSGeneral JSAn easy-to-use library that makes elements fade, slide, or zoom into view as the user scrolls down your page.
archiverGeneral JSStreaming interface for generating ZIP and TAR archives in Node.js — create compressed archives programmatically from files, directories, and buffers.
@formkit/auto-animateGeneral JS> Add smooth transitions to any element with a single line — zero-config animations for adds, removes, and moves.