Skip to main content

调试

本文档介绍了如何使用 VS Code 调试器Chrome 开发工具 调试具有完整源映射支持的 Next.js 前端和后端代码。

¥This documentation explains how you can debug your Next.js frontend and backend code with full source maps support using either the VS Code debugger or Chrome DevTools.

任何可以附加到 Node.js 的调试器也可以用于调试 Next.js 应用。你可以在 Node.js 调试指南 中找到更多详细信息。

¥Any debugger that can attach to Node.js can also be used to debug a Next.js application. You can find more details in the Node.js Debugging Guide.

使用 VS Code 进行调试

¥Debugging with VS Code

在项目的根目录下创建一个名为 .vscode/launch.json 的文件,其中包含以下内容:

¥Create a file named .vscode/launch.json at the root of your project with the following content:

{
"version": "0.2.0",
"configurations": [
{
"name": "Next.js: debug server-side",
"type": "node-terminal",
"request": "launch",
"command": "npm run dev"
},
{
"name": "Next.js: debug client-side",
"type": "chrome",
"request": "launch",
"url": "http://localhost:3000"
},
{
"name": "Next.js: debug full stack",
"type": "node",
"request": "launch",
"program": "${workspaceFolder}/node_modules/.bin/next",
"runtimeArgs": ["--inspect"],
"skipFiles": ["<node_internals>/**"],
"serverReadyAction": {
"action": "debugWithEdge",
"killOnServerStop": true,
"pattern": "- Local:.+(https?://.+)",
"uriFormat": "%s",
"webRoot": "${workspaceFolder}"
}
}
]
}

如果你使用的是 Yarn,则 npm run dev 可以替换为 yarn dev;如果你使用的是 pnpm,则可以替换为 pnpm dev

¥npm run dev can be replaced with yarn dev if you're using Yarn or pnpm dev if you're using pnpm.

如果你的应用启动于 更改端口号,请将 http://localhost:3000 中的 3000 替换为你正在使用的端口。

¥If you're changing the port number your application starts on, replace the 3000 in http://localhost:3000 with the port you're using instead.

如果你从根目录以外的目录运行 Next.js(例如,如果你使用 Turborepo),则需要将 cwd 添加到服务器端和全堆栈调试任务。例如,"cwd": "${workspaceFolder}/apps/web"

¥If you're running Next.js from a directory other than root (for example, if you're using Turborepo) then you need to add cwd to the server-side and full stack debugging tasks. For example, "cwd": "${workspaceFolder}/apps/web".

现在转到“调试”面板(Windows/Linux 上为 Ctrl+Shift+D,macOS 上为 ⇧+⌘+D),选择启动配置,然后按 F5 或选择“调试”:从命令面板开始调试以启动调试会话。

¥Now go to the Debug panel (Ctrl+Shift+D on Windows/Linux, ⇧+⌘+D on macOS), select a launch configuration, then press F5 or select Debug: Start Debugging from the Command Palette to start your debugging session.

使用 Jetbrains WebStorm 中的调试器

¥Using the Debugger in Jetbrains WebStorm

单击列出运行时配置的下拉菜单,然后单击 Edit Configurations...。使用 http://localhost:3000 作为 URL 创建 Javascript Debug 调试配置。根据你的喜好进行自定义(例如用于调试的浏览器、存储为项目文件),然后单击 OK。运行此调试配置,所选浏览器应自动打开。此时,你应该有 2 个应用处于调试模式:NextJS 节点应用和客户端/浏览器应用。

¥Click the drop down menu listing the runtime configuration, and click Edit Configurations.... Create a Javascript Debug debug configuration with http://localhost:3000 as the URL. Customize to your liking (e.g. Browser for debugging, store as project file), and click OK. Run this debug configuration, and the selected browser should automatically open. At this point, you should have 2 applications in debug mode: the NextJS node application, and the client/ browser application.

使用 Chrome 开发者工具进行调试

¥Debugging with Chrome DevTools

客户端代码

¥Client-side code

通过运行 next devnpm run devyarn dev 像平常一样启动开发服务器。服务器启动后,在 Chrome 中打开 http://localhost:3000(或你的备用 URL)。接下来,打开 Chrome 的开发者工具(Windows/Linux 上为 Ctrl+Shift+J,macOS 上为 ⌥+⌘+I),然后转到“源”选项卡。

¥Start your development server as usual by running next dev, npm run dev, or yarn dev. Once the server starts, open http://localhost:3000 (or your alternate URL) in Chrome. Next, open Chrome's Developer Tools (Ctrl+Shift+J on Windows/Linux, ⌥+⌘+I on macOS), then go to the Sources tab.

现在,只要客户端代码到达 debugger 语句,代码执行就会暂停,并且该文件将出现在调试区域中。你还可以在 Windows/Linux 上按 Ctrl+P 或在 macOS 上按 ⌘+P 来搜索文件并手动设置断点。请注意,在此处搜索时,你的源文件将具有以 webpack://_N_E/./ 开头的路径。

¥Now, any time your client-side code reaches a debugger statement, code execution will pause and that file will appear in the debug area. You can also press Ctrl+P on Windows/Linux or ⌘+P on macOS to search for a file and set breakpoints manually. Note that when searching here, your source files will have paths starting with webpack://_N_E/./.

服务器端代码

¥Server-side code

要使用 Chrome DevTools 调试服务器端 Next.js 代码,你需要将 --inspect 标志传递给底层 Node.js 进程:

¥To debug server-side Next.js code with Chrome DevTools, you need to pass the --inspect flag to the underlying Node.js process:

NODE_OPTIONS='--inspect' next dev

如果你使用的是 npm run devyarn dev,那么你应该更新 package.json 上的 dev 脚本:

¥If you're using npm run dev or yarn dev then you should update the dev script on your package.json:

{
"scripts": {
"dev": "NODE_OPTIONS='--inspect' next dev"
}
}

使用 --inspect 标志启动 Next.js 开发服务器将如下所示:

¥Launching the Next.js dev server with the --inspect flag will look something like this:

Debugger listening on ws://127.0.0.1:9229/0cf90313-350d-4466-a748-cd60f4e47c95
For help, see: https://nodejs.cn/docs/inspector
ready - started server on 0.0.0.0:3000, url: http://localhost:3000

请注意,运行 NODE_OPTIONS='--inspect' npm run devNODE_OPTIONS='--inspect' yarn dev 不起作用。这将尝试在同一端口上启动多个调试器:一种用于 npm/yarn 流程,另一种用于 Next.js。然后你会在控制台中收到类似 Starting inspector on 127.0.0.1:9229 failed: address already in use 的错误。

¥Be aware that running NODE_OPTIONS='--inspect' npm run dev or NODE_OPTIONS='--inspect' yarn dev won't work. This would try to start multiple debuggers on the same port: one for the npm/yarn process and one for Next.js. You would then get an error like Starting inspector on 127.0.0.1:9229 failed: address already in use in your console.

服务器启动后,在 Chrome 中打开一个新选项卡并访问 chrome://inspect,你应该在远程目标部分中看到 Next.js 应用。单击应用下的“检查”以打开单独的 DevTools 窗口,然后转到“源”选项卡。

¥Once the server starts, open a new tab in Chrome and visit chrome://inspect, where you should see your Next.js application inside the Remote Target section. Click inspect under your application to open a separate DevTools window, then go to the Sources tab.

此处调试服务器端代码的工作方式与使用 Chrome DevTools 调试客户端代码非常相似,不同之处在于,当你在此处使用 Ctrl+P⌘+P 搜索文件时,你的源文件将具有以 webpack://{application-name}/./ 开头的路径(其中 {application-name} 将替换为 根据你的 package.json 文件进行应用)。

¥Debugging server-side code here works much like debugging client-side code with Chrome DevTools, except that when you search for files here with Ctrl+P or ⌘+P, your source files will have paths starting with webpack://{application-name}/./ (where {application-name} will be replaced with the name of your application according to your package.json file).

在 Windows 上调试

¥Debugging on Windows

Windows 用户在使用 NODE_OPTIONS='--inspect' 时可能会遇到问题,因为 Windows 平台不支持该语法。要解决此问题,请安装 cross-env 包作为开发依赖(-Dnpmyarn),并将 dev 脚本替换为以下内容。

¥Windows users may run into an issue when using NODE_OPTIONS='--inspect' as that syntax is not supported on Windows platforms. To get around this, install the cross-env package as a development dependency (-D with npm and yarn) and replace the dev script with the following.

{
"scripts": {
"dev": "cross-env NODE_OPTIONS='--inspect' next dev"
}
}

无论你使用哪个平台(包括 Mac、Linux 和 Windows),cross-env 都会设置 NODE_OPTIONS 环境变量,并允许你跨设备和操作系统进行一致的调试。

¥cross-env will set the NODE_OPTIONS environment variable regardless of which platform you are on (including Mac, Linux, and Windows) and allow you to debug consistently across devices and operating systems.

很高兴知道:确保你的计算机上禁用了 Windows Defender。该外部服务将检查读取的每个文件,据报告这会大大增加 next dev 的快速刷新时间。这是一个已知问题,与 Next.js 无关,但它确实影响 Next.js 开发。

¥Good to know: Ensure Windows Defender is disabled on your machine. This external service will check every file read, which has been reported to greatly increase Fast Refresh time with next dev. This is a known issue, not related to Next.js, but it does affect Next.js development.

更多信息

¥More information

要了解有关如何使用 JavaScript 调试器的更多信息,请查看以下文档:

¥To learn more about how to use a JavaScript debugger, take a look at the following documentation: