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.
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.
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 utf8If 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.
Error: EMFILE: too many open files, watch
EMFILE: fs.watch() limit exceeded
Error: Middleware next() called multiple times (next() invoked twice)
Express middleware next() called multiple times
Error: Worker failed to initialize (worker startup error)
Worker failed to initialize in Node.js
Error: EMFILE: too many open files, open 'file.txt'
EMFILE: too many open files
Error: cluster.fork() failed (cannot create child process)
cluster.fork() failed - Cannot create child process