iconv-lite - Character Encoding Conversion

0.7.2 · active · verified Sun Apr 19

iconv-lite is a pure JavaScript library for converting character encodings without relying on native Node.js bindings or external dependencies for core functionality. It provides a robust solution for handling various text encodings, making it suitable for applications that need to process files, network streams, or user input in different character sets. The current stable version is `0.7.2`, with `v1.0.0-alpha.1` being an actively developed upcoming major release introducing significant breaking changes. While there isn't a strict release cadence, the project sees regular maintenance and improvements, including recent type definition enhancements and bug fixes. Its key differentiator is its pure JavaScript implementation, which avoids installation complexities associated with native modules like `node-iconv`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to encode a JavaScript string into a Buffer using different character sets (UTF-8, Windows-1251) and then decode those Buffers back into strings. It also shows how to check for supported encodings.

import iconv from 'iconv-lite';

// --- Encoding a string to a Buffer ---
const inputString = 'Hello, world! Это тест.';
const utf8Buffer = iconv.encode(inputString, 'utf8');
console.log('UTF-8 Buffer:', utf8Buffer.toString('hex'));

const win1251Buffer = iconv.encode(inputString, 'windows-1251');
console.log('Windows-1251 Buffer:', win1251Buffer.toString('hex'));

// --- Decoding a Buffer to a string ---
const decodedUtf8 = iconv.decode(utf8Buffer, 'utf8');
console.log('Decoded UTF-8:', decodedUtf8);

const decodedWin1251 = iconv.decode(win1251Buffer, 'windows-1251');
console.log('Decoded Windows-1251:', decodedWin1251);

// --- Check if an encoding is supported ---
const supportsUtf8 = iconv.encodingExists('utf8');
console.log('Supports UTF-8:', supportsUtf8);

const supportsFoo = iconv.encodingExists('foo-bar-encoding');
console.log('Supports foo-bar-encoding:', supportsFoo);

view raw JSON →