Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print a utf8 string in the console using nodejs

I want to print a string in Bulgarian in the console using nodejs. I have a index.js file which does:

console.log('Текст на български.');

When I run it in the console with: node index.js It prints only - '??????????' I am using Webstorm and I have set the IDE and Project File Encoding settings to utf8. I also tried:

var strInBulgarian = 'Текст на български.';
console.log(strInBulgarian.toString('utf8'));

The same result. If I run the following in nodejs REPL I get the correct result:

console.log('Текст на български.');

Which leads me to beleive that the reason I get '???????' is because of the file encoding, but it is set to utf8. Any ideas? Here is a screenshot from the settings in Webstorm. enter image description here

Hmm now that I changed all of the settings to UTF8 the text that is in Bulgarian in my comments changed to '?????' and it was fine before that. Someting is definitely not right. When I make a index.js file from Notepad++ and set the encoding to utf8 I have no trouble there. Something is not right with the settings of Webstorm.

like image 871
D_Andreev Avatar asked Nov 01 '22 00:11

D_Andreev


1 Answers

Webstorm's "Project Encoding" setting seems to only apply to newly added files. Judging from your screenshot (see lower-right corner), your individual files still use Windows-1252. You need to manually make Webstorm interpret each file as UTF-8.

Either through the dropdown menu on the bottom right of your window, after opening the file in question:

screen 1

Or through the 'File Encodings' settings window itself:

screen 2


Another possibility would be to directly mess with the .idea/encodings.xml file in your project dir, but I won't go into details there.

like image 93
Aaa Avatar answered Nov 15 '22 04:11

Aaa