# @thzero/library_id_nanoid

> ![GitHub package.json version](https://img.shields.io/github/package-json/v/thzero/library_id_nanoid) ![David](https://img.shields.io/david/thzero/library_id_nanoid) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensour

Latest version **0.19.8** (published 2026-11-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @thzero/library_id_nanoid
pnpm add @thzero/library_id_nanoid
yarn add @thzero/library_id_nanoid
bun add @thzero/library_id_nanoid
```

## Health

**Score 60/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; no types; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.19.8 |
| Published | 2026-11-09 |
| First published | 2023-03-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=22 |
| Dependencies | 1 |
| Unpacked size | 14.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | thZero |
| Maintainers | thzero |

## Links

- npm: https://www.npmjs.com/package/@thzero/library_id_nanoid
- Repository: https://github.com/thzero/library_id_nanoid
- Homepage: https://github.com/thzero/library_id_nanoid#readme
- Issues: https://github.com/thzero/library_id_nanoid/issues
- npm.io page: https://npm.io/package/@thzero/library_id_nanoid

## Dependencies (1)

- [nanoid](https://npm.io/package/nanoid.md) ^6.0.2

## Recent versions

- 0.19.8 (latest) — 2026-11-09
- 0.19.7 — 2026-10-03
- 0.19.5 — 2026-10-02
- 0.19.4 — 2026-10-02
- 0.19.3 — 2026-10-02
- 0.19.1 — 2026-09-15
- 0.18.21 — 2026-08-27
- 0.18.20 — 2026-07-20
- 0.18.19 — 2026-06-19
- 0.18.18 — 2026-05-21
- 0.18.17 — 2026-05-10
- 0.18.16 — 2026-04-21
- 0.18.15 — 2026-02-07
- 0.18.13 — 2026-02-06
- 0.18.11 — 2025-10-16
- … 22 more at https://npm.io/package/@thzero/library_id_nanoid/versions

## README

![GitHub package.json version](https://img.shields.io/github/package-json/v/thzero/library_id_nanoid)
![David](https://img.shields.io/david/thzero/library_id_nanoid)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

# library_id_nanoid

Id generation for [@thzero/library_common](https://github.com/thzero/library_common),
backed by [nanoid](https://github.com/ai/nanoid).

The default — `library_common` depends on this one directly, so unless an
application swaps it out, this is what `Utility.generateId()` uses. Ids are URL
safe, 21 characters long and 16 short, on nanoid's default alphabet.

## Requirements

### NodeJs

[NodeJs](https://nodejs.org) version 22+

### Installation

[![NPM](https://nodei.co/npm/@thzero/library_id_nanoid.png?compact=true)](https://npmjs.org/package/@thzero/library_id_nanoid)

```
npm install @thzero/library_id_nanoid
```

Already a dependency of `@thzero/library_common`; install it explicitly only if
you import it directly.

#### Peer dependencies

None. [nanoid](https://github.com/ai/nanoid) is a direct dependency.

## What it provides

`index.js` — default export `IdGenerator`, a class of statics implementing the
generator contract `library_common` delegates to.

| Member | Behaviour |
|---|---|
| `generateId()` | `generateLongId()` |
| `generateLongId()` | 21 characters, or `_lengthLong` when set |
| `generateShortId()` | 16 characters, or `_lengthShort` when set |
| `setAlphabet(alphabet)` | Installs a `customAlphabet` generator. `null` restores the default |
| `setLengthLong(length)` / `setLengthShort(length)` | Set the lengths |
| `translateToShortId(id)` / `translateToId(id)` | **Identity.** nanoid ids have no separate short form |
| `init(alphabet)` | Builds and **returns** a generator — see below |

`openSource.js` — a default-exported function returning the licence manifest for
this package and nanoid, under both the `client` and `server` categories, for an
application's attribution page.

### `init` does not install anything

```js
IdGenerator.init('AB');          // returns a generator; the class is unchanged
IdGenerator.setAlphabet('AB');   // this is the one that takes effect
```

`init(alphabet)` returns `customAlphabet(alphabet)` and does not assign it to the
class, so calling it and expecting later `generateId()` calls to use that
alphabet is a trap the name invites. Use `setAlphabet`. `init` is useful only if
you want a standalone generator function of your own.

### Choosing lengths

Both lengths are a collision trade-off. Read
[nanoid's collision calculator](https://zelark.github.io/nano-id-cc) before
changing them or the alphabet — a short alphabet with a short length collides
much sooner than the defaults.

## Configuration

None. This package reads no configuration. Lengths and alphabet are set through
the API above.

## Wiring it up

Nothing to do — `library_common` already uses it. To set an alphabet or lengths:

```js
Utility.setIdGeneratorAlphabet(AppSharedConstants.IdGenerator.alphabet);
Utility.setIdGeneratorLengthLong(24);
Utility.setIdGeneratorLengthShort(8);
```

On the server these are reached through `BootMain`'s `_initIdGeneratorAlphabet`,
`_initIdGeneratorLengthLong` and `_initIdGeneratorLengthShort` hooks:

```js
class AppBootMain extends BootMain {
    _initIdGeneratorAlphabet() {
        return AppSharedConstants.IdGenerator.alphabet;
    }
}
```

### Swapping generators

The other two implementations of the same contract are
[library_id_shortuuid](https://github.com/thzero/library_id_shortuuid) (uuid v4
with a real 22-character short form) and
[library_id_uuid](https://github.com/thzero/library_id_uuid) (plain uuid v4).
Both are drop-in:

```js
import IdGenerator from '@thzero/library_id_shortuuid';

Utility.setIdGenerator(IdGenerator);
```

## Development

```
npm run lint       # eslint .
npm run lint:fix   # eslint . --fix
npm test           # node --test "test/*.test.js"
```

---
_Source: https://npm.io/package/@thzero/library_id_nanoid · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
