How to fix RangeError: Unknown encoding 'utf-99' (invalid buffer encoding) in Node.js
This error occurs when you attempt to use an unsupported character encoding with Node.js Buffer operations. Node.js only supports a limited set of standard encodings like utf8, ascii, hex, and base64. Using an invalid encoding name causes a RangeError.
Quick fix
Fixes ERR_UNKNOWN_ENCODING
// Wrong
const buf = Buffer.from('hello', 'utf-99');
// Correct
const buf = Buffer.from('hello', 'utf8');- Identify the invalid encoding name
- Verify against valid Node.js encodings
- Replace with the correct encoding
- Check for dynamic encoding values
// Wrongconst buf = Buffer.from('hello', 'utf-99');// Correctconst buf = Buffer.from('hello', 'utf8');RangeError: Unknown encoding 'utf-99' (invalid buffer encoding)
What this error means
When you pass an encoding parameter to a Buffer method (like Buffer.from(), toString(), or write operations), Node.js validates the encoding name against its whitelist of supported encodings. If the encoding name doesn't match any supported encoding, Node.js throws this RangeError. The error typically includes the invalid encoding name you provided, making it easy to identify the typo or unsupported format.
Typical symptoms
- RangeError thrown when calling Buffer.from() with an encoding parameter
- toString() calls fail when specifying an invalid encoding
- Application crashes when writing to files or streams with wrong encoding
- Parsing or file I/O operations that specify unsupported encodings fail immediately
Common causes
- Typo in encoding name (e.g., 'utf-99' instead of 'utf8' or 'utf-8')
- Using a non-standard encoding that Node.js doesn't support natively
- Referencing an encoding from another language or framework that Node.js hasn't implemented
- Configuration or environment variables containing invalid encoding values
- Legacy code written for a different runtime or with deprecated encoding names
How to fix "RangeError: Unknown encoding 'utf-99' (invalid buffer encoding)"
Check the error message carefully. It will display the exact encoding string you used. For example, if the error is Unknown encoding 'utf-99', then 'utf-99' is what you passed as the encoding parameter.
Node.js supports these encodings:
- utf8 (or utf-8) - Default, standard UTF-8
- ascii - 7-bit ASCII only
- hex - Hexadecimal format
- base64 - Base64 encoding
- latin1 (or binary) - ISO-8859-1
- utf16le (or utf-16le) - UTF-16 Little Endian
- ucs2 (or ucs-2) - Alias for utf16le
Check if your intended encoding is in this list.
Update your code to use the correct encoding name from the supported list. Most commonly:
// Wrong
const buf = Buffer.from('hello', 'utf-99');
// Correct
const buf = Buffer.from('hello', 'utf8');// Wrong
const str = buf.toString('UTF-8'); // Wrong case/format
// Correct
const str = buf.toString('utf8');If encoding comes from configuration, environment variables, or user input, validate it before using:
const Buffer = require('buffer').Buffer;
const encoding = process.env.FILE_ENCODING || 'utf8';
// Validate the encoding
if (!Buffer.isEncoding(encoding)) {
throw new Error(`Invalid encoding: ${encoding}`);
}
const buf = Buffer.from(data, encoding);Before using an encoding from an untrusted source, validate it with the built-in Buffer.isEncoding() method:
const Buffer = require('buffer').Buffer;
function safeBufferFrom(str, encoding = 'utf8') {
if (!Buffer.isEncoding(encoding)) {
console.error(`Unknown encoding: ${encoding}. Using utf8 instead.`);
encoding = 'utf8';
}
return Buffer.from(str, encoding);
}
const buf = safeBufferFrom('hello', 'ascii'); // Works
const buf2 = safeBufferFrom('hello', 'utf-99'); // Falls back to utf8Advanced notes
If you need to work with encodings beyond Node.js's built-in support, consider using third-party libraries like iconv-lite or encoding which can handle legacy character sets and codepages. For example, to support Windows-1252 or Big5 encoding: npm install iconv-lite and then use iconv.encode(str, 'windows1252') or iconv.decode(buffer, 'big5'). Always validate encoding values programmatically when they come from external sources (config files, environment variables, user input) to provide clear error messages and fallback defaults.
Frequently asked questions
- What does "ERR_UNKNOWN_ENCODING" mean?
- js validates the encoding name against its whitelist of supported encodings. js throws this RangeError. The error typically includes the invalid encoding name you provided, making it easy to identify the typo or unsupported format.
- How do I fix "ERR_UNKNOWN_ENCODING"?
- This error occurs when you attempt to use an unsupported character encoding with Node.js Buffer operations. Node.js only supports a limited set of standard encodings like utf8, ascii, hex, and base64. Using an invalid encoding name causes a RangeError.
- What causes "ERR_UNKNOWN_ENCODING"?
- Typo in encoding name (e.g., 'utf-99' instead of 'utf8' or 'utf-8') Using a non-standard encoding that Node.js doesn't support natively Referencing an encoding from another language or framework that Node.js hasn't implemented
- Is the "ERR_UNKNOWN_ENCODING" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- ๐Node.js Official Documentation
- ๐Node.js Buffer API - Character Encodings(Official Docs)
- ๐
- ๐
- ๐