调试node.js代码时,有没有办法在Visual Studio Code(版本1.10.2)的调试控制台中显示颜色(如在终端中)?
我想到目前为止最好的方法是将调试输出放入备用目标:
在启动配置属性中,console
设置可以设置为以下之一:internalConsole
(默认情况下,内置调试控制台)externalTerminal
(外部cmd窗口)或integratedTerminal
(VS代码终端)。
外部终端命令行还可以在VS代码设置中根据以下某项指定:terminal.external.windowsExec
,terminal.external.osxExec
和terminal.external.linuxExec
,默认为您的默认操作系统终端。
来源:VS Code文档,例如node.js:https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-attributes
为获得最佳效果,请避免打开控制台。这是我使用Jest调试当前文件的配置:
{
"type": "node",
"request": "launch",
"name": "Jest Test (current)",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"--config=jest.config.json",
"--runInBand",
"${relativeFile}",
],
// The vscode console does not support colors used by Jest output
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
}
要从visual studio中的nodejs输出彩色消息,您可以在console.log方法中使用格式化消息。例如 :
console.log('\u001b[' + 32 + 'm' + 'hello stack' + '\u001b[0m')
在Mocha实施。 32是颜色代码。您可以在他们的回购中找到其他颜色代码和用法样本:https://github.com/mochajs/mocha/blob/9e95d36e4b715380cef573014dec852bded3f8e1/lib/reporters/base.js#L48
或者作为日志库,您可以使用,例如pinojs + pino-pretty模块,您的日志输出将显示如下:
我的设置,彩色步骤:
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"console": "integratedTerminal",
"name": "Cucumber",
"program": "${workspaceFolder}/tests/cucumberjs/node_modules/cucumber/bin/cucumber-js",
"cwd": "${workspaceFolder}/tests/cucumberjs",
"args": [
"--tags=@luke",
"--format=node_modules/cucumber-pretty"
],
"outputCapture": "std"
}
]
}