
Nano ID
A tiny JavaScript library that generates short, random, URL-friendly unique IDs.
- Stars
- 27.1k
- Forks
- 897
- Licence
- MIT
- Language
- JavaScript
- Last update
- 21 days ago
- Type
- Library
What it is
Nano ID is a small JavaScript library that creates random string identifiers, such as "V1StGXR8_Z5jdHi6B-myT", for records, objects or database entries. The README says it is about 117 bytes minified and brotlied, has no dependencies, and uses the URL-safe characters A-Za-z0-9_- so IDs can sit in web addresses. It uses a hardware-backed random source in Node.js and browsers, and the README describes it as a shorter alternative to UUID v4.
- Generate a 21-character ID for a new database record with nanoid()
- Create shorter 10-character IDs with nanoid(10) where collision risk is acceptable
- Build a custom alphabet ID generator with customAlphabet for codes or tokens
- Use customRandom with a seeded generator for reproducible test data
Great for
- JavaScript or TypeScript developers who need unique IDs for new records or objects
- Projects where short, URL-safe IDs are preferred over 36-character UUIDs
- Front-end or Node.js code that wants a dependency-free ID generator
- Teams that need custom alphabets or ID lengths via customAlphabet
Not ideal for
- Developers who need a standardized UUID format, since the README positions Nano ID as a UUID-like alternative rather than a UUID implementation
- Anyone generating React list keys on every render, which the README explicitly advises against
- Non-JavaScript projects, unless they use one of the ports; the README mentions over 20 languages but this entry does not verify them
How to get it
- 1
Install with a package manager
You'll need: Node.js or a modern browser environment
Install from npm
npm install nanoid - 2
Run it
- Install the package with npm install nanoid.
- Import the function with import { nanoid } from 'nanoid'.
- Call nanoid() to get a 21-character ID.
- Optionally pass a size, such as nanoid(10), and check collision risk with the author's calculator.
- 3
Get the most out of it
- Use customAlphabet to restrict characters to a set you need, and check the result with the linked collision calculator
- Use nanoid-dictionary for prebuilt alphabets, as the README suggests
- Use the non-secure import only where hardware random generators are unavailable and security is not a concern
- For React keys, prefer stable IDs from your data or the useId hook, as the README recommends
Downloads
Strengths
- Very small and dependency-free, with the README citing 117 bytes minified and brotlied
- Uses the platform's cryptographic random source and documents its uniformity approach
- Simple API: a single nanoid() call with optional size and alphabet
Trade-offs
- Non-secure variant is slower and seeded custom generators are not guaranteed to produce the same output across versions
- Shorter custom sizes increase collision probability, so users must check their chosen size
- Benchmark figures in the README are from the author's environment and may not reflect your own
How it compares
| Project | Stars | Type | Level | Updated | Best for |
|---|---|---|---|---|---|
| nanoid (this) | 27.1k | Library | beginner | 21 days ago | JavaScript or TypeScript developers who need unique IDs for new records or objects |
| uuid | 15.3k | Library | beginner | 4 days ago | JavaScript or TypeScript developers who need standard-format IDs |
Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.