The Punycode Module in Node.js

The punycode module in Node.js is a bundled implementation of the Punycode.js library, based on RFC 3492. It provides encoding and decoding functionality for Internationalized Domain Names (IDNs), converting Unicode characters into ASCII-compatible strings. Since hostnames in URLs must be ASCII-only, Punycode is used to represent non-ASCII domain names. However, the module has been deprecated since Node.js v7.0.0 and will be removed in future versions. Developers should migrate to the standalone Punycode.js library or use modern alternatives such as the WHATWG URL API.

Punycode RFC 3492Internationalized Domain Names (IDN)punycode.encode / punycode.decodepunycode.toASCII / punycode.toUnicodepunycode.ucs2.encode / punycode.ucs2.decode

~2 min read • Updated Dec 30, 2025

1. Introduction


Punycode is a character encoding scheme that converts Unicode strings into ASCII-only strings. It is primarily used for domain names containing non-ASCII characters, ensuring compatibility with DNS and URL systems.


const punycode = require('node:punycode');

2. Core Methods


  • punycode.encode(string): Converts Unicode strings to Punycode.
  • punycode.decode(string): Converts Punycode strings back to Unicode.

punycode.encode('mañana'); // 'maana-pta'
punycode.decode('maana-pta'); // 'mañana'

3. Domain Conversion


  • punycode.toASCII(domain): Converts Unicode domain names to Punycode.
  • punycode.toUnicode(domain): Converts Punycode domain names back to Unicode.

punycode.toASCII('mañana.com');  // 'xn--maana-pta.com'
punycode.toUnicode('xn--maana-pta.com'); // 'mañana.com'

4. UCS2 Utilities


  • punycode.ucs2.decode(string): Returns an array of Unicode code points.
  • punycode.ucs2.encode(codePoints): Builds a string from an array of code points.

punycode.ucs2.decode('abc'); // [0x61, 0x62, 0x63]
punycode.ucs2.encode([0x61, 0x62, 0x63]); // 'abc'

5. Version


  • punycode.version: Returns the current Punycode.js version string.

6. Deprecation and Alternatives


The punycode module is deprecated and will be removed in future Node.js releases. Recommended alternatives include:


  • The standalone Punycode.js library.
  • url.domainToASCII or the WHATWG URL API for domain encoding.

Conclusion


The punycode module provided a convenient way to handle internationalized domain names in Node.js. However, with its deprecation, developers should adopt modern APIs or external libraries to ensure compatibility and future-proof applications.


Written & researched by Dr. Shahin Siami