Package Exports
- nanoid
- nanoid/format
- nanoid/generate
- nanoid/random
- nanoid/random-browser
- nanoid/random-browser.js
- nanoid/random.js
- nanoid/url
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nanoid) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Nano ID
Very small secure URL-friendly unique ID generator.
var nanoid = require('nanoid')
model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"
Safe. It uses cryptographically-strong random APIs and tests symbols distribution.
Small. Only 258 bytes (minified and gzipped). Zero-dependency.
Compact. It uses more symbols than UUID (A-Za-z0-9_~
)
and have same uniqueness in 22 symbols instead of 36.
Generator supports Node.js and all browsers from IE 11.
Security
Good article about random generators theory: Secure random values (in Node.js)
Unpredictability
Nano ID doesn’t use unsafe Math.random()
. It use crypto
module in Node.js
and Web Crypto API in browsers.
Uniformity
random % alphabet
is a popular mistake in ID generator. Change to get some
symbols will be lower and it will reduce amount of guesses in bruteforcing.
Nano ID use better algorithm and test uniformity:

Usage
Normal
Main module uses URL-friendly symbols (A-Za-z0-9_~
) and return ID
with 22 characters (to have same uniqueness as UUID).
var nanoid = require('nanoid')
model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"
Symbols -,.()
are not encoded in URL, but in the end of link they could
be detected as punctuation.
Custom Alphabet or Length
If you want to change ID alphabet or length you can use low-level generate
module.
var generate = require('nanoid/generate')
model.id = generate('1234567890abcdef', 10) //=> "4f90d13a42"
If you want to use same URL-friendly symbols and just change length,
you can get default alphabet from url
module:
var url = require('nanoid/url')
model.id = generate(url, 10) //=> "Uakgb_J5m9"
Custom Random Bytes Generator
You can replace default safe random generators by format
module.
It could be useful if you need seed-based generator.
var format = require('nanoid/format')
function random (size) {
var result = []
for (var i = 0; i < size; i++) result.push(randomByte())
return result
}
format(random, "abcdef", 10) //=> "fbaefaadeb"
random
callback must accept array size and return array of random numbers.