nanoid
nanoid is a tiny, secure, URL-friendly unique string ID generator that produces compact identifiers significantly shorter than UUIDs while maintaining excellent…
Installation
npm install nanoid
yarn add nanoid
pnpm add nanoid
Import
import { nanoid } from 'nanoid';Quick Example
import { nanoid, customAlphabet } from 'nanoid';
const id = nanoid(); // 'V1StGXR8_Z5jdHi6B-myT'
const shortId = nanoid(10); // 'IRFa-VaY2b'
const numericId = customAlphabet('0123456789', 8);
console.log(numericId()); // '48239127'About nanoid
nanoid is a tiny, secure, URL-friendly unique string ID generator that produces compact identifiers significantly shorter than UUIDs while maintaining excellent collision resistance. At just 130 bytes minified, nanoid generates 21-character IDs by default using a cryptographically strong random number generator (crypto.getRandomValues in browsers, crypto.randomBytes in Node.js). The default alphabet uses URL-safe characters (A-Za-z0-9_-), making nanoid IDs safe for use in URLs, HTML attributes, CSS selectors, and filenames without encoding. The collision probability with 21-character IDs requires generating over one billion IDs per second for 149 years to have a 1% probability of a single collision. nanoid supports custom alphabets and ID lengths through the customAlphabet function, allowing generation of numeric-only IDs, hex strings, or IDs from any character set. The library provides both synchronous and asynchronous APIs and has no dependencies. nanoid has become the default ID generator for many modern JavaScript tools and frameworks — it is used internally by React for generating unique keys, by PostCSS for generating CSS module class names, and by numerous databases and applications that need compact unique identifiers.
Quick Facts
| Package | nanoid |
| Category | Utility |
| Weekly Downloads | 40M+ |
| License | MIT |
| Install | npm install nanoid |
Related Packages
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.