canirequire

canirequire › ESM-only › nanoid

Is nanoid ESM-only? Can you require() it?

A tiny (118 bytes), secure URL-friendly unique string ID generator

Last verified Sep 9, 2026 against nanoid@6.0.1. Markdown · JSON

ESM-only ESM-only since 4.0.0 — last CommonJS version is 3.3.18

nanoid dropped its CommonJS build in 4.0.0 (June 2022). The last version with a plain require()-able CommonJS entry is 3.3.18, published Aug 7, 2026; it declares support for Node.js ^10 || ^12 || ^13.7 || ^14 || >=15.0.1.

On Node.js 22.12+ and newer (nanoid@6.0.1 declares Node.js ^22 || ^24 || >=26), require("nanoid") works with the latest version: Node.js loads synchronous ES modules through require(). Verified on Node.js 22, 24 and 26. Node.js 18 has no require(esm), and nanoid@6.0.1 declares Node.js ^22 || ^24 || >=26, so 18 is outside its supported range anyway.

nanoid@6.0.1 declares engines "^22 || ^24 || >=26", so Node.js 18, 20 are outside its supported range regardless of module format.

What to do

  1. On Node.js 22.12+ and newer (nanoid@6.0.1 declares Node.js ^22 || ^24 || >=26), require("nanoid") works with the latest version: Node.js loads synchronous ES modules through require(). Verified on Node.js 22, 24 and 26.
  2. From a CommonJS file, use a dynamic import inside an async function: const nanoid = await import("nanoid"). That works on every Node.js version the package supports (^22 || ^24 || >=26).
  3. If you need a synchronous require() or run Node.js 18: npm install nanoid@3.3.18, the last CommonJS release (published Aug 7, 2026); it declares Node.js ^10 || ^12 || ^13.7 || ^14 || >=15.0.1.
  4. nanoid@6.0.1 declares engines "^22 || ^24 || >=26", so Node.js 18, 20 are unsupported regardless: upgrade Node.js or pin an older release.
  5. TypeScript: types are bundled; install nothing extra.
Latest
6.0.1 (Aug 3, 2026)
Module format
ESM-only
Last CommonJS version
3.3.18
ESM-only since
4.0.0 (Jun 8, 2022)
TypeScript types
Bundled
Node engines
^22 || ^24 || >=26
Weekly downloads
214.3M
License
MIT

Does nanoid@6.0.1 actually load? Runtime test results

Installed and executed on Sep 9, 2026 with require("nanoid") from a CommonJS file and import "nanoid" from an ES module.

Runtimerequire("nanoid")import "nanoid"
Node.js 18✗ failsERR_REQUIRE_ESM✓ works
Node.js 20✓ works✓ works
Node.js 22✓ works✓ works
Node.js 24✓ works✓ works
Node.js 26✓ works✓ works
Bun 1.4.0✓ works✓ works

How to import nanoid

ES modules (import)

import * as nanoid from "nanoid";

Namespace import shown because the ESM entry may have no default export; use named imports for what it exports.

CommonJS (require)

// Node.js 22.12+ and newer (verified):
const nanoid = require("nanoid");

// Any Node.js version the package supports (^22 || ^24 || >=26), from an async function:
async function main() {
  const nanoid = await import("nanoid");
}

// Or pin the last CommonJS release (declares Node.js ^10 || ^12 || ^13.7 || ^14 || >=15.0.1):
// npm install nanoid@3.3.18

Module format by version

Derived from the type, main and exports fields of every stable release on npm, corrected where a runtime test of that version contradicted them (132 versions; the newest 60 shown).

VersionsFormatFirst published
4.0.0 – 6.0.1ESM-onlyJun 8, 2022
3.3.6 – 3.3.18Dual (CommonJS + ESM)Mar 26, 2023
3.3.5CommonJSMar 26, 2023
3.1.28 – 3.3.4Dual (CommonJS + ESM)Sep 26, 2021
3.1.27CommonJSSep 26, 2021
3.1.25 – 3.1.26Dual (CommonJS + ESM)Aug 12, 2021

Frequently asked

Is nanoid ESM-only?

nanoid@6.0.1 is ESM-only. nanoid dropped its CommonJS build in 4.0.0 (June 2022). The last version with a plain require()-able CommonJS entry is 3.3.18, published Aug 7, 2026; it declares support for Node.js ^10 || ^12 || ^13.7 || ^14 || >=15.0.1.

Which version of nanoid still supports CommonJS / require()?

3.3.18 is the last version of nanoid with a CommonJS build; it declares Node.js ^10 || ^12 || ^13.7 || ^14 || >=15.0.1. Install it with "npm install nanoid@3.3.18". Every version from 4.0.0 on is ESM-only.

How do I use nanoid from a CommonJS file?

From a CommonJS file, use a dynamic import inside an async function: const nanoid = await import("nanoid"). That works on every Node.js version the package supports (^22 || ^24 || >=26). On Node.js 22.12+ and newer (nanoid@6.0.1 declares Node.js ^22 || ^24 || >=26), require("nanoid") works with the latest version: Node.js loads synchronous ES modules through require(). Verified on Node.js 22, 24 and 26. Alternatively pin nanoid@3.3.18 (declares Node.js ^10 || ^12 || ^13.7 || ^14 || >=15.0.1).

Does nanoid work in Bun?

Yes. import "nanoid" loaded successfully in Bun 1.4.0 on Sep 9, 2026.

Does nanoid ship TypeScript types?

Yes. nanoid bundles its own type declarations; no @types package is needed.

Links

Other esm-only packages

Similar popularity