Punycode Converter
Punycode.js is a robust JavaScript library for converting Unicode strings to Punycode and vice-versa, strictly adhering to RFC 3492 (Punycode) and RFC 5891 (IDNA2008). It provides low-level functions for encoding and decoding individual string parts, as well as higher-level utilities for processing entire domain names and email addresses. The current stable version is 2.3.1. While previously bundled with Node.js up to v7, the userland module (`npm install punycode`) now specifically targets modern Node.js v6+ environments and browsers supporting ES2015+ features. Version 1.4.1 remains available for broader compatibility with older runtimes like Rhino, Ringo, and Narwhal, offering the same functionality with a UMD build. The project is actively maintained with periodic releases.
Common errors
-
Error: Cannot find module 'punycode'
cause Attempting to `require('punycode')` in a Node.js environment where the built-in module is not available or has been overridden by the npm package without the correct import path.fixIf you've installed the `punycode` npm package, ensure you are importing it with a trailing slash: `const punycode = require('punycode/');`. -
SyntaxError: Unexpected token ... (or similar ES2015+ syntax error)
cause Using `punycode` v2.0.0 or higher in an environment that does not fully support ES2015+ features.fixUpgrade your Node.js version to v6 or higher, or use a modern browser. If backward compatibility for older runtimes is essential, downgrade to `punycode` v1.4.1.
Warnings
- breaking Version 2.0.0 and above dropped support for older JavaScript environments. It targets Node.js v6+ and modern browsers, requiring ES2015+ features.
- gotcha In Node.js, the userland `punycode` package from npm can conflict with Node.js's deprecated built-in `punycode` module.
- deprecated The `punycode` module bundled with Node.js itself has been deprecated (soft-deprecated from v7) and is no longer recommended for new projects. It is advised to use the standalone npm package.
Install
-
npm install punycode -
yarn add punycode -
pnpm add punycode
Imports
- punycode
const punycode = require('punycode');const punycode = require('punycode/'); - punycode
import { decode, encode } from 'punycode';import punycode from 'punycode';
- toASCII
import { toASCII } from 'punycode'; toASCII('mañana.com');const punycode = require('punycode/'); punycode.toASCII('mañana.com');
Quickstart
const punycode = require('punycode/');
// Encode a Unicode domain name to Punycode
const unicodeDomain = 'mañana.com';
const punycodeDomain = punycode.toASCII(unicodeDomain);
console.log(`Unicode domain: ${unicodeDomain}`);
console.log(`Punycode domain: ${punycodeDomain}`);
// Decode a Punycode domain name back to Unicode
const decodedUnicodeDomain = punycode.toUnicode(punycodeDomain);
console.log(`Decoded Unicode domain: ${decodedUnicodeDomain}`);
// Encode/decode individual string parts
const unicodePart = '☃-⌘';
const punycodePart = punycode.encode(unicodePart);
console.log(`Unicode part: ${unicodePart}`);
console.log(`Punycode part: ${punycodePart}`);
const decodedUnicodePart = punycode.decode(punycodePart);
console.log(`Decoded Unicode part: ${decodedUnicodePart}`);