| description | Modern, secure alternatives to the shortid package for generating URL‑friendly unique IDs |
|---|
nanoid is a tiny, secure, URL‑friendly, unique string ID generator. It’s also faster than shortid.
:::info Good to know before migration
-
shortid.isValid(id): there’s no direct equivalent. Validate with a regex that matches your chosen alphabet and length, e.g./^[A-Za-z0-9_-]{21}$/. -
shortid.seed()/shortid.worker(): not needed and not provided bynanoid(it uses a secure random source). Avoid seeded/deterministic IDs for security.
:::
import shortid from 'shortid' // [!code --]
import { nanoid } from 'nanoid' // [!code ++]
const id = shortid.generate() // [!code --]
const id = nanoid() // [!code ++] => "V1StGXR8_Z5jdHi6B-myT"// shortid produced ~7-14 chars; with nanoid you pick the size explicitly:
nanoid(10) // e.g., "NG3oYbq9qE"shortid.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@') // [!code --]
import { customAlphabet } from 'nanoid' // [!code ++]
const alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@' // [!code ++]
const makeId = customAlphabet(alphabet, 12) // [!code ++]
const id = makeId() // [!code ++]