Node.js:如何附加到正在运行的进程并使用控制台调试服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13052548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js: How to attach to a running process and to debug the server with a console?
提问by Croplio
I use 'forever' to run my application. I want to attach to the running environment to inspect my application. So what can I do?
我使用“永远”来运行我的应用程序。我想附加到运行环境来检查我的应用程序。那我能做什么?
回答by Bill
From http://nodejs.org/api/debugger.html:
从http://nodejs.org/api/debugger.html:
Advanced Usage
The V8 debugger can be enabled and accessed either by starting Node with the --debug command-line flag or by signaling an existing Node process with SIGUSR1.
高级用法
可以通过使用 --debug 命令行标志启动 Node 或使用 SIGUSR1 向现有 Node 进程发送信号来启用和访问 V8 调试器。
Find the PID of your nodeprocess and then sending SIGUSR1should do the trick:
找到您的node进程的 PID,然后发送SIGUSR1应该可以解决问题:
kill -s SIGUSR1 nodejs-pid
Then run node-inspectorand browse to the URL it indicates. More in this tutorial.
然后运行node-inspector并浏览到它指示的 URL。本教程中的更多内容。
回答by Andrew Mao
Starting from Node 6.3, node has a built-in debugger that can be triggered (even in a production app) by doing:
从Node 6.3开始,node 有一个内置的调试器,可以通过执行以下操作来触发(甚至在生产应用程序中):
kill -USR1 <node-pid>
The node process will spit out something like this:
节点进程会吐出这样的东西:
Debugger listening on ws://127.0.0.1:9229/f3f6f226-7dbc-4009-95fa-d516ba132fbd
For help see https://nodejs.org/en/docs/inspector
- If you can access the server from a browser, you can use
chrome://inspectonhttp://host.domain:9229. If you cannot connect via a browser (e.g. the server is in a firewalled production cluster), you can activate a REPL to inspect over the command line:
node inspect -p <node-pid>- If you can't access the server from a browser, but you can SSH into that server, then setup SSH port forwarding (
ssh -nNTL 9229:localhost:9229 <your_host>) and you'll find your script underchrome://inspectafter a few seconds.
- 如果您可以从浏览器访问服务器,则可以使用
chrome://inspectonhttp://host.domain:9229。 如果您无法通过浏览器连接(例如,服务器位于受防火墙保护的生产集群中),您可以激活 REPL 以通过命令行进行检查:
node inspect -p <node-pid>- 如果您无法从浏览器访问服务器,但可以通过 SSH 连接到该服务器,然后设置 SSH 端口转发 (
ssh -nNTL 9229:localhost:9229 <your_host>),chrome://inspect几秒钟后您将在下面找到您的脚本。
Prior to this version, node-inspectorwas a separate tool for debugging Node processes. However, as documented on its own page, it is mostly deprecated as the now-bundled debugger is actively maintained and provides more advanced features. For more information on this change, see this thread.
在此版本之前,node-inspector是用于调试 Node 进程的单独工具。然而,正如它自己的页面所记录的那样,它大多已被弃用,因为现在捆绑的调试器得到积极维护并提供更高级的功能。有关此更改的更多信息,请参阅此线程。
回答by danmactough
You can add a REPLto your app. For example, if you add a REPL to listen on localhost port 5001, you start your app as usual and login with telnet: telnet localhost 5001. That will take you to a prompt where you can interact with your app directly.
您可以向您的应用程序添加REPL。例如,如果您添加一个 REPL 来侦听 localhost 端口 5001,则您可以像往常一样启动您的应用程序并使用 telnet: 登录telnet localhost 5001。这将带您进入一个提示,您可以在其中直接与您的应用程序交互。
Alternatively, if you need to your app to "pause" when it reaches a certain state, you need to add "debugger;" lines to areas of your code where you want those breakpoints, then start the app in debugmode.
或者,如果您需要让您的应用在达到某个状态时“暂停”,则需要添加“调试器”;行到您想要这些断点的代码区域,然后在调试模式下启动应用程序。
Hope that helps.
希望有帮助。
回答by zink02
For me, running node version 6.9.10 I had to:
对我来说,运行节点版本 6.9.10 我必须:
kill -USR1 <node-pid>
kill -USR1 <node-pid>
then
然后
node debug -p <node-pid>
node debug -p <node-pid>
the node inspect -p <node-pid>command failed for this version of node.
node inspect -p <node-pid>此版本节点的命令失败。
回答by Qwerty
Windows users
Windows 用户
If you are on Windows that doesn't support POSIX signals, you can use this workaround from anothercmd.
如果您使用的 Windows 不支持 POSIX 信号,则可以从另一个cmd.
node -e "process._debugProcess(PID)"
为一个 detailed guide详细指南,或在 VSCode 中设置调试,请按照以下简单步骤操作:
- In VSCode, open
launch.jsonconfiguration or create new by clicking on the wheel
(this is the debug view CtrlShiftD)
- 在 VSCode 中,
launch.json通过单击滚轮打开配置或创建新配置
(这是调试视图CtrlShiftD)
- The node will listen on port 9229 by default, so add this configuration:
- 节点默认会监听 9229 端口,所以添加这个配置:
{
"type": "node",
"request": "attach",
"name": "Attach to 9229",
"port": 9229
},
- Open Task Manager and locate the PID of your node process
I could identify my by the"build"folder where theindex.jsis.
- open another
cmdorgit-bashand run this command,
where21392is the PID of your process.
node -e "process._debugProcess(21392)"
Everything should be ready now.
现在一切都应该准备好了。
回答by Miquel
Even it's an old yet answered question, there is an easier way, which is passing parameters to node:
即使这是一个古老但尚未回答的问题,还有一种更简单的方法,即将参数传递给节点:
forever start -c 'node --debug-brk' main.js
If you don't want to wait for debugger to be attached, replace --debug-brkby --debug
如果您不想等待附加调试器,请替换--debug-brk为--debug
回答by DarckBlezzer
To inspect nodejs and debug it, use this command
要检查 nodejs 并调试它,请使用此命令
forever -w -c 'node --inspect=IP:PORT' file.js
forever -w -c 'node --inspect=IP:PORT' file.js
- -cif for a custom command
- use -w to reloadif the file is re-save
- You can pass ip and portfor external inspect
- port custom is 9229
- -c如果是自定义命令
- 如果重新保存文件,请使用-w 重新加载
- 您可以通过 ip 和端口进行外部检查
- 端口自定义为9229

