如何以编程方式检测nodejs中的调试模式? [英] How to programmatically detect debug mode in nodejs?

查看:31
本文介绍了如何以编程方式检测nodejs中的调试模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到其他平台/语言问过这个问题 - 有什么想法吗?我想做类似的事情:

if (detectDebug()){require('tty').setRawMode(true);var stdin = process.openStdin();stdin.on('keypress', function (chunk, key) {做工作();}}别的{做工作();}

我希望能够在调试时切换键盘输入作为脚本的开始,这样我就可以有时间启动 chrome 来监听我的节点检查器端口.

***快速更新 - 我猜我实际上可以使用process.argv"来检测是否传入了 --debug.这是最好/正确的方法吗?

解决方案

NodeJS 在调试模式下运行时创建一个 v8debug 全局对象:node debug script.js

因此,可能的解决方案是:

var debug = typeof v8debug === 'object';

对于我的用例,我使用它是因为我想避免传递环境变量.我的主节点进程启动子节点进程,我想要一个 node debug mainScript.js 来触发子进程的调试模式(同样,不将 env 变量传递给子进程)

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();
}

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.

***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 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';

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆