如何在nodejs中以编程方式检测调试模式? [英] How to programmatically detect debug mode in nodejs?
问题描述
我已经看到这个问题询问其他平台/语言 - 任何想法?我想做一些类似的东西:
I've seen this question asked of other platform/languages - any ideas? I'd like to do something like:
if (detectDebug())
{
require('tty').setRawMode(true);
var stdin = process.openStdin();
stdin.on('keypress', function (chunk, key) {
DoWork();
}
}
else
{
DoWork();
}
我想要在调试时切换键盘输入作为脚本的开始,以便我可以稍等一下触发chrome来聆听我的节点检查器端口。
I'd like to be able to toggle keyboard input as a start for the script when debugging so that I can have a moment to fire up chrome to listen to my node-inspector port.
***快速更新 - 我猜我实际上可以使用process.argv来检测--debug是否被传入。这是最好的/正确的方式吗?
***Quick update - I'm guessing I can actually use "process.argv" to detect if --debug was passed in. Is this the best/right way?
推荐答案
NodeJS在调试模式下运行时创建一个 v8debug
全局对象: node debug script.js
NodeJS creates a v8debug
global object when running in debug mode: node debug script.js
所以,一个可能的解决方案是:
So, a possible solution would be:
var debug = typeof v8debug === 'object';
对于我的用例,我使用它是因为我我想要避免传递环境变量我的主节点进程启动子节点进程,我想要一个节点调试mainScript.js
还可以触发儿童的调试模式(再次,没有向子进程传递env变量)
For my use case, I use it because I want to avoid passing environment variables. My main node process starts child node processes and I want a node debug mainScript.js
to trigger debug mode for children as well (again, without passing env variables to child processes)
这篇关于如何在nodejs中以编程方式检测调试模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!