如何调试Node.js服务器?调试器跳过了我的断点! (使用VSCode)
我正在尝试使用自定义服务器调试Next.js应用,该服务器通常使用执行node server.js
的dev
Yarn脚本运行.
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包括一个Node.js调试扩展程序和本指南, ve跟着.我创建了一个名为dev:debug
的新纱线脚本,该脚本运行node --inspect server.js
,并在launch.json中进行以下配置:
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
}
但是,跳过了server.js导入的模块中的某些断点,我不确定为什么.当我在Web应用程序中加载页面时,Next组件中的其他断点会起作用.有什么想法吗?
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?
好!这是正在发生的事情(由于无法在线找到答案,我将所有这些信息都发布到了SO中):
OK! Here's what's happening (and I'm posting all this to SO because I couldn't find the answer online):
构建Next.js应用程序时,您要同时编写服务器应用程序(节点)和服务器端Web应用程序(下一个),这是分开的事情!此外,您还基于此构建一个客户端应用程序(React). (如果您习惯使用诸如内置服务器,Apache或NginX之类的服务器的PHP或Python Web应用程序之类的其他平台,这并不明显!)
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!)
运行node --inspect server.js
告诉调试器加载服务器,然后仅开始调试Web应用程序(用户代码").
Running node --inspect server.js
tells the debugger to load the server and THEN start debugging your web app ONLY ("user code").
解决方案:node
CLI具有特殊的--inspect-brk
选项!是的,解决方案是再添加4个字符(-brk
).
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
).