{"id":12270,"library":"uid","title":"Tiny and Fast Unique ID Generator","description":"The `uid` package provides a highly optimized and compact utility for generating fixed-length random unique IDs in JavaScript environments, supporting both Node.js and browsers. The current stable version is 2.0.2, with releases focusing on performance, bug fixes, and TypeScript compatibility, indicating an active maintenance and development cadence. Key differentiators include its extremely small footprint (130B to 205B gzipped) and high performance, with benchmarks showing it to be significantly faster than alternatives like `nanoid` in its default non-secure mode. It offers three distinct modes: `uid` (fast, `Math.random`-based, non-secure), `uid/secure` (cryptographically secure via `crypto.getRandomValues`), and `uid/single` (non-secure, no internal cache, ideal for short-lived environments). This flexibility allows developers to choose the appropriate balance of speed, security, and bundle size for their specific use case.","status":"active","version":"2.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/lukeed/uid","tags":["javascript","id","uid","uuid","random","generate","secure","crypto","foid","typescript"],"install":[{"cmd":"npm install uid","lang":"bash","label":"npm"},{"cmd":"yarn add uid","lang":"bash","label":"yarn"},{"cmd":"pnpm add uid","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v2.0.0, the default export was replaced with a named export `uid` for the non-secure `Math.random` based ID generation.","wrong":"import uid from 'uid';","symbol":"uid","correct":"import { uid } from 'uid';"},{"note":"The secure, cryptographically random ID generator is imported from a subpath. It also uses a named export `uid`.","wrong":"import uidSecure from 'uid/secure';","symbol":"uid (secure)","correct":"import { uid } from 'uid/secure';"},{"note":"The 'single' mode, ideal for short-lived environments without an internal cache, is imported via a subpath and uses a named `uid` export. ES module imports are preferred.","wrong":"const { uid } = require('uid/single');","symbol":"uid (single)","correct":"import { uid } from 'uid/single';"},{"note":"For CommonJS environments, ensure you destructure the named `uid` export from the subpath.","wrong":"const uidSecure = require('uid/secure').default;","symbol":"CommonJS require (secure)","correct":"const { uid } = require('uid/secure');"}],"quickstart":{"code":"import { uid } from 'uid';\nimport { uid as secureUid } from 'uid/secure';\nimport { uid as singleUid } from 'uid/single';\n\n// Generate a default non-secure ID (length 11, hexadecimal alphabet)\nconst id1 = uid();\nconsole.log(`Default non-secure ID: ${id1}`); // e.g., 'c92054d1dd6'\n\n// Generate a non-secure ID with custom length\nconst id2 = uid(16);\nconsole.log(`Non-secure ID (length 16): ${id2}`); // e.g., '8234dbf9a7dcec3b'\n\n// Generate a cryptographically secure ID (length 11, hexadecimal alphabet)\n// Requires crypto.getRandomValues() support (Node.js & modern browsers)\nconst secureId = secureUid();\nconsole.log(`Secure ID: ${secureId}`); // e.g., 'f8a4b6c1e9d0a2b'\n\n// Generate a single-use ID (length 11, alphanumeric alphabet)\n// Does not maintain an internal buffer, good for ephemeral contexts\nconst singleUseId = singleUid();\nconsole.log(`Single-use ID: ${singleUseId}`); // e.g., 'aG5jK7lP2qR'\n\n// Example of generating multiple IDs within a loop\nfor (let i = 0; i < 3; i++) {\n  console.log(`Loop ID ${i}: ${uid(8)}`);\n}\n","lang":"typescript","description":"Demonstrates how to import and use the default, secure, and single-use `uid` generators with custom lengths."},"warnings":[{"fix":"Change `import uid from 'uid';` to `import { uid } from 'uid';`. For CommonJS, `const uid = require('uid');` should become `const { uid } = require('uid');`.","message":"The `uid` package changed its default export to a named export in v2.0.0. Code relying on `import uid from 'uid';` will break.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"For cryptographically secure unique IDs, use `import { uid } from 'uid/secure';`. Ensure the environment (Node.js or browser) supports the Web Crypto API's `crypto.getRandomValues()`.","message":"The default `uid` export and `uid/single` mode rely on `Math.random` and are NOT cryptographically secure. They should not be used for security-sensitive applications where unpredictability is paramount.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Verify `crypto.getRandomValues` availability in target environments if using `uid/secure`. For universal compatibility or non-security-critical needs, fall back to the default `uid` or `uid/single`.","message":"The `uid/secure` mode requires the environment's `crypto` module (specifically `crypto.getRandomValues`). In older or constrained environments without this API, `uid/secure` will not function correctly.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"For applications requiring a very high guarantee of uniqueness, consider using longer ID lengths or cryptographically secure methods (`uid/secure`) and evaluate the probability of collisions based on your specific use case.","message":"While `uid` aims for uniqueness, the risk of collisions increases with shorter lengths and a higher number of generated IDs. The default length of 11 uses a hexadecimal alphabet.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change the import statement to `import { uid } from 'uid';`.","cause":"Attempting to call `uid()` directly after an incorrect `import uid from 'uid';` statement in v2.0.0+ ESM environments, where `uid` is `undefined` because it's no longer the default export.","error":"TypeError: Cannot read properties of undefined (reading 'length')"},{"fix":"Destructure the `uid` function: `const { uid } = require('uid');`.","cause":"In CommonJS environments, attempting to call `require('uid')()` directly after the v2.0.0 breaking change. `require('uid')` now returns an object `{ uid: Function }`, not the function itself.","error":"TypeError: uid is not a function"}],"ecosystem":"npm"}