简体   繁体   中英

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. 在此输入图像描述

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.

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:

屏幕1

Or through the 'File Encodings' settings window itself:

屏幕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.

If you're using the standard Windows command prompt, you need to make sure that both the UTF8 code page is being used ( chcp 65001 ) and that you are using the Lucida Console font instead of the Raster Fonts.

After making these changes, the text is shown just fine for me, both in the REPL and when executed from a file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM