How to debug a Node.js server? The debugger skips my breakpoints! (Using VSCode)

穿精又带淫゛_ 提交于 2020-01-25 08:41:25

问题


I'm trying to debug a Next.js app with a custom server which I normally run with a dev Yarn script that executes node server.js.

VSCode includes a Node.js debugging extension and this guide, which I've followed. I created a new Yarn script called dev:debug that runs node --inspect server.js, and the following configuration in launch.json:

{
  "type": "node",
  "request": "launch",
  "name": "Debug via Yarn",
  "runtimeExecutable": "yarn",
  "runtimeArgs": ["dev:debug"],
  "port": 9229
}

However some breakpoints in modules imported by server.js are skipped and I'm not sure why. Other breakpoints in Next components do work when I load pages in my web app. Any ideas?


回答1:


OK! Here's what's happening (and I'm posting all this to SO because I couldn't find the answer online):

When you build a Next.js app, you're writing BOTH the server app (node), AND the server-side web app (next), which are separate things! Additionally, you're also building a client-side app (React) on top of that. (If you're used to other platforms like PHP or Python web apps that use servers like the built-in ones, Apache, or NginX, this isn't obvious!)

Running node --inspect server.js tells the debugger to load the server and THEN start debugging your web app ONLY ("user code").

Solution: node CLI has a special --inspect-brk option to use when you also want to debug the code of the server itself! Yep, the solution is 4 more characters (-brk).



来源:https://stackoverflow.com/questions/59596138/how-to-debug-a-node-js-server-the-debugger-skips-my-breakpoints-using-vscode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!