fs.readFileSync("./bank/"+client,'cp1251'); is throwing throw new ERR_INVALID_OPT_VALUE_ENCODING

1k Views Asked by At

I'm trying read from a file (The file is in Bulgarian) and with utf 8 it returns nonsense characters so I tried cp1251 but it throws: ERR_INVALID_OPT_VALUE_ENCODING.

var str = fs.readFileSync("./bank1/"+client,'cp1251');
2

There are 2 best solutions below

0
Konard On BEST ANSWER

Install iconv:

npm install iconv

Use the code to read file encoded in cp1251:

const fs = require('fs');
var Iconv = require('iconv').Iconv;
var iconv = new Iconv('cp1251', 'utf-8');
const encoded = fs.readFileSync("./bank1/"+client);
const decoded = iconv.convert(encoded).toString();
1
Rafael Rocha On

You should add the encoding option as an object like so:

var str = fs.readFileSync("./bank1/"+client, { encoding: 'cp1251' });

Hope it helps.