gpt4 book ai didi

debugging - 从进程中将 NodeJs 应用程序切换到 Debug模式

转载 作者:搜寻专家 更新时间:2023-11-01 00:09:11 26 4
gpt4 key购买 nike

我有一个 NodeJs 模块“some-module”,我想在全局安装它,这样它就可以直接从命令行运行而无需 Node 可执行文件前缀。即:$> some-module [args] 我希望这些参数之一是 --debug。这样做的原因是我不想要求这个模块的用户将它安装到他们的本地目录只是为了运行 node --debug-brk node_modules/some-module/[path to entry point] [args ]

NodeJs 文档在调试的高级用法部分中说明 (http://nodemanual.org/latest/nodejs_ref_guide/debugging.node.js.html)

The V8 debugger can be enabled and accessed either by starting Node.js with the --debug command-line flag or by signaling an existing Node.js process with SIGUSR1.

我尝试这样做:

process.kill(process.pid, 'SIGUSR1');

产生错误的地方:

node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Unknown signal: SIGUSR1
at EventEmitter.kill (node.js:366:17)
at Object.<anonymous> (c:\dev\some-module\app.js:94:17)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Array.0 (module.js:479:10)
at EventEmitter._tickCallback (node.js:192:40)

我需要做什么才能让正在运行的进程切换到 Debug模式?

此外,我想使用 Node 检查器调试给定的应用程序。

最佳答案

我不太确定我是否理解你的问题,但是......

您可以全局安装一个应用使用 npm 让它在断点处停止。在 package.json 中放置:

...
"scripts": {"start": "node --debug-brk some-module.js"},
"bin" : { "some-module" : "./some-module.js" },
...

运行 npm start -g some-module 将在第一行中断。然后,您可以使用 Node 检查器进行调试。

关于从代码中停止的部分,node 有一个内置调试器(非常初级),但它允许此功能。如果您在代码中包含某处:

debugger;

然后运行:

node debug some-module.js 

它会在调试器中停在那里(注意:这与 node-inspector 不同,我不知道这是否可以通过 node-inspector 实现)。

不太了解您这样做的原因,但希望这对您有所帮助。

关于debugging - 从进程中将 NodeJs 应用程序切换到 Debug模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10013743/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com