[英]Node.js: How to attach to a running process and to debug the server with a console?
I use 'forever' to run my application.我使用“永远”来运行我的应用程序。 I want to attach to the running environment to inspect my application.
我想附加到运行环境来检查我的应用程序。 So what can I do?
那我能做什么?
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 node
process and then sending SIGUSR1
should do the trick:找到您的
node
进程的 PID,然后发送SIGUSR1
应该可以解决问题:
kill -s SIGUSR1 nodejs-pid
Then run node-inspector
and browse to the URL it indicates.然后运行
node-inspector
并浏览到它指示的 URL。 More in this tutorial . 本教程中的更多内容。
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
chrome://inspect
on http://host.domain:9229
.http://host.domain:9229
上使用chrome://inspect
。 If you cannot connect via a browser (eg the server is in a firewalled production cluster), you can activate a REPL to inspect over the command line:如果您无法通过浏览器连接(例如,服务器位于受防火墙保护的生产集群中),您可以激活 REPL 以通过命令行进行检查:
node inspect -p <node-pid>
ssh -nNTL 9229:localhost:9229 <username>@<your_host> -i <keyfile>.pem
) and you'll find your script under chrome://inspect
after a few seconds.ssh -nNTL 9229:localhost:9229 <username>@<your_host> -i <keyfile>.pem
)和几秒钟后,您将在chrome://inspect
下找到您的脚本。 Prior to this version, node-inspector
was a separate tool for debugging Node processes.在此版本之前,
node-inspector
是用于调试 Node 进程的单独工具。 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 .
有关此更改的更多信息,请参阅此线程。
For me, running node version 6.9.10 I had to:对我来说,运行节点版本 6.9.10 我必须:
kill -USR1 <node-pid>
then然后
node debug -p <node-pid>
the node inspect -p <node-pid>
command failed for this version of node.对于此版本的节点,
node inspect -p <node-pid>
命令失败。
You can add a REPL to your app.您可以向您的应用程序添加REPL 。 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
.例如,如果您添加 REPL 以侦听 localhost 端口 5001,则您可以像往常一样启动您的应用程序并使用 telnet 登录:
telnet localhost 5001
。 That will take you to a prompt where you can interact with your app directly.这将带您进入一个提示,您可以在其中直接与您的应用程序交互。
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 debug mode.
行到您想要这些断点的代码区域,然后在调试模式下启动应用程序。
Hope that helps.希望有帮助。
If you are on Windows that doesn't support POSIX signals, you can use this workaround from another cmd
.如果您使用的 Windows 不支持 POSIX 信号,则可以从另一个
cmd
使用此解决方法。
node -e "process._debugProcess(PID)"
launch.json
configuration or create new by clicking on the wheellaunch.json
配置或通过单击滚轮创建新配置 { "type": "node", "request": "attach", "name": "Attach to 9229", "port": 9229 },
"build"
folder where the index.js
is.index.js
所在的"build"
文件夹来识别我的。 cmd
or git-bash
and run this command,cmd
或git-bash
并运行此命令,21392
is the PID of your process.21392
是您的进程的 PID。 node -e "process._debugProcess(21392)"
Everything should be ready now.现在一切都应该准备好了。
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-brk
by --debug
如果您不想等待附加调试器,请将
--debug-brk
替换为--debug
To inspect nodejs and debug it, use this command要检查 nodejs 并调试它,请使用此命令
forever -w -c 'node --inspect=IP:PORT' file.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.