canirequire

canirequireESM-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. 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 you can require() on every Node.js version, including 18 and older, is 3.3.18, published Aug 7, 2026.

On Node.js 20.19+, 22.12+ and every newer release, require("nanoid") works anyway: Node can load synchronous ES modules through require(). Verified on Node 22, 24, 26. Node.js 18 and older still fail with ERR_REQUIRE_ESM.

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

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✓ works✓ works

How to import nanoid

ES modules (import)

import nanoid from "nanoid";

CommonJS (require)

// Node.js 20.19+ / 22.12+ / newer (package has no top-level await):
const nanoid = require("nanoid");

// Any Node.js version:
const nanoid = await import("nanoid");

// Or pin the last CommonJS release:
// npm install nanoid@3.3.18

Module format by version

Derived from the type, main and exports fields of every stable release on npm (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 you can require() on every Node.js version, including 18 and older, is 3.3.18, published Aug 7, 2026.

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

3.3.18 is the last version of nanoid with a CommonJS build. 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?

Use a dynamic import: const mod = await import("nanoid"). On Node.js 20.19+, 22.12+ and newer, a plain require("nanoid") also works for ESM packages that do not use top-level await. Alternatively pin nanoid@3.3.18.

Does nanoid work in Bun?

Yes. import "nanoid" loaded successfully in Bun 1.4 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