Last Updated: 3/9/2026
Installation
Nano ID supports multiple JavaScript environments and package managers. Choose the installation method that fits your project.
ESM (Recommended)
Nano ID 5 is designed for ESM projects using import statements. This is the recommended approach for modern JavaScript.
npm install nanoidThen import in your code:
import { nanoid } from 'nanoid'
const id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"Requirements
- Node.js 18+ or modern browsers
- ESM-compatible bundler (Vite, webpack 5+, Rollup, etc.)
CommonJS
For CommonJS projects (require() syntax), you have several options:
Option 1: Node.js 22.12+ (Recommended)
Latest Node.js supports require() for ESM packages out of the box:
const { nanoid } = require('nanoid')
const id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"Option 2: Node.js 20 with Flag
Node.js 20 requires the experimental flag:
node --experimental-require-module your-script.jsconst { nanoid } = require('nanoid')Option 3: Dynamic Import (Node.js 18+)
For older Node.js versions, use dynamic import():
let nanoid
module.exports.createID = async () => {
if (!nanoid) {
({ nanoid } = await import('nanoid'))
}
return nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
}Option 4: Nano ID 3.x
Nano ID 3 has native CommonJS support (still maintained):
npm install nanoid@3const { nanoid } = require('nanoid')JSR (JavaScript Registry)
JSR is a modern alternative to npm with better governance and TypeScript support.
npx jsr add @sitnik/nanoidThen import:
import { nanoid } from '@sitnik/nanoid'Deno
For Deno, install via JSR:
deno add jsr:@sitnik/nanoidOr import directly:
import { nanoid } from 'jsr:@sitnik/nanoid'Bun
Bun works with both npm and JSR:
bun add nanoid
# or
bun add @sitnik/nanoidCDN
For quick prototypes or experimentation, load Nano ID from a CDN:
import { nanoid } from 'https://cdn.jsdelivr.net/npm/nanoid/nanoid.js'
const id = nanoid()⚠️ Not recommended for production due to:
- Performance overhead (extra network request)
- No version pinning (breaking changes possible)
- Dependency on external CDN availability
Alternative CDNs
// unpkg
import { nanoid } from 'https://unpkg.com/nanoid/nanoid.js'
// esm.sh
import { nanoid } from 'https://esm.sh/nanoid'CLI
Generate IDs from the command line without installing Nano ID:
npx nanoid
# Output: LZfXLFzPPR4NNrgjlWDxnCustomize size:
npx nanoid --size 10
# Output: L3til0JS4zCustom alphabet:
npx nanoid --alphabet abc --size 15
# Output: bccbcabaabaccabSee CLI guide for more options.
Verification
Verify your installation:
import { nanoid } from 'nanoid'
console.log(nanoid()) // Should print a 21-character ID
console.log(nanoid(10)) // Should print a 10-character IDTroubleshooting
”Cannot find module ‘nanoid’”
- Ensure you’ve installed the package:
npm install nanoid - Check your
package.jsonhas"type": "module"for ESM - For TypeScript, ensure
"moduleResolution": "node16"or"bundler"
”require() of ES Module not supported”
You’re trying to require() an ESM package. See CommonJS options above.
Module resolution errors in TypeScript
Update tsconfig.json:
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "bundler",
"esModuleInterop": true
}
}See Troubleshooting for more solutions.
What’s Next?
- Quick Start - Generate your first IDs
- TypeScript - Type-safe ID generation
- Node.js - Platform-specific details