What is the Punycode in Node.js ?
Last Updated :
25 Apr, 2025
Punycode is a special encoding syntax that is specifically used to convert Unicode characters (UTF-8) to ASCII, which is nothing but the restricted string character set.Â
Why this type of specific conversion needed ? The hostnames will understand only ASCII characters. Punycode is used by the International Domain Names(IDN) in order to encode/decode the URL which has been typed in the browser.Â
For example: If you search mañana.com in the browser, your browser which has an inbuilt IDNA service convert that to xn--maana-pta.com with the help of Punycode converter embedded in the browser.Â
Now let's see, how-to-use Punycode with the help of Node.js.Â
Punycode in Node.js: Punycode is bundled with node.js v0.6.2 and the later versions. If you want to use Punycode, you need to install Punycode module using npm installation.Â
npm installation:
npm install punycode --save
Include punycode module:
const punycode = require('punycode');
punycode.decode(string): It is used to convert Punycode strings of ASCII to Unicode symbols.Â
Example:Â
javascript
// Include punycode module
const punycode = require('punycode');
// Decode Punycode strings of ASCII
// to Unicode symbols
console.log(punycode.decode('manama-pta'));
console.log(punycode.decode('--dqo34k'));
Output:
Â
Â
punycode.encode(string): It is used to convert Unicode strings to Punycode strings of ASCII symbols.Â
Example:Â
JavaScript
// Include punycode module
const punycode = require('punycode');
// Encode Unicode symbols to
// Punycode ASCII string
console.log(punycode.encode('máanama'));
console.log(punycode.encode('?-?'));
Output:
manama-pta
--dqo34k
punycode.toUnicode(input): It is used to convert Punycode strings that represent a domain name or an email address to Unicode symbols. It doesn't matter you call it on an already converted Unicode.Â
Example:Â
javascript
// Include punycode module
const punycode = require('punycode');
console.log(punycode.toUnicode('xn--maana-pta.com'));
console.log(punycode.toUnicode('xn----dqo34k.com'));
Output:
Â
Â
punycode.toASCII(input): It is used to convert lowercased Unicode strings that represent a domain name or an email address to Punycode symbols. It doesn't matter you call it with a domain that's already in ASCII.Â
Example:Â
javascript
// Include punycode module
const punycode = require('punycode');
console.log(punycode.toASCII('mañana.com'));
console.log(punycode.toASCII('?-?.com'));
Output:
xn--maana-pta.com
xn----dqo34k.com
punycode.ucs2.decode(string): Creates an array of numeric code point values for each Unicode code symbols in the string.Behind the scenes in the browser which was built on Javascript internally, the UCS-2 function in it, will convert a pair of surrogate halves into a single coded point.Â
Example:Â
javascript
// Include punycode module
const punycode = require('punycode');
// Decoding strings
console.log(punycode.ucs2.decode('abc'));
console.log(punycode.ucs2.decode('\uD834\uDF06'));
Output:
[ 97, 98, 99 ]
[ 119558 ]
UCS-2: UCS-2 is a 2-byte Universal Character Set that produces a fixed-length format by using 16-bit code unit. The code point ranges from 0 to 0xFFFF.Â
Surrogate pairs: Characters that are outside BMP, e.g. U+1D306 TETRAGRAM FOR CENTRE:, can only be encoded by using two 16-bit code units. This is known as "surrogate pairs". The Surrogate pairs only represent a single character alone.Â
punycode.ucs2.encode(codePoints): It is used to create a string based on the array of numeric code point values.Â
Example:Â
javascript
// Include punycode module
const punycode = require('punycode');
console.log(punycode.ucs2.encode([0x61, 0x62, 0x63]));
console.log(punycode.ucs2.encode([0x1D306]));
Output:
abc
????
You can see the Punycode Converter to see the live result.
Similar Reads
What is package.json in Node.js ?
In the world of Node.js development, package.json is a crucial file that serves as the heart of any Node.js project. It acts as a manifest that defines the projectâs metadata, dependencies, scripts, and more. This article will provide an in-depth look at what package.json is, why it's essential, and
4 min read
What is the Purpose of __filename Variable in Node.js ?
In Node.js, there are several global variables that are available in all modules. One such variable is __filename. This article delves into the purpose, usage, and practical applications of the __filename variable in Node.js.What is __filename?The __filename variable is a built-in global variable in
3 min read
What are modules in Node JS ?
In NodeJS, modules are encapsulated units of code that can be reused across different parts of an application. Modules help organize code into smaller, manageable pieces, promote code reusability, and facilitate better maintainability and scalability of NodeJS applications. Types of Modules:Core Mod
2 min read
What are Modules in Node.js ?
In Node.js Application, a Module can be considered as a block of code that provide a simple or complex functionality that can communicate with external application. Modules can be organized in a single file or a collection of multiple files/folders. Almost all programmers prefer modules because of t
5 min read
Why Zlib is used in Node.js ?
Zlib is a crucial library in Node.js used for data compression and decompression. It's widely employed in web development and various applications to handle data more efficiently. Understanding Zlib's significance in Node.js can greatly enhance your capability to manage data transmission, storage, a
4 min read
What is Buffer in Node.js ?
In Node, Buffer is used to store and manage binary data. Pure JavaScript is great with Unicode-encoded strings, but it does not handle binary data very well. It is not problematic when we perform an operation on data at the browser level but at the time of dealing with TCP stream and performing a re
3 min read
What are the Key Features of Node.js ?
Node.js has gained immense popularity among developers for its ability to handle server-side operations efficiently and effectively. Built on Chrome's V8 JavaScript engine, Node.js is designed to build scalable and high-performance applications. Here, we explore the key features that make Node.js a
5 min read
What is the purpose of the Buffer class in Node ?
In Node, the Buffer class plays a crucial role in handling binary data, allowing developers to work with raw binary data directly. The Buffer class provides a way to create, manipulate, and convert binary data efficiently, making it essential for various tasks such as file I/O, network communication
2 min read
What is the purpose of the 'node_modules' folder ?
The node_modules folder is a directory in NodeJS projects that stores third-party libraries and dependencies. It's essential for managing dependencies, which are packages or modules that a NodeJS project relies on. When you install a package using npm or Yarn, these tools download the package along
5 min read
What is Crypto Module in Node.js and How it is used ?
The crypto module in Node.js provides cryptographic functionality that includes a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions. This module enables you to perform various security operations, such as hashing, encryption, and decryption, directly in your Node
4 min read