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

28,546

Solution 1

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) {});

Solution 2

I elaborated on this answer in detail on this answer - Adding UTF-8 BOM to string/Blob.

This is a very sparse answer that doesn't go into detail as to why this works. The FEFF bytes are actually the UTF16LE BOM, so the previous answer is confusing.

Share:
28,546
Admin
Author by

Admin

Updated on July 18, 2022

Comments

  • Admin
    Admin almost 2 years

    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 ?