Node Webkit - Options for encoding in writeFileSync not work?

618 views Asked by At

I am a beginner.

I want to write an HTML file containing Chinese characters in UTF8 encoding. I found the following code from the internet.

fs.writeFileSync(target, generateHTML(), "utf8");

Though when I read the documentation, it didn't explicitly say I can add an encoding flag. generateHTML() returns the HTML string.

However, the following characters "返回" became something like this: "活動" in the file. I am sure that it is an encoding error.

Am I using the wrong function? How can I write a file in sync using proper utf-8?


EDIT

The fs.writeFileSync worked alone well, but not when the content is returned as a function. Please try this:

function generateHTML(){return "返回"}
fs.writeFileSync("index.html", generateHTML(), "utf8");

The file contains ԏ in utf-8 format, which is not the intended content.


EDIT

I tested this with my installed node.js version and it was working properly. It seems to have something to do with node webkit.

I'll include the version later. The time here is not convenient for me.

2

There are 2 answers

0
Daniel Cheung On BEST ANSWER

The solution is to set the working node-webkit application page to have a utf-8 encoding. I have to add this to the page:

<meta charset="utf-8">

It turns out to be something very simple yet I've missed.

3
michelem On

This is working well for me:

fs.writeFileSync('test.html', '返回', 'utf8');