This error occurs when using latin1 (ISO-8859-1) encoding in Node.js Buffer operations on versions that don't support it. The fix is to either upgrade Node.js to v6.4.0 or later, or use an alternative encoding method.
This error indicates that your Node.js version doesn't recognize 'latin1' as a valid encoding for Buffer operations. Latin1 (also called ISO-8859-1) is a single-byte character encoding that supports Unicode characters from U+0000 to U+00FF. The 'binary' encoding is an alias for latin1. This error typically appears when you try to use latin1 in Buffer methods like buffer.toString('latin1') or Buffer.from(data, 'latin1') on older Node.js versions that predate native latin1 support.
First, verify which version of Node.js you're running:
node --versionIf the version is earlier than v6.4.0, you need to upgrade.
Update Node.js to version 6.4.0 or newer where latin1 encoding is natively supported:
Using nvm (Node Version Manager):
nvm install 18 # or any version >= 6.4.0
nvm use 18Using Homebrew (macOS):
brew upgrade nodeUsing apt (Ubuntu/Debian):
sudo apt update
sudo apt upgrade nodejsVerify the upgrade:
node --version # Should show v6.4.0 or higherOnce upgraded, you can safely use 'latin1' in your Buffer operations:
Reading with latin1 encoding:
const buffer = Buffer.from('Hello', 'latin1');
const str = buffer.toString('latin1');
console.log(str); // 'Hello'Converting between encodings:
// Convert UTF-8 string to latin1 buffer
const utf8String = 'café';
const latin1Buffer = Buffer.from(utf8String, 'utf8');
// Note: latin1 only supports characters U+0000 to U+00FF
// Characters outside this range will be replaced with '?'
const latin1String = latin1Buffer.toString('latin1');If you want to be explicit or need compatibility, you can use 'binary' instead of 'latin1' - they are equivalent:
// These are identical
const buf1 = Buffer.from(data, 'latin1');
const buf2 = Buffer.from(data, 'binary');
console.log(buf1.toString('latin1'));
console.log(buf2.toString('binary'));If you absolutely cannot upgrade Node.js, use the third-party iconv-lite library to handle latin1 encoding:
Install iconv-lite:
npm install iconv-liteUse it for latin1 conversion:
const iconv = require('iconv-lite');
// Encoding to latin1
const latin1Buffer = iconv.encode('Hello café', 'latin1');
// Decoding from latin1
const str = iconv.decode(latin1Buffer, 'latin1');
console.log(str); // 'Hello caf?' (é becomes ? in latin1)Latin1 character limitations: Latin1 only supports the Unicode range U+0000 to U+00FF (256 characters). If your data contains characters outside this range (like emoji, CJK characters, or most diacritics), they will be replaced with '?' when encoded as latin1. For multilingual content, use UTF-8 instead.
Binary vs Latin1: In Node.js, 'binary' and 'latin1' are aliases for the same encoding. The 'binary' name is a legacy term; 'latin1' is more explicit about what's actually being used.
Performance consideration: Buffer encoding/decoding is very fast, but if you're doing high-volume conversions between latin1 and UTF-8, consider keeping data in UTF-8 internally and only using latin1 at boundaries (file I/O, network, legacy systems).
When to use latin1: Primarily for reading legacy files or communicating with older systems that use ISO-8859-1. Modern web applications and APIs should use UTF-8 instead.
Error: Listener already called (once event already fired)
EventEmitter listener already called with once()
Error: EACCES: permission denied, open '/root/file.txt'
EACCES: permission denied
Error: Invalid encoding specified (stream encoding not supported)
How to fix Invalid encoding error in Node.js readable streams
Error: EINVAL: invalid argument, open
EINVAL: invalid argument, open
TypeError: readableLength must be a positive integer (stream config)
TypeError: readableLength must be a positive integer in Node.js streams