How can I print utf8 string to console using nodejs

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

console.log('  .');

      

When I run it in the console with: node index.js It only prints - '??????????' I am using Webstorm and I have set the IDE and project file encoding options to utf8. I've also tried:

var strInBulgarian = '  .';
console.log(strInBulgarian.toString('utf8'));

      

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

console.log('  .');

      

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

Now when I changed all settings to UTF8, the text that is in Bulgarian in my comments has changed to '?????' and it was good before. The socket is definitely wrong. When I make an index.js file from Notepad ++ and set the encoding to utf8 I have no problem. There is something wrong with the Webstorm settings.

+3


source to share


2 answers


The Webstorm "Project Encoding" setting seems to only apply to newly added files. Based on your screenshot (see bottom right corner), your personal files are still using Windows-1252 . You need to manually force Webstorm to interpret each file as UTF-8.

Or via the drop-down menu in the lower right corner of the window, after opening the file in question:

screen 1

Or through the "File Encoding" settings window:



screen 2


Another possibility is to link directly to a file .idea/encodings.xml

in the project directory, but I won't go into details there.

+2


source


If you are using the standard Windows command line, you need to make sure to use both the UTF8 ( chcp 65001

) codepage and Lucida Console instead of Raster Fonts.



After making these changes, the text will be displayed just right for me, both in the REPL and when executed from a file.

0


source







All Articles