Replacements for shortid
nanoid
nanoid
is a tiny, secure, URL‑friendly, unique string ID generator. It’s also faster than shortid
.
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.
Basic migration
ts
import shortid from 'shortid'
import { nanoid } from 'nanoid'
const id = shortid.generate()
const id = nanoid() // => "V1StGXR8_Z5jdHi6B-myT"
Control length
ts
// shortid produced ~7-14 chars; with nanoid you pick the size explicitly:
nanoid(10) // e.g., "NG3oYbq9qE"
Custom alphabet (replacement for shortid.characters
)
ts
shortid.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@')
import { customAlphabet } from 'nanoid'
const alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@'
const makeId = customAlphabet(alphabet, 12)
const id = makeId()