Skip to Content
getting-startedInstallation

Last Updated: 3/9/2026


Installation

Nano ID supports multiple JavaScript environments and package managers. Choose the installation method that fits your project.

Nano ID 5 is designed for ESM projects using import statements. This is the recommended approach for modern JavaScript.

npm install nanoid

Then 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:

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.js
const { 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@3
const { nanoid } = require('nanoid')

JSR (JavaScript Registry)

JSR  is a modern alternative to npm with better governance and TypeScript support.

npx jsr add @sitnik/nanoid

Then import:

import { nanoid } from '@sitnik/nanoid'

Deno

For Deno, install via JSR:

deno add jsr:@sitnik/nanoid

Or import directly:

import { nanoid } from 'jsr:@sitnik/nanoid'

Bun

Bun works with both npm and JSR:

bun add nanoid # or bun add @sitnik/nanoid

CDN

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: LZfXLFzPPR4NNrgjlWDxn

Customize size:

npx nanoid --size 10 # Output: L3til0JS4z

Custom alphabet:

npx nanoid --alphabet abc --size 15 # Output: bccbcabaabaccab

See 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 ID

Troubleshooting

”Cannot find module ‘nanoid’”

  1. Ensure you’ve installed the package: npm install nanoid
  2. Check your package.json has "type": "module" for ESM
  3. 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?