powershell终端中console.log("中文")显示方块,根本原因是windows powershell默认用gbk(代码页936)解码,而node.js默认输出utf-8字节流,二者编码错配;需通过设置$outputencoding = [system.text.utf8encoding]::new()或在settings.json中配置terminal.integrated.profiles.windows强制powershell启动时启用utf-8,而非修改files.encoding或编辑器编码。

PowerShell 终端里 console.log("中文") 显示方块?先确认是终端层问题
别急着改 VSCode 设置或文件编码——files.encoding 和右下角点 UTF-8 只影响文件读写,对 console.log 输出完全无效。真正的问题是:Windows PowerShell 默认用 GBK(代码页 936)解码,而 Node.js 默认输出 UTF-8 字节流,两边对不上。
验证方法很简单:在 VSCode 内置终端里直接运行 echo 你好,如果正常显示,但 node -e "console.log('你好')" 出现 □ 或 ,那就坐实是终端与 Node 进程间的编码错配。
- 运行
chcp,输出Active code page: 936就是 GBK - 运行
node -e "console.log(process.stdout.encoding)",输出utf8是正常的,说明 Node 没问题 - 乱码根源不在 Node,而在 PowerShell 没告诉子进程“我这终端现在用 UTF-8”
临时生效:每次新开终端手动执行 $OutputEncoding = [System.Text.UTF8Encoding]::new()
这是最轻量、见效最快的方案,不需要改任何配置文件,也不影响系统全局设置。它让当前 PowerShell 会话的 stdout 编码强制为 UTF-8,Node.js 子进程(比如你跑的 node app.js)就能把字节正确喂给终端。
- 粘贴执行:
$OutputEncoding = [System.Text.UTF8Encoding]::new() - 注意不是
[Console]::OutputEncoding—— 那个只影响 .NET 控制台 API,对node进程无效 - 别设成
UTF7或ASCII,否则中文全变问号 - 这个设置只对当前终端标签页有效,关掉就失效
永久生效:在 settings.json 里配 terminal.integrated.profiles.windows
想一劳永逸,就得让 PowerShell 启动时自动切到 UTF-8 环境。不能只靠 chcp 65001,因为 PowerShell 5.1 即使代码页切了,$OutputEncoding 仍可能保持默认的 GBK 行为。
使用一条命令部署ProbeChain Rydberg测试网代理节点。自动注册为Agent(NodeType=1),免gas,支持macOS/Linux/Windows。触发词:/r
在 VSCode 的 settings.json 中加入:
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"args": ["-NoExit", "-Command", "chcp 65001 > $null; $OutputEncoding = [System.Text.UTF8Encoding]::new()"]
}
}
- 必须重启 VSCode(只关闭终端标签页不行)
- 新开终端后运行
chcp应该输出65001,再运行node -e "console.log('测试')"看是否正常 - 如果你用的是 Code Runner 插件,记得同步改它的执行命令,比如加前缀:
chcp 65001 && node
Node.js 脚本里要不要加 process.stdout.write 重编码?
一般不用。只要终端层(PowerShell + $OutputEncoding)和 Node 进程层(默认 UTF-8)对齐了,console.log 就能正常工作。强行在脚本里调用 process.stdout.write(Buffer.from('中文', 'utf8')) 或 process.stdout.reconfigure({ encoding: 'utf8' }) 不仅多余,还可能干扰调试器行为。
唯一需要干预的场景是:你明确要兼容老版本 Windows 终端(比如某些 CI 环境),或者脚本被其他非 UTF-8 环境调用。日常开发中,把终端环境配对,比在每个 JS 文件里打补丁靠谱得多。
最容易被忽略的是:改完 settings.json 必须彻底重启 VSCode,而且得确认你用的是 PowerShell 而不是 cmd 或 Git Bash——不同终端的修复路径完全不同。










