1

I keep running in to encoding issues on windows.

var content = fs.readFileSync("file.txt", 'utf8').toString();
console.log(content)

Where file.txt was created in notepad with the text "123". The output of the above is

??1 2 3

and not "123" as I would expect. If I read on windows a text file created on a Mac, it works fine. So it is just windows created files causing me the issue.

I've hunted with web with no luck, including this post node.js readfile error with utf8 encoded file on windows

I also experimented with toString('utf8') with no luck.

Later on in code I construct a JSON string using this input, and it ends up as

??1\u00002\u00003\u0000�

Yuk.

What am I doing wrong?

Thanks Gary.

Community
  • 1
  • 1
Gary
  • 216
  • 3
  • 8
  • 1
    possible duplicate of [node.js readfile error with utf8 encoded file on windows](http://stackoverflow.com/questions/24356713/node-js-readfile-error-with-utf8-encoded-file-on-windows) – keithmo Sep 23 '15 at 06:46
  • Check the link from the comment above, because at least I don't experiment this issue. – blfuentes Sep 23 '15 at 06:50

1 Answers1

2

The issue you've linked to has the answer you want. Notepad is saving the file in a 2-byte encoding with a byte-order mark (BOM). Have a look at the answers to this question.

Community
  • 1
  • 1
Alex Taylor
  • 8,343
  • 4
  • 25
  • 40