nickj
nickj

Reputation: 360

How to set file encoding in Node.js

I have some issues in set encoding in node.js. I tried to set encoding to utf8 using writeFile function. But no results. current file I want to set encoding using node.js was encoded in unicode.

Upvotes: 5

Views: 19389

Answers (1)

Sibiraj
Sibiraj

Reputation: 4756

By default, the fs module will write files with an encoding of 'utf8'. UTF-8 is an encoding commonly used in web pages and other documents. File encoding refers to the character set that is used for the contents of the file. Commonly used encodings are 'utf8', 'ascii', 'binary', 'hex', 'base64' and 'utf16le'.

Source from stackabuse.

To write file with encoding

If options is a string, then it specifies the encoding. Example:

fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);

or is options is an object then,

fs.writeFile('message.txt', 'Hello Node.js', {encoding: 'utf8'}, callback);

Check here for more information

Upvotes: 10

Related Questions