Gathering detailed insights and metrics for nanoid
Gathering detailed insights and metrics for nanoid
Gathering detailed insights and metrics for nanoid
Gathering detailed insights and metrics for nanoid
A tiny (124 bytes), secure, URL-friendly, unique string ID generator for JavaScript
npm install nanoid
99.6
Supply Chain
77.9
Quality
90.1
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
24,707 Stars
1,095 Commits
795 Forks
157 Watching
3 Branches
134 Contributors
Updated on 28 Nov 2024
Minified
Minified + Gzipped
JavaScript (96.32%)
HTML (3.68%)
Cumulative downloads
Total Downloads
Last day
-3.9%
8,059,463
Compared to previous day
Last week
4.2%
45,714,433
Compared to previous week
Last month
9.8%
186,822,821
Compared to previous month
Last year
26%
1,913,909,538
Compared to previous year
No dependencies detected.
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.”
A-Za-z0-9_-
).
So ID size was reduced from 36 to 21 symbols.1import { nanoid } from 'nanoid' 2model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
Made at Evil Martians, product consulting for developer tools.
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:
uuid/v4
package:
130 bytes instead of 423.1$ node ./test/benchmark.js 2crypto.randomUUID 7,619,041 ops/sec 3uuid v4 7,436,626 ops/sec 4@napi-rs/uuid 4,730,614 ops/sec 5uid/secure 4,729,185 ops/sec 6@lukeed/uuid 4,015,673 ops/sec 7nanoid 3,693,964 ops/sec 8customAlphabet 2,799,255 ops/sec 9nanoid for browser 380,915 ops/sec 10secure-random-string 362,316 ops/sec 11uid-safe.sync 354,234 ops/sec 12shortid 38,808 ops/sec 13 14Non-secure: 15uid 11,872,105 ops/sec 16nanoid/non-secure 2,226,483 ops/sec 17rndm 2,308,044 ops/sec
Test configuration: Framework 13 7840U, Fedora 39, Node.js 21.6.
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 the crypto
module in Node.js and the Web Crypto API in browsers.
These modules use unpredictable hardware random generator.
Uniformity. random % alphabet
is 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.
1npm install nanoid
Nano ID 5 works only with ESM projects, in tests or Node.js scripts.
For CommonJS you need to use latest Node.js 20 or 22
with --experimental-require-module
:
1node --experimental-require-module app.js
Or you can use Nano ID 3.x (we still support it):
1npm install nanoid@3
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.
1import { nanoid } from 'https://cdn.jsdelivr.net/npm/nanoid/nanoid.js'
Nano ID has 2 APIs: normal and non-secure.
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).
The safe and easiest way to use Nano ID.
In rare cases could block CPU from other work while noise collection for hardware random generator.
1import { nanoid } from 'nanoid' 2model.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.
1nanoid(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.
By default, 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.
1import { nanoid } from 'nanoid/non-secure' 2const id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"
customAlphabet
returns a function that allows you to create nanoid
with your own alphabet and ID size.
1import { customAlphabet } from 'nanoid' 2const nanoid = customAlphabet('1234567890abcdef', 10) 3model.id = nanoid() //=> "4f90d13a42"
1import { customAlphabet } from 'nanoid/non-secure' 2const nanoid = customAlphabet('1234567890abcdef', 10) 3user.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:
1import { customAlphabet } from 'nanoid' 2const nanoid = customAlphabet('1234567890abcdef', 10) 3model.id = nanoid(5) //=> "f01a2"
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:
1import { customRandom } from 'nanoid' 2 3const rng = seedrandom(seed) 4const nanoid = customRandom('abcdef', 10, size => { 5 return (new Uint8Array(size)).map(() => 256 * rng()) 6}) 7 8nanoid() //=> "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
.
1const { customRandom, urlAlphabet } = require('nanoid') 2const 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.
There’s no correct way to use Nano ID for React key
prop
since it should be consistent among renders.
1function Todos({todos}) { 2 return ( 3 <ul> 4 {todos.map(todo => ( 5 <li key={nanoid()}> /* DON’T DO IT */ 6 {todo.text} 7 </li> 8 ))} 9 </ul> 10 ) 11}
You should rather try to reach for stable ID inside your list item.
1const todoItems = todos.map((todo) => 2 <li key={todo.id}> 3 {todo.text} 4 </li> 5)
In case you don’t have stable IDs you'd rather use index as key
instead of nanoid()
:
1const todoItems = todos.map((text, index) => 2 <li key={index}> /* Still not recommended but preferred over nanoid(). 3 Only do this if items have no stable IDs. */ 4 {text} 5 </li> 6)
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 does not have built-in random generator. The following polyfill
works for plain React Native and Expo starting with 39.x
.
react-native-get-random-values
docs and install it.1import 'react-native-get-random-values' 2import { nanoid } from 'nanoid'
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:
1db.put({ 2 _id: 'id' + nanoid(), 3 … 4})
Web Workers do not have access to a secure random generator.
Security is important in IDs when IDs should be unpredictable. For instance, in "access by URL" link generation. If you do not need unpredictable IDs, but you need to use Web Workers, you can use the non‑secure ID generator.
1import { nanoid } from 'nanoid/non-secure' 2nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"
Note: non-secure IDs are more prone to collision attacks.
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.
1$ npx nanoid 2npx: installed 1 in 0.63s 3LZfXLFzPPR4NNrgjlWDxn
Size of generated ID can be specified with --size
(or -s
) option:
1$ npx nanoid --size 10 2L3til0JS4z
Custom alphabet can be specified with --alphabet
(or -a
) option
(note that in this case --size
is required):
1$ npx nanoid --alphabet abc --size 15 2bccbcabaabaccab
Nano ID was ported to many languages. You can use these ports to have the same ID generator on the client and server side.
For other environments, CLI is available to generate IDs from a command line.
nanoid-dictionary
with popular alphabets to use with customAlphabet
.nanoid-good
to be sure that your ID doesn’t contain any obscene words.The latest stable version of the package.
Stable Version
1
5.5/10
Summary
Exposure of Sensitive Information to an Unauthorized Actor in nanoid
Affected Versions
>= 3.0.0, < 3.1.31
Patched Versions
3.1.31
Reason
11 commit(s) and 5 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
Found 7/26 approved changesets -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More