简体   繁体   English

Node.js调试流程

[英]Node.js Debugging flow

I'd like to debug node.js like rails. 我想像rails一样调试node.js。 I've tried several ways: 我尝试了几种方法:

  1. Webstorm debug – in this case I need to click "rerun debug" each time after code changed Webstorm调试–在这种情况下,每次更改代码后,我都需要单击“重新运行调试”
  2. Nodemon with chrome remote debugger – in this case I need to reconnect to debugger each time after code reloaded by nodemon 使用chrome远程调试器的Nodemon –在这种情况下,每次由nodemon重新加载代码后,我都需要重新连接到调试器
  3. pry.js – here I need to type "eval(pry.it)" – and have no option to make it simpler like "debug" or pry() pry.js –在这里我需要输入“ eval(pry.it)” –并且没有选择使其更简单,例如“ debug”或pry()

So what's the best option to debug node apps like byebug for rails? 那么调试像byebug for rails这样的节点应用程序的最佳选择是什么?

Upgrade: 升级:

Thx to @AbhinavD I've managed to do it in VSCode: First you need to edit app/.vscode/launch.js javascript { "version": "0.2.0", "configurations": [ { "type": "node", "request": "attach", "name": "Node: Nodemon", "processId": "${command:PickProcess}", "restart": true, "protocol": "inspector", }, ] } So now nodemon restarts server after changes and VSCode reconnect to debugger automatically. 谢谢@AbhinavD我已经在VSCode中做到了:首先,您需要编辑app / .vscode / launch.js javascript { "version": "0.2.0", "configurations": [ { "type": "node", "request": "attach", "name": "Node: Nodemon", "processId": "${command:PickProcess}", "restart": true, "protocol": "inspector", }, ] }现在,nodemon在更改后重新启动服务器,并且VSCode自动重新连接到调试器。

See video https://vimeo.com/267975071 观看视频https://vimeo.com/267975071

Is anybody able to do it in WebStorm? 有人可以在WebStorm中做到吗?

To answer your question: There is no way . 回答您的问题: 没有办法 Unfortunately, with Node.js, you always have to restart the debugging session when you change your code 😕. 不幸的是,使用Node.js,更改代码时总是必须重新启动调试会话。

I've recently written an article about debugging both JavaScript and TypeScript using different tools, if you're interested, but there's nothing like live debugging. 如果您感兴趣的话, 我最近写了一篇有关使用不同工具调试JavaScript和TypeScript 的文章 ,但是没有什么比实时调试好。

Now with the upgrade works just fine. 现在进行升级就可以了。 [23/09/2018] [23/09/2018]

For configuration you need to have the Webstorm 2018. 2 and follow the steps below: 要进行配置,您需要拥有Webstorm 2018. 2并执行以下步骤:

Webstorm运行/调试配置

and ALSO include the option to "Attach to Node.js/Chrome" [+] as follow: 并且还包含“附加到Node.js / Chrome” [+]选项,如下所示:

webstorm运行/调试配置附加选项

To make the debugger restart after every change you would have to enable the [ x] Reconnect Automatically option. 要使调试器在每次更改后重新启动,您必须启用[ x]自动重新连接选项。

The flow of working with the debug is: 使用调试的流程是:

  1. Run your (in my case) Nodemon. 运行您的Nodemon。
  2. Debug your (in my case) NodemonDebugger 调试您的NodemonDebugger
  3. Set the breakpoints and everything else and you are good to go. 设置断点和其他所有内容,您就可以开始了。

Note: If you are going direct to the debugger instead (and therefore not following this process) one would probably be expecting a termination of the process after changing the code lines. 注意:如果您直接转到调试器(因此不遵循此过程),则可能会期望更改代码行后该过程终止。

I am not sure how byebug works. 我不确定byebug工作方式。 However if you use VSCode and nodemon (installed globally), VSCode can reattach itself to the running process and will break at the same point in the debugger. 但是,如果使用VSCode和nodemon(全局安装),则VSCode可以将自身重新附加到正在运行的进程中,并且将在调试器的同一点中断。

Here is what my config file looks like 这是我的配置文件的样子

{
      "name": "Launch server.js via nodemon",
      "type": "node",
      "request": "launch",
      "runtimeExecutable": "nodemon",
      "program": "${workspaceFolder}/joiValidation.js",
      "restart": true,
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen"
    }

Here is the small video I made from my debugging. 这是我通过调试制作的小视频 Official documentation 官方文件

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

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