Last Updated: 3/9/2026
Troubleshooting
Common issues and solutions when using Nano ID.
Installation Issues
”Cannot find module ‘nanoid’”
Symptoms:
Error: Cannot find module 'nanoid'Solutions:
-
Install the package:
npm install nanoid -
Check package.json:
{ "dependencies": { "nanoid": "^5.0.0" } } -
Clear cache and reinstall:
rm -rf node_modules package-lock.json npm install
“require() of ES Module not supported”
Symptoms:
Error [ERR_REQUIRE_ESM]: require() of ES Module
/node_modules/nanoid/index.js not supportedCause: Trying to require() an ESM package.
Solutions:
-
Use dynamic import (Node.js 18+):
let nanoid async function getId() { if (!nanoid) { ({ nanoid } = await import('nanoid')) } return nanoid() } -
Use Node.js 22.12+ with native support:
const { nanoid } = require('nanoid') // Works in 22.12+ -
Use Node.js 20 with flag:
node --experimental-require-module script.js -
Downgrade to Nano ID 3.x:
npm install nanoid@3 -
Convert to ESM:
// package.json { "type": "module" }// script.js import { nanoid } from 'nanoid'
TypeScript Issues
”Cannot find module ‘nanoid’ or its type declarations”
Symptoms:
TS2307: Cannot find module 'nanoid' or its corresponding type declarations.Solutions:
-
Install package:
npm install nanoid -
Update tsconfig.json:
{ "compilerOptions": { "module": "ESNext", "moduleResolution": "bundler", "esModuleInterop": true } } -
For Node16 resolution:
{ "compilerOptions": { "module": "Node16", "moduleResolution": "node16" } }
“Module ‘“nanoid”’ has no exported member ‘nanoid’”
Symptoms:
TS2305: Module '"nanoid"' has no exported member 'nanoid'Cause: Incorrect import syntax.
Solution:
// ❌ Wrong
import nanoid from 'nanoid'
// ✅ Correct
import { nanoid } from 'nanoid'React Issues
IDs Change on Every Render
Symptoms: Component re-renders completely, losing state.
Cause: Generating ID in render function.
Solution:
import { nanoid } from 'nanoid'
import { useMemo } from 'react'
// ❌ Wrong
function Component() {
const id = nanoid() // New ID every render!
return <div id={id}>Content</div>
}
// ✅ Correct
function Component() {
const id = useMemo(() => nanoid(), []) // Generate once
return <div id={id}>Content</div>
}“crypto.getRandomValues is not a function”
Symptoms (React Native):
TypeError: crypto.getRandomValues is not a functionCause: Missing polyfill in React Native.
Solution:
npm install react-native-get-random-values// MUST be imported BEFORE nanoid
import 'react-native-get-random-values'
import { nanoid } from 'nanoid'Browser Issues
”crypto is not defined”
Symptoms:
ReferenceError: crypto is not definedCause: Very old browser or non-HTTPS context.
Solutions:
-
Use HTTPS:
cryptoonly available on HTTPS (or localhost) -
Use non-secure variant:
import { nanoid } from 'nanoid/non-secure' -
Check browser support: Chrome 11+, Firefox 21+, Safari 6.1+
Bundle Size Too Large
Symptoms: Nano ID adds unexpected size to bundle.
Cause: Bundler not tree-shaking correctly.
Solutions:
-
Use named imports:
// ✅ Good: Tree-shakeable import { nanoid } from 'nanoid' // ❌ Bad: Imports everything import * as Nanoid from 'nanoid' -
Check bundler config (Webpack):
module.exports = { optimization: { usedExports: true, sideEffects: false } } -
Use non-secure for smaller size:
import { nanoid } from 'nanoid/non-secure' // 90 bytes vs 118 bytes
Performance Issues
Slow ID Generation
Symptoms: Generating IDs takes >1ms.
Cause: Recreating custom alphabet generator.
Solution:
import { customAlphabet } from 'nanoid'
// ❌ Wrong: Creates generator every time
for (let i = 0; i < 1000; i++) {
const nanoid = customAlphabet('0123456789', 10)
const id = nanoid() // Slow!
}
// ✅ Correct: Create once, use many times
const nanoid = customAlphabet('0123456789', 10)
for (let i = 0; i < 1000; i++) {
const id = nanoid() // Fast!
}High Memory Usage
Symptoms: Memory grows when generating many IDs.
Cause: Not a Nano ID issue (IDs are just strings).
Solution: Check if storing IDs unnecessarily:
// ❌ Bad: Storing all IDs in memory
const ids = []
for (let i = 0; i < 1000000; i++) {
ids.push(nanoid())
}
// ✅ Good: Process and discard
for (let i = 0; i < 1000000; i++) {
const id = nanoid()
await db.insert({ id, data: '...' })
// ID discarded after insert
}Collision Issues
Getting Duplicate IDs
Symptoms: Database reports duplicate key errors.
Causes & Solutions:
-
ID too short:
// ❌ Bad: Only 36 bits of entropy const id = nanoid(6) // High collision risk! // ✅ Good: 126 bits of entropy const id = nanoid() // Default 21 chars -
Using non-secure in production:
// ❌ Bad: Predictable import { nanoid } from 'nanoid/non-secure' // ✅ Good: Cryptographically secure import { nanoid } from 'nanoid' -
Clock skew in distributed systems:
- Not a Nano ID issue (IDs are random, not time-based)
- Check if using time-based IDs elsewhere
-
Add collision detection:
async function createUniqueId(maxRetries = 3) { for (let i = 0; i < maxRetries; i++) { const id = nanoid() const exists = await db.exists(id) if (!exists) return id } throw new Error('Failed to generate unique ID') }
Testing Issues
Unpredictable Tests
Symptoms: Tests fail randomly due to different IDs.
Solution: Mock Nano ID:
// __mocks__/nanoid.js
let counter = 0
export const nanoid = () => `test-id-${counter++}`
// test.js
jest.mock('nanoid')
import { nanoid } from 'nanoid'
test('creates user', () => {
const id = nanoid() // "test-id-0"
expect(id).toBe('test-id-0')
})Snapshot Tests Failing
Symptoms: Snapshots differ due to random IDs.
Solution: Mock or use deterministic IDs in tests:
import { customRandom } from 'nanoid'
// Deterministic ID for tests
let seed = 0
const testNanoid = customRandom('abcdef', 10, (size) => {
return new Uint8Array(size).map(() => (seed++) % 256)
})
test('renders component', () => {
seed = 0 // Reset seed
const id = testNanoid() // Always same ID
expect(component).toMatchSnapshot()
})Getting Help
Check Documentation
Search Issues
Search GitHub Issues for similar problems.
Report Bug
- Check if issue exists
- Create minimal reproduction
- Open new issue
Security Issues
Report security vulnerabilities to Tidelift .