简体   繁体   English

Node.js:如何附加到正在运行的进程并使用控制台调试服务器?

[英]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
  • If you can access the server from a browser, you can use 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>
  • 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 <username>@<your_host> -i <keyfile>.pem ) and you'll find your script under chrome://inspect after a few seconds.如果您无法从浏览器访问服务器,但可以通过 SSH 连接到该服务器,则设置 SSH 端口转发( 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.希望有帮助。

Windows users Windows 用户

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)"




For a detailed guide , or to set up debugging in VSCode, follow these simple steps: 如需详细指南或在 VSCode 中设置调试,请按照以下简单步骤操作:

  1. In VSCode, open launch.json configuration or create new by clicking on the wheel在 VSCode 中,打开launch.json配置或通过单击滚轮创建新配置
    (this is the debug view Ctrl Shift D ) (这是调试视图Ctrl Shift D

在此处输入图片说明

  1. The node will listen on port 9229 by default , so add this configuration:节点默认会监听 9229 端口,所以添加这个配置:
 { "type": "node", "request": "attach", "name": "Attach to 9229", "port": 9229 },
  1. Open Task Manager and locate the PID of your node process打开任务管理器并找到您的节点进程的PID
    I could identify my by the "build" folder where the index.js is.我可以通过index.js所在的"build"文件夹来识别我的。 在此处输入图片说明
  2. open another cmd or git-bash and run this command,打开另一个cmdgit-bash并运行此命令,
    where 21392 is the PID of your process.其中21392是您的进程的 PID。
 node -e "process._debugProcess(21392)"
  1. you should see this你应该看到这个调试器监听 ws:9229
  2. Start debugging from VSCode Attach to 9229从 VSCode Attach to 9229开始调试
    在此处输入图片说明

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

  • -c if for a custom command -c如果是自定义命令
  • use -w to reload if the file is re-save如果重新保存文件,请使用-w 重新加载
  • You can pass ip and port for external inspect您可以通过 ip 和端口进行外部检查
  • port custom is 9229端口自定义为9229

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM