
nanoid
A tiny (118 bytes), secure URL-friendly unique string ID generator
About
A tiny (118 bytes), secure URL-friendly unique string ID generator
What detecting nanoid tells you about a site
nanoid reveals a preference for tiny, URL-safe unique IDs over heavier UUIDs — chosen for shorter identifiers and a smaller footprint. Its presence often marks a performance-conscious app generating IDs for keys, share links or client-side records.
Why the exact nanoid version matters
nanoid moved to ESM-only in v4, which affects how it is bundled; the exact version tells you which module format the build consumes.
nanoid in a real-world stack
When you find nanoid in a bundle, it rarely travels alone. Frequently bundled transitively (many tools depend on it); otherwise alongside a state or routing layer that needs short IDs.
Quick facts
npm install nanoidHow Sourcemap Explorer detects nanoid
nanoid ships as v6.0.1, published 2026-08-03 and carries 0 direct dependencies, 131 versions on the registry. Those exact numbers are the footprint Sourcemap Explorer matches when nanoid rides inside a deployed bundle — here is how the detection works.
We catch nanoid from two complementary signals: bundled source paths and the embedded package.json. Modern bundlers (webpack, Vite, esbuild, Rollup, Turbopack) preserve the original node_modules/nanoid/ paths inside the JavaScript sourcemap's sources[] array — that's the canonical signal. When the matching package.json is also captured in sourcesContent[], we read the exact version field — patch number included. No regex guessing, no version inference.
- 1
Confirm the site exposes sourcemaps
In DevTools Network, check the response headers of any application script for `SourceMap` or `X-SourceMap`. Failing that, fetch the script's last 4 KB and look for a `//# sourceMappingURL=` comment — that map is where the `nanoid` paths live.
- 2
Find the package in the bundle
Open DevTools → Network → reload. Click any application script and look at its sourcemap. Inside, search `sources[]` for entries matching `node_modules/nanoid/` — every match confirms the package is bundled. The matching `sourcesContent[i]` for `node_modules/nanoid/package.json` gives you the exact installed version.
- 3
Read the version directly from package.json
Run `jq -r '. as $m | $m.sources | to_entries[] | select(.value | endswith("node_modules/nanoid/package.json")) | $m.sourcesContent[.key] | fromjson | .version' bundle.js.map`. Sourcemap Explorer automates the same query in the popup.
Major releases of nanoid
When each major version first landed. Major bumps are where breaking changes live, so this timeline is the fastest way to date the nanoid version a site actually ships against the ecosystem.
Recent security advisories for nanoid
The 2 most recent advisories affecting some versions of nanoid, aggregated from OSV.dev (GitHub Advisory + CVE data). A listing here doesn't mean the version a given site ships is affected — each advisory applies to a specific version range. Sourcemap Explorer reads the exact bundled version so you can check it against these ranges.
Predictable results in nanoid generation when given non-integer values
Exposure of Sensitive Information to an Unauthorized Actor in nanoid
Recent versions
nanoid README
Live mirror of the GitHub README, for reference. Updated whenever the repo's default branch changes.
Nano ID
English | 日本語 | Русский | 简体中文 | Bahasa Indonesia | 한국어 | العربية
A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
“An amazing level of senseless perfectionism, which is simply impossible not to respect.”
- Small. 118 bytes (minified and brotlied). No dependencies. Size Limit controls the size.
- Fast. 50% faster than native
crypto.randomUUID(). - Safe. It uses hardware random generator. Can be used in clusters.
- Short IDs. It uses a larger alphabet than UUID (
A-Za-z0-9_-). So ID size was reduced from 36 to 21 symbols. - Portable. Nano ID was ported to over 20 programming languages.
import { nanoid } from 'nanoid'
model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
Nano ID is built by Evil Martians, an American design and engineering consultancy for developer tools, AI, and cybersecurity startups.
Table of Contents
Comparison with UUID
Nano ID is quite comparable to UUID v4 (random-based). It has a similar number of random bits in the ID (126 in Nano ID and 122 in UUID), so it has a similar collision probability:
For there to be a one in a billion chance of duplication, 103 trillion version 4 IDs must be generated.
There are two main differences between Nano ID and UUID v4:
- Nano ID uses a bigger alphabet, so a similar number of random bits are packed in just 21 symbols instead of 36.
- Nano ID is faster than
crypto.randomUUIDanduuid/v4.
Benchmark
$ node ./test/benchmark.js
nope-id 20,386,830 ops/sec
nanoid 20,434,827 ops/sec
customAlphabet 20,544,476 ops/sec
crypto.randomUUID 12,865,759 ops/sec
uuid v4 7,930,104 ops/sec
@napi-rs/uuid 5,573,171 ops/sec
uid/secure 6,308,267 ops/sec
@lukeed/uuid 5,278,597 ops/sec
nanoid for browser 311,497 ops/sec
secure-random-string 301,667 ops/sec
uid-safe.sync 297,815 ops/sec
Non-secure:
uid 20,286,757 ops/sec
nanoid/non-secure 2,397,594 ops/sec
rndm 2,445,462 ops/sec
Security
See a good article about random generators theory: Secure random values (in Node.js)
-
Unpredictability. Instead of using the unsafe
Math.random(), Nano ID uses thecryptomodule in Node.js and the Web Crypto API in browsers. These modules use unpredictable hardware random generator. -
Uniformity.
random % alphabetis a popular mistake to make when coding an ID generator. The distribution will not be even; there will be a lower chance for some symbols to appear compared to others. So, it will reduce the number of tries when brute-forcing. Nano ID uses a better algorithm and is tested for uniformity.
-
Well-documented: all Nano ID hacks are documented. See comments in the source.
-
Vulnerabilities: to report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
Install
npm install nanoid
JSR
JSR is a replacement for npm with open governance and active development (in contrast to npm).
npx jsr add @sitnik/nanoid
You can use it in Node.js, Deno, Bun, etc.
// Replace `nanoid` to `@sitnik/nanoid` in all imports
import { nanoid } from '@sitnik/nanoid'
For Deno install it by deno add jsr:@sitnik/nanoid or import
from jsr:@sitnik/nanoid.
CDN
For quick hacks, you can load Nano ID from CDN. Though, it is not recommended to be used in production because of the lower loading performance.
import { nanoid } from 'https://cdn.jsdelivr.net/npm/nanoid/nanoid.js'
API
By default, Nano ID uses URL-friendly symbols (A-Za-z0-9_-) and returns an ID
with 21 characters (to have a collision probability similar to UUID v4).
import { nanoid } from 'nanoid'
model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
If you want to reduce the ID size (and increase collisions probability), you can pass the size as an argument.
nanoid(10) //=> "IRFa-VaY2b"
Don’t forget to check the safety of your ID size in our ID collision probability calculator.
You can also use a custom alphabet or a random generator.
Custom Alphabet or Size
customAlphabet returns a function that allows you to create nanoid
with your own alphabet and ID size.
import { customAlphabet } from 'nanoid'
const nanoid = customAlphabet('1234567890abcdef', 10)
model.id = nanoid() //=> "4f90d13a42"
import { customAlphabet } from 'nanoid/non-secure'
const nanoid = customAlphabet('1234567890abcdef', 10)
user.id = nanoid()
Check the safety of your custom alphabet and ID size in our
ID collision probability calculator. For more alphabets, check out the options
in nanoid-dictionary.
Alphabet must contain 256 symbols or less. Otherwise, the security of the internal generator algorithm is not guaranteed.
In addition to setting a default size, you can change the ID size when calling the function:
import { customAlphabet } from 'nanoid'
const nanoid = customAlphabet('1234567890abcdef', 10)
model.id = nanoid(5) //=> "f01a2"
Custom Random Bytes Generator
customRandom allows you to create a nanoid and replace alphabet
and the default random bytes generator.
In this example, a seed-based generator is used:
import { customRandom } from 'nanoid'
const rng = seedrandom(seed)
const nanoid = customRandom('abcdef', 10, size => {
return new Uint8Array(size).map(() => 256 * rng())
})
nanoid() //=> "fbaefaadeb"
random callback must accept the array size and return an array
with random numbers.
If you want to use the same URL-friendly symbols with customRandom,
you can get the default alphabet using the urlAlphabet.
import { customRandom, urlAlphabet } from 'nanoid'
const nanoid = customRandom(urlAlphabet, 10, random)
Note, that between Nano ID versions we may change random generator call sequence. If you are using seed-based generators, we do not guarantee the same result.
Non-Secure
Nano ID uses hardware random bytes generation for security and low collision probability. If you are not so concerned with security, you can use it for environments without hardware random generators.
import { nanoid } from 'nanoid/non-secure'
const id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"
Note, that non-secure version is slower than secure. Use it only if you have to.
Usage
React
There’s no correct way to use Nano ID for React key prop
since it should be consistent among renders.
function Todos({ todos }) {
return (
<ul>
{todos.map(todo => (
<li key={nanoid()}>
{' '}
/* DON’T DO IT */
{todo.text}
</li>
))}
</ul>
)
}
You should rather try to reach for stable ID inside your list item.
const todoItems = todos.map(todo => <li key={todo.id}>{todo.text}</li>)
In case you don’t have stable IDs you'd rather use index as key
instead of nanoid():
const todoItems = todos.map((text, index) => (
<li key={index}>
{' '}
/* Still not recommended but preferred over nanoid(). Only do this if items
have no stable IDs. */
{text}
</li>
))
In case you just need random IDs to link elements like labels
and input fields together, useId is recommended.
That hook was added in React 18.
React Native
React Native does not have built-in random generator. The following polyfill
works for plain React Native and Expo starting with 39.x.
- Check
react-native-get-random-valuesdocs and install it. - Import it before Nano ID.
import 'react-native-get-random-values'
import { nanoid } from 'nanoid'
PouchDB and CouchDB
In PouchDB and CouchDB, IDs can’t start with an underscore _.
A prefix is required to prevent this issue, as Nano ID might use a _
at the start of the ID by default.
Override the default ID with the following option:
db.put({
_id: 'id' + nanoid(),
…
})
CLI
You can get unique ID in terminal by calling npx nanoid. You need only
Node.js in the system. You do not need Nano ID to be installed anywhere.
$ npx nanoid
npx: installed 1 in 0.63s
LZfXLFzPPR4NNrgjlWDxn
Size of generated ID can be specified with --size (or -s) option:
$ npx nanoid --size 10
L3til0JS4z
Custom alphabet can be specified with --alphabet (or -a) option
(note that in this case --size is required):
$ npx nanoid --alphabet abc --size 15
bccbcabaabaccab
TypeScript
Nano ID allows casting generated strings into opaque strings in TypeScript. For example:
declare const userIdBrand: unique symbol
type UserId = string & { [userIdBrand]: true }
// Use explicit type parameter:
mockUser(nanoid<UserId>())
interface User {
id: UserId
name: string
}
const user: User = {
// Automatically casts to UserId:
id: nanoid(),
name: 'Alice'
}
Other Programming Languages
Nano ID was ported to many languages. You can use these ports to have the same ID generator on the client and server side.
- C
- C#
- C++
- Clojure and ClojureScript
- ColdFusion/CFML
- Crystal
- Dart & Flutter
- Elixir
- Gleam
- Go
- Haskell
- Haxe
- Janet
- Java
- Kotlin
- MySQL/MariaDB
- Nim
- OCaml
- Perl
- PHP
- Python native implementation with dictionaries and fast implementation (written in Rust)
- Postgres Extension and Native Function
- R (with dictionaries)
- Ruby
- Rust
- Swift
- Unison
- V
- Zig
For other environments, CLI is available to generate IDs from a command line.
Tools
- ID size calculator shows collision probability when adjusting the ID alphabet or size.
nanoid-dictionarywith popular alphabets to use withcustomAlphabet.nanoid-goodto be sure that your ID doesn’t contain any obscene words.
FAQ
What is nanoid used for?
A tiny (118 bytes), secure URL-friendly unique string ID generator
How can I tell if a website is using nanoid?
Open the page in Chrome with the Sourcemap Explorer extension installed and read the Stack tab. We catch `nanoid` from two complementary signals: `node_modules/nanoid/` paths inside the JavaScript sourcemap, and the embedded `package.json` we read for exact-version detection. Without the extension you can do the same lookup manually in DevTools — the steps are listed in the "How Sourcemap Explorer detects" section above.
How do I find out which version of nanoid a website is running?
Read it straight from the site's JavaScript sourcemap. When a build ships source maps, the bundled `nanoid/package.json` carries the exact `version` string — Sourcemap Explorer extracts it in one click on the Stack tab, and you can do it by hand in DevTools by opening the `.map` file and searching for `node_modules/nanoid/package.json`. That is far more reliable than inferring the version from an asset-hash or a `?ver=` query string, which is all surface-level detectors have to go on. The current npm release is 6.0.1, but real deployments frequently run an older pinned version — which is exactly why reading the bundled number matters.
What is the latest version of nanoid?
6.0.1, as published on the npm registry. The "Recent versions" table on this page lists the most recent 8 releases with their release dates. Sourcemap Explorer reports the version actually bundled into a site, which can lag the latest release by months on real-world deployments.
Is nanoid actively maintained?
Very actively maintained — the last release shipped within the past three months. The last published release was 2026-08-03. Source code: https://github.com/ai/nanoid.
Does nanoid have known security vulnerabilities?
2 recent advisories affecting some versions of nanoid are listed in the "Recent security advisories" section above, aggregated from OSV.dev (GitHub Advisory + CVE data). Whether a particular site is exposed depends entirely on the exact version it ships — each advisory applies to a specific version range, not to the package as a whole. That is why the precise bundled version matters: Sourcemap Explorer reads the version a site actually runs, so you can check it against the affected ranges instead of assuming the latest release is what's deployed.
Where can I read more?
Project homepage: https://github.com/ai/nanoid#readme. Source code: https://github.com/ai/nanoid. Published on npm: https://www.npmjs.com/package/nanoid. Licensed as MIT.
Detected by Sourcemap Explorer
When a bundle ships sourcemaps, we read the embedded package.json for nanoid and report the precise version (the registry's latest is v6.0.1, published 2026-08-03; the bundled copy is often older). Without sourcemaps, an import / require in the page's scripts is enough to flag it.