var fs = require('fs');
const detectCharacterEncoding = require('detect-character-encoding'); //npm install detect-character-encoding
var buffer = fs.readFileSync('filename.txt');
var originalEncoding = detectCharacterEncoding(buffer);
var file = fs.readFileSync('filename.txt', originalEncoding.encoding);
fs.writeFileSync('filename.txt', file, 'UTF-8');
How does this work?
When fs reads in a file, it converts it from the encoding of the file to the format that JS uses.
After that, when fs writes the file, it converts the string stored by JS to UTF-8 and writes it to file.