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 ?
The fs. writeFileSync() creates a new file if the specified file does not exist.
The fs. writeFileSync() is a synchronous method & creates a new file if the specified file does not exist while fs. writeFile() is an asynchronous method.
The simplest way, and often the most appropriate, is to use the writeFile method in the fs module. This allows you to write to a specified file path, with asynchronous behavior, and creates a new file or replaces one if it exists at the path.
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) {});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With