# idna

> Internationalized Domain Names in Applications (IDNA 2008) with UTS #46 compatibility mapping

Latest version **1.0.0** (published 2026-11-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install idna
pnpm add idna
yarn add idna
bun add idna
```

Provides the command `idna`.

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2026-11-28 |
| First published | 2020-04-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 387.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1 |
| Author | Tuğrul Topuz |
| Maintainers | tugrul |
| Keywords | idna, idna2008, idn, punycode, uts46, unicode, domain |

## Links

- npm: https://www.npmjs.com/package/idna
- Repository: https://github.com/tugrul/node-idna
- Homepage: https://github.com/tugrul/node-idna#readme
- Issues: https://github.com/tugrul/node-idna/issues
- npm.io page: https://npm.io/package/idna

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2026-11-28
- 0.0.1 — 2020-04-18

## README

# idna [![Build Status](https://github.com/tugrul/node-idna/actions/workflows/ci.yml/badge.svg)](https://github.com/tugrul/node-idna/actions)

Internationalized domain names for JavaScript and TypeScript: converts between the Unicode and the ASCII (`xn--`) form of a domain name and validates it against IDNA 2008 ([RFC 5890](https://www.rfc-editor.org/rfc/rfc5890)–[5893](https://www.rfc-editor.org/rfc/rfc5893)), with the optional [UTS #46](https://www.unicode.org/reports/tr46/) compatibility mapping.

It is a port of Python's [idna](https://github.com/kjd/idna) package (version 3.20) and behaves like it.

- No dependencies. The library uses no Node.js APIs, so it also runs in browsers.
- ES module and CommonJS builds, with type declarations.
- Lookup tables generated from Unicode 18.0.0.

## Installation

```sh
npm install idna
```

Node.js 18 or later.

## Usage

```js
import { decode, encode } from 'idna';
// or: const { decode, encode } = require('idna');

encode('ドメイン.テスト'); // 'xn--eckwd4c7c.xn--zckzah'
decode('xn--eckwd4c7c.xn--zckzah'); // 'ドメイン.テスト'
```

`alabel` and `ulabel` do the same for a single label:

```js
alabel('пример'); // 'xn--e1afmkfd'
ulabel('xn--e1afmkfd'); // 'пример'
```

### Compatibility mapping (UTS #46)

IDNA 2008 is strict about its input. Uppercase letters, for one, are not allowed. What a user types is usually normalized first, which is what the UTS #46 mapping does: it lowercases, folds fullwidth characters, removes soft hyphens and so on.

```js
encode('Königsgäßchen');
// InvalidCodepoint: Codepoint U+004B at position 1 of "Königsgäßchen" not allowed

encode('Königsgäßchen', { uts46: true }); // 'xn--knigsgchen-b4a3dun'
decode('xn--knigsgchen-b4a3dun'); // 'königsgäßchen'
```

### Decoding for display

To show a domain to a user, pass `display: true`. Labels that start with `xn--` but cannot be decoded are then kept as they are instead of failing the whole domain:

```js
decode('example.xn--pokxncvks'); // throws InvalidCodepoint
decode('example.xn--pokxncvks', { display: true }); // 'example.xn--pokxncvks'
```

## API

### Conversion

| Function | Description |
| --- | --- |
| `encode(domain, options?)` | Domain to its ASCII form. ASCII labels are validated and returned as they are. |
| `decode(domain, options?)` | Domain to its Unicode form. ASCII labels are lowercased. |
| `alabel(label)` | Single label to its A-label (ASCII form). |
| `ulabel(label)` | Single label to its U-label (Unicode form). |
| `uts46Remap(domain, std3Rules = true)` | Applies the UTS #46 mapping and returns the result in NFC. |
| `toASCII(domain)`, `toUnicode(domain)` | `encode` and `decode` without options. |

Options of `encode` and `decode`:

| Option | Default | Description |
| --- | --- | --- |
| `uts46` | `false` | Apply the UTS #46 mapping first. |
| `std3Rules` | `false` | With `uts46`: reject ASCII characters other than letters, digits and hyphens during the mapping. |
| `strict` | `false` | Split labels at `.` only. By default `。` (U+3002), `．` (U+FF0E) and `｡` (U+FF61) separate labels too. |
| `display` | `false` | `decode` only: keep `xn--` labels that are not valid, see above. |

A trailing dot is kept. `encode` enforces the DNS limits of 63 characters per label and 253 per domain.

### Validation

These are the checks the conversion functions run. `checkLabel` runs all of them.

| Function | Description |
| --- | --- |
| `checkLabel(label)` | Throws unless the label is valid under IDNA 2008. |
| `checkNfc(label)` | Throws unless the label is in Normalization Form C. |
| `checkHyphenOk(label)` | Throws for a hyphen at either end, or in both the 3rd and 4th position. |
| `checkInitialCombiner(label)` | Throws if the label starts with a combining mark. |
| `checkBidi(label, checkLtr = false)` | Throws unless the label satisfies the Bidi Rule of RFC 5893. |
| `validContextJ(label, pos)` | Whether the joiner at index `pos` is allowed there (RFC 5892, appendix A.1–A.2). |
| `validContextO(label, pos)` | Whether the character at index `pos` is allowed there (RFC 5892, appendix A.3–A.9). |
| `validLabelLength(label)` | Whether the label has at most 63 characters. |
| `validStringLength(domain, trailingDot)` | Whether the domain has at most 253 characters, 254 with a trailing dot. |

Indexes and lengths count code points, not UTF-16 code units.

### Code points

`getCodepointClass(cp)` returns the IDNA 2008 derived property value of a code point: `'PVALID'`, `'CONTEXTJ'`, `'CONTEXTO'`, `'DISALLOWED'` or `'UNASSIGNED'`.

```js
getCodepointClass(0x00df); // 'PVALID' (ß)
getCodepointClass(0x0041); // 'DISALLOWED' (A)
getCodepointClass(0x30fb); // 'CONTEXTO' (katakana middle dot)
```

`unicodeVersion` is the Unicode version of the tables, `'18.0.0'`.

### Errors

Invalid input throws an `IDNAError` or one of its subclasses:

- `InvalidCodepoint`: a character is not allowed in domain names.
- `InvalidCodepointContext`: a character is not allowed in this position.
- `IDNABidiError`: left-to-right and right-to-left characters are mixed in a way that is not allowed.

Besides the message, errors carry a stable `code` and, when a single character is at fault, `text` (the label or domain), `codepoint` and `position` (1-based, in code points):

```js
try {
    encode('ab😀.example');
} catch (error) {
    error.name; // 'InvalidCodepoint'
    error.code; // 'disallowed_codepoint'
    error.codepoint; // 0x1f600
    error.position; // 3
    error.text; // 'ab😀'
}
```

The codes are `input_too_long`, `label_too_long`, `domain_too_long`, `empty_label`, `empty_domain`, `not_nfc`, `hyphen_3_4`, `hyphen_start_end`, `leading_combiner`, `disallowed_codepoint`, `contextj`, `contexto`, `bidi_rule_1` to `bidi_rule_6`, `bidi_unknown_direction`, `invalid_alabel`, `non_canonical_alabel`, `uts46_disallowed` and `uts46_std3`.

## Command line

```sh
$ npx idna пример.рф
xn--e1afmkfd.xn--p1ai
$ npx idna xn--e1afmkfd.xn--p1ai
пример.рф
```

The direction is taken from the first domain unless `-e` (encode) or `-d` (decode) is given. Domains can also be piped in, one per line. The UTS #46 mapping is applied unless `--strict` is given. Failures are reported on standard error and make the exit status 1.

## `getLabel`

The original API of this package. It still works as it did in 0.0.1.

```js
const idna = require('idna');

idna.getLabel(49, true, true); // 'PVALID'
```

`getLabel(code, idna2003, idna2008)` looks up a code point in the [tables ICANN published](https://www.icann.org/resources/pages/idna-protocol-2012-02-25-en) for the IDN ccTLD Fast Track, which compare IDNA2003 and IDNA2008 for Unicode 5.2. The two flags select the table:

| `idna2003` | `idna2008` | Table |
| --- | --- | --- |
| `true` | `true` | Characters valid under both protocols |
| `true` | `false` | Characters valid under IDNA2003 only |
| `false` | `true` | Characters valid under IDNA2008 only |

It returns `'PVALID'`, `'CONTEXTO'` or `'DISALLOWED'`, or `'UNDEF'` when the table does not list the code point, and throws when both flags are `false`. The tables are frozen. For the class of a code point in current Unicode use `getCodepointClass`.

## Differences from Python's idna

- Names are camelCase, and the keyword arguments of `encode` and `decode` are an options object.
- Everything is a string. There are no `bytes` inputs or results, and no codec.
- The deprecated `transitional` argument does not exist.
- A value that is not a string throws a `TypeError`, not an `IDNAError`.
- An A-label that decodes to surrogate code points is rejected as `invalid_alabel`.

## Notes

- **Unicode version.** The tables are generated from one Unicode version, but normalization is done by the JavaScript runtime. A runtime with older Unicode data does not know how to normalize characters added since, so it can accept or reject a label with such characters differently.
- **Not everything UTS #46 allows is valid.** With `uts46`, the mapping prepares the input, but labels are still validated against IDNA 2008. Emoji and other symbols stay invalid.
- **Where it differs from UTS #46.** A trailing dot is accepted, and the Bidi Rule is applied to each label that has right-to-left characters, not to the other labels of the same domain.

## Development

Development needs Node.js 22.18 or later, which runs the TypeScript sources directly.

```sh
npm install
npm test            # run the tests
npm run typecheck   # check types
npm run build       # compile to dist/
```

The tests include the conformance test of UTS #46 (`test/fixtures/IdnaTestV2.txt`).

GitHub Actions runs these steps on Node.js 22, 24 and 26 for every push to `master` and every pull request, and tries the packed package on Node.js 18 and 20.

### Updating the Unicode data

The tables in `src/data` are generated from the Unicode Character Database by `scripts/update.ts`. It follows RFC 5892 to derive the IDNA 2008 classes, and downloads the data files from unicode.org:

```sh
npm run update -- 19.0.0
```

Without a version, the tables are rebuilt for the version they already use. Set `UNICODE_BASE_URL` to download from a mirror of `https://www.unicode.org/Public`.

### Releasing

Pushing a version tag starts the `Publish` workflow, which tests the package and stages it on npm:

```sh
npm version patch   # or minor, major
git push --follow-tags
```

The staged version is published once a maintainer approves it on npmjs.com, or with `npm stage approve`.

The workflow uses npm [trusted publishing](https://docs.npmjs.com/trusted-publishers) instead of a token. For this, the package settings on npmjs.com have to list the GitHub repository `tugrul/node-idna` with the workflow `publish.yml` as trusted publisher.

## License

MIT.

The validation logic is ported from Python's idna (BSD 3-Clause), and the tables are derived from Unicode data files (Unicode License v3). See [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).

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