NodeJs - how to make function fs.writeFile write with BOM?

user706355 picture user706355 · Dec 13, 2012 · Viewed 25.3k times · Source

I'm using nodeJS v0.8.6 and the native library fs. Here is my code :

var filesys = require('fs');
filesys.writeFile('test.txt', 'This is an example with accents : é è à ','utf8', function (err) {});

The problem is that it writes in utf8 without BOM (I use notepad++ to verify it) and it doesn't work in wordpad on Windows (the accents are not well displayed). The thing is that I need that file to be well read by womeone using wordpad.

How can I add the BOM to my file ?

Answer

Esailija picture Esailija · Dec 13, 2012

UTF-8 doesn't require a bom, but you can add it by yourself of course.

filesys.writeFile('test.txt', '\ufeffThis is an example with accents : é è à ','utf8', function (err) {});