调试
在 Nuxt 中,您可以直接在浏览器和 IDE 中开始调试您的应用程序。
源映射
默认情况下,源映射在服务器构建中启用,并在开发模式下为客户端构建启用,但您可以在配置中更具体地启用它们。
export default defineNuxtConfig({
// 或 sourcemap: true
sourcemap: {
server: true,
client: true
}
})
使用 Node Inspector 调试
您可以使用 Node inspector 来调试 Nuxt 服务器端。
nuxt dev --inspect
这将以激活调试器的 dev
模式启动 Nuxt。如果一切正常,Node.js 图标将出现在您的 Chrome DevTools 上,您可以附加到调试器。
请注意,Node.js 和 Chrome 进程需要在同一平台上运行。这在 Docker 内部不起作用。
在您的 IDE 中调试
在开发时,可以在您的 IDE 中调试 Nuxt 应用程序。
示例 VS Code 调试配置
您可能需要使用您的网页浏览器路径更新下面的配置。有关更多信息,请访问 VS Code 关于调试配置的文档。
{
// 使用 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 中调试 Nuxt 应用程序,例如 IntelliJ IDEA、WebStorm 或 PhpStorm。
-
在项目根目录中创建一个新文件,并命名为
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>
其他 IDEs
如果您有其他 IDE 并希望贡献示例配置,欢迎 打开 PR!