调试
在 Nuxt 中,您可以直接在浏览器和您的 IDE 中开始调试您的应用程序。
源映射
源映射默认启用于您的服务器构建,在开发模式下客户端构建也会启用,但您可以在配置中更具体地启用它们。
export default defineNuxtConfig({
// 或者 sourcemap: true
sourcemap: {
server: true,
client: true
}
})
使用 Node Inspector 调试
您可以使用 Node inspector 来调试 Nuxt 服务器端。
nuxi dev --inspect
这将在调试器活动的情况下以 dev
模式启动 Nuxt。如果一切正常,您将在 Chrome 开发者工具中看到一个 Node.js 图标,您可以附加到调试器。
::重要 请注意,Node.js 和 Chrome 进程需要在同一平台上运行。这在 Docker 内部无法工作。 ::
在您的 IDE 中调试
在开发 Nuxt 应用程序时,您可以在 IDE 中进行调试。
示例 VS Code 调试配置
您可能需要根据您的网络浏览器路径更新下面的配置。有关更多信息,请访问 VS Code 文档关于调试配置。
::重要
如果您使用 pnpm
,您需要将 nuxi
安装为 devDependency,以便下面的配置能够正常工作。
::
{
// 使用 IntelliSense 了解可能的属性。
// 悬停查看现有属性的描述。
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "client: chrome",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}"
},
{
"type": "node",
"request": "launch",
"name": "server: nuxt",
"outputCapture": "std",
"program": "${workspaceFolder}/node_modules/nuxt/bin/nuxt.mjs",
"args": [
"dev"
],
}
],
"compounds": [
{
"name": "fullstack: nuxt",
"configurations": [
"server: nuxt",
"client: chrome"
]
}
]
}
如果您更喜欢您常用的浏览器扩展,可以在上面的 chrome 配置中添加:
"userDataDir": false,
示例 JetBrains IDEs 调试配置
您还可以在 JetBrains IDEs(如 IntelliJ IDEA、WebStorm 或 PhpStorm)中调试您的 Nuxt 应用程序。
- 在项目根目录中创建一个新文件,命名为
nuxt.run.xml
。 - 打开
nuxt.run.xml
文件,并粘贴以下调试配置:
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="client: chrome" type="JavascriptDebugType" uri="http://localhost:3000" useFirstLineBreakpoints="true">
<method v="2" />
</configuration>
<configuration default="false" name="server: nuxt" type="NodeJSConfigurationType" application-parameters="dev" path-to-js-file="$PROJECT_DIR$/node_modules/nuxt/bin/nuxt.mjs" working-dir="$PROJECT_DIR$">
<method v="2" />
</configuration>
<configuration default="false" name="fullstack: nuxt" type="CompoundRunConfigurationType">
<toRun name="client: chrome" type="JavascriptDebugType" />
<toRun name="server: nuxt" type="NodeJSConfigurationType" />
<method v="2" />
</configuration>
</component>
其他 IDE
如果您有其他 IDE 并希望贡献示例配置,请随时 提交 PR!