How to fix Error: Buffer encoding mismatch (reading with wrong encoding) in Node.js

Node.jsINTERMEDIATEMEDIUMUpdated Dec 9, 2025· 154 views

This error occurs when a Buffer is converted to a string using an encoding that differs from the original encoding used to create the Buffer. Node.js supports specific encodings (utf8, hex, base64, etc.), and mismatches produce garbled output or replacement characters.

Quick fix

Fixes Error: Buffer encoding mismatch (reading with wrong encoding)

const encoding = 'latin1';
if (Buffer.isEncoding(encoding)) {
  console.log('Supported encoding');
} else {
  console.log('Unsupported - need external library');
}
  1. Identify the correct source encoding
  2. Specify the correct encoding when reading files
  3. Use the correct toString() encoding parameter
  4. Install iconv-lite for extended encoding support
node.js — error
const encoding = 'latin1';
if (Buffer.isEncoding(encoding)) {
console.log('Supported encoding');
} else {
console.log('Unsupported - need external library');
}
Error: Buffer encoding mismatch (reading with wrong encoding)

What this error means

This error indicates that a Buffer object is being decoded to a string using an incorrect character encoding. In Node.js, Buffers represent raw binary data and must be explicitly converted to strings using a character encoding like 'utf8', 'hex', 'base64', 'ascii', or 'utf16le'. When the encoding specified during the toString() conversion doesn't match the encoding used when the data was originally encoded, you get either garbled text, Unicode replacement characters (�), or in some cases an explicit error. Node.js only supports a limited set of encodings natively. If you attempt to use an unsupported encoding like 'latin1', 'ISO-8859-1', or 'shift_jis' without additional libraries, Node.js will either throw an "Unknown encoding" error or fall back to UTF-8, causing the mismatch. The most common scenario is reading file data with fs.readFile() and assuming UTF-8 encoding when the file was actually saved in a different encoding, or receiving binary data (like images or encrypted content) and attempting to convert it to a string without specifying the correct encoding format.

Typical symptoms

  • Garbled or corrupted text output with strange characters
  • Unicode replacement characters (�) appearing in strings
  • Error message: "Unknown encoding" when using unsupported encodings
  • File contents appear correct in editor but garbled when read by Node.js
  • Base64 or hex strings produce binary garbage when decoded incorrectly
  • Non-English characters (Chinese, Japanese, Cyrillic) display as question marks

Common causes

  1. Reading a file with fs.readFile() without specifying encoding, defaulting to UTF-8 when file uses different encoding
  2. Converting a base64 or hex-encoded Buffer using toString() without specifying the encoding parameter
  3. Using an unsupported encoding name like "latin1", "ISO-8859-1", or "shift_jis" without the iconv-lite library
  4. Mixing encoding types - encoding data with one format (e.g., base64) then decoding with another (e.g., utf8)
  5. Receiving binary data from network requests and attempting to convert to string without proper encoding
  6. File saved with Windows-1252 or other legacy encodings but read as UTF-8

How to fix "Error: Buffer encoding mismatch (reading with wrong encoding)"

1Identify the correct source encoding

First, determine what encoding your data actually uses. For files, check the file properties or how it was originally saved. For network data, check response headers or API documentation.

Common encodings:
- Text files: usually 'utf8'
- Binary data (images, PDFs): should not be converted to string
- Base64 encoded data: use 'base64'
- Hexadecimal data: use 'hex'

You can verify supported encodings:

javascript
const encoding = 'latin1';
if (Buffer.isEncoding(encoding)) {
  console.log('Supported encoding');
} else {
  console.log('Unsupported - need external library');
}
2Specify the correct encoding when reading files

When using fs.readFile() or fs.readFileSync(), always explicitly specify the encoding if you want a string:

javascript
const fs = require('fs');

// Correct - specify encoding
fs.readFile('data.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data); // String with correct encoding
});

// For non-UTF8 files with iconv-lite
const iconv = require('iconv-lite');
fs.readFile('legacy.txt', (err, buffer) => {
  if (err) throw err;
  const text = iconv.decode(buffer, 'windows-1252');
  console.log(text);
});

If you omit the encoding parameter, you'll get a Buffer object instead of a string, which is safer for binary data.

3Use the correct toString() encoding parameter

When converting Buffers to strings manually, always specify the encoding:

javascript
// Wrong - defaults to utf8
const buffer = Buffer.from('aGVsbG8gd29ybGQ=', 'base64');
const wrong = buffer.toString(); // Garbled output

// Correct - specify encoding
const buffer = Buffer.from('aGVsbG8gd29ybGQ=', 'base64');
const correct = buffer.toString('utf8'); // "hello world"

// For hex encoding
const hexBuffer = Buffer.from('68656c6c6f', 'hex');
const text = hexBuffer.toString('utf8'); // "hello"

Match the encoding to your data type: 'utf8' for text, 'base64' for base64 data, 'hex' for hexadecimal, etc.

4Install iconv-lite for extended encoding support

If you need encodings beyond Node.js native support (like latin1, shift_jis, ISO-8859-1), use the iconv-lite package:

bash
npm install iconv-lite

Then use it to decode Buffers:

javascript
const iconv = require('iconv-lite');

// Reading a file with legacy encoding
const fs = require('fs');
const buffer = fs.readFileSync('legacy.txt');
const text = iconv.decode(buffer, 'ISO-8859-1');
console.log(text);

// Encoding text to a specific format
const encoded = iconv.encode('Hello', 'win1252');
fs.writeFileSync('output.txt', encoded);

The iconv-lite library supports 450+ encodings and is widely used in the Node.js ecosystem.

5Verify encoding matches between encode and decode operations

Ensure consistency when encoding and decoding data:

javascript
// Correct - same encoding throughout
const original = 'Hello World';
const encoded = Buffer.from(original, 'utf8');
const decoded = encoded.toString('utf8');
console.log(decoded === original); // true

// Wrong - encoding mismatch
const text = 'Hello';
const buffer = Buffer.from(text, 'utf8');
const wrong = buffer.toString('base64'); // "SGVsbG8="
const garbled = Buffer.from(wrong, 'utf8').toString('utf8'); // Wrong!

// Correct round-trip
const correctDecoded = Buffer.from(buffer.toString('base64'), 'base64').toString('utf8');
console.log(correctDecoded === text); // true

Advanced notes

Binary Data Handling: Never convert binary data (images, PDFs, executables) to strings. Always keep them as Buffers and write them directly to files or streams. Converting binary data to strings with any encoding will corrupt the data.

UTF-8 Validation: When decoding Buffers that may contain invalid UTF-8 sequences, Node.js replaces invalid bytes with the Unicode replacement character U+FFFD (�). This is silent behavior - no error is thrown. If you see replacement characters, your data likely wasn't UTF-8 encoded.

Default Encoding Behavior: If you call Buffer.toString() without arguments, it defaults to 'utf8'. Similarly, fs.readFile() without an encoding option returns a Buffer, not a string. Being explicit about encodings prevents subtle bugs.

Encoding Case Sensitivity: Node.js accepts encoding strings in any case: 'utf8', 'UTF8', 'Utf8', 'UTF-8' are all valid and equivalent. However, for consistency and standards compliance, use lowercase versions.

BOM (Byte Order Mark): Some UTF-8 files include a BOM (EF BB BF bytes at the start). Node.js doesn't automatically strip this. If you see weird characters at the start of files, check for and remove the BOM: text.replace(/^\uFEFF/, '').

Stream Encoding: When working with streams, set encoding on the stream itself rather than converting chunks manually: readStream.setEncoding('utf8'). This handles multi-byte characters that might be split across chunk boundaries.

Frequently asked questions

What does "Error: Buffer encoding mismatch (reading with wrong encoding)" mean?
This error indicates that a Buffer object is being decoded to a string using an incorrect character encoding. js, Buffers represent raw binary data and must be explicitly converted to strings using a character encoding like 'utf8', 'hex', 'base64', 'ascii', or 'utf16le'. When the encoding specified during the toString() conversion doesn't match the encoding used when the data was originally encoded, you get either garbled text, Unicode replacement characters (�), or in some cases an explicit error.
How do I fix "Error: Buffer encoding mismatch (reading with wrong encoding)"?
This error occurs when a Buffer is converted to a string using an encoding that differs from the original encoding used to create the Buffer. Node.js supports specific encodings (utf8, hex, base64, etc.), and mismatches produce garbled output or replacement characters.
What causes "Error: Buffer encoding mismatch (reading with wrong encoding)"?
Reading a file with fs.readFile() without specifying encoding, defaulting to UTF-8 when file uses different encoding Converting a base64 or hex-encoded Buffer using toString() without specifying the encoding parameter Using an unsupported encoding name like "latin1", "ISO-8859-1", or "shift_jis" without the iconv-lite library
Is the "Error: Buffer encoding mismatch (reading with wrong encoding)" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading