0

Create a simple JS:

console.log('English');
console.log('中文');

And then in Powershell, run it with node.js:

PS D:\temp\1> node .\a.js
English
中文

Now, print it with echo, or redirect it to a file, or use tee:

PS D:\temp\1> node .\a.js | echo
English
涓枃

PS D:\temp\1> node .\a.js > log.txt

Both the print (above) and the log.txt file are garbled. I tried it on a Chinese Windows and English Windows, same result.

How do I fix it?

By the way, Python works fine:

print('Print some English')
print('Print some Chinese: 中文中文中文')
PS D:\temp\1> python .\a.py
Print some English
Print some Chinese: 中文中文中文
PS D:\temp\1> python .\a.py | echo
Print some English
Print some Chinese: 中文中文中文
fireattack
  • 101
  • 1
  • 7
  • 1
    In short: While the output from external programs may _print_ OK to the _console_, when it comes to _capturing the output in a variable or redirecting it_, PowerShell decodes the output into .NET strings (first), using the character encoding stored in `[Console]::OutputEncoding`. Therefore, you may have to (temporarily) set it to the encoding used by the external program. E.g., for programs that output UTF-8, such as `node`: `[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()`. See [this answer](https://stackoverflow.com/a/58438716/45375) to the linked duplicate for details. – mklement0 Dec 13 '21 at 19:26

0 Answers0