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. 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.
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.
| Runtime | require("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.18Module format by version
Derived from the type, main and exports fields of every stable release on npm (132 versions; the newest 60 shown).
| Versions | Format | First published |
|---|---|---|
| 4.0.0 – 6.0.1 | ESM-only | Jun 8, 2022 |
| 3.3.6 – 3.3.18 | Dual (CommonJS + ESM) | Mar 26, 2023 |
| 3.3.5 | CommonJS | Mar 26, 2023 |
| 3.1.28 – 3.3.4 | Dual (CommonJS + ESM) | Sep 26, 2021 |
| 3.1.27 | CommonJS | Sep 26, 2021 |
| 3.1.25 – 3.1.26 | Dual (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
- ansi-styles 7.0.0last CJS
5.2.0 - strip-ansi 7.2.0last CJS
6.0.1 - commander 15.0.0last CJS
14.0.3 - supports-color 11.0.0last CJS
8.1.1 - chalk 6.0.0last CJS
4.1.2 - tslib 2.8.1last CJS
2.5.2 - string-width 8.2.2last CJS
4.2.3 - ansi-regex 6.3.0last CJS
5.0.1
Similar popularity
- @esbuild/linux-x64 0.28.2CommonJS
- locate-path 8.0.0ESM-only
- p-locate 7.0.0ESM-only
- ignore 7.0.9CommonJS
- resolve-from 5.0.0CommonJS
- cliui 9.0.1ESM-only
- cross-spawn 7.0.6CommonJS
- resolve 1.22.12CommonJS