m.zaki
m.zaki

Reputation: 47

Arabic text encoding in node

I am creating a file from node.js and inserting Arabic text in it but the output is garbage text instead of Arabic text

    var fs = require('fs');
    fs.writeFile('message.txt', 'تمكين و تطوير جمعيات إسكان تعاونية لبناء المساك')

Output Image

Upvotes: 2

Views: 2925

Answers (4)

m.zaki
m.zaki

Reputation: 47

Figured out the problem, I was using webstorm as IDE and create js file from webstorm by default its create file in ancsi encode i just change the encode of my js file and its working fine now anyways Thanks for you quick help

Upvotes: 1

Tareq
Tareq

Reputation: 5363

you can add encoding in options as below

fs.writeFile('message.txt', 'تمكين و تطوير جمعيات إسكان تعاونية لبناء المساك', {encoding: 'utf8'});

Also, are you sure that notepad showing arabic text right in the first place ? try to open it with web browser (chrome, FF) and set encoding to UTF-8 (usually found under developer menu)

Reference

Upvotes: 0

Christiaan
Christiaan

Reputation: 848

When running the same js sample and then running cat message.txt I get the Arabic output. Running file message.txt returns UTF-8 Unicode text, with no line terminators which is also expected.

My guess is this has to do with notepad not supporting utf8. When I open the file using gedit on Ubuntu it shows the Arabic just fine.

Upvotes: 0

Fka
Fka

Reputation: 6234

Have you tried with options additional parameter?

fs.writeFile('message.txt', 'تمكين و تطوير جمعيات إسكان تعاونية لبناء المساك', 'utf8', function(err, data) { 
        /// ... 
});

Upvotes: 0

Related Questions