调试

在 Nuxt 中,你可以直接在浏览器以及 IDE 中开始调试你的应用程序。

源代码映射(Sourcemaps)

默认情况下,服务器构建会启用源代码映射,开发模式下的客户端构建也会启用,但你可以在配置中更具体地启用它们。

export default defineNuxtConfig({
  // 或者 sourcemap: true
  sourcemap: {
    server: true,
    client: true
  }
})

使用 Node 检查器调试

你可以使用 Node 检查器 来调试 Nuxt 的服务器端代码。

nuxt dev --inspect

这将以调试模式启动 Nuxt 的开发服务器。如果一切正常,Chrome 开发者工具上会出现一个 Node.js 图标,你可以通过它连接到调试器。

注意,Node.js 和 Chrome 进程需要运行在相同的平台上。这在 Docker 内部无法正常工作。

在 IDE 中调试

你可以在开发 Nuxt 应用时,在 IDE 中对其进行调试。

VS Code 调试配置示例

你可能需要用你的 web 浏览器路径更新下面的配置。更多信息,请查看 VS Code 调试配置文档

{
  // 使用智能提示了解可能的属性。
  // 悬停查看现有属性的描述。
  "version": "0.2.0",
  "configurations": [
    {
      "type": "chrome",
      "request": "launch",
      "name": "客户端: chrome",
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}"
    },
    {
      "type": "node",
      "request": "launch",
      "name": "服务器: nuxt",
      "outputCapture": "std",
      "program": "${workspaceFolder}/node_modules/nuxt/bin/nuxt.mjs",
      "args": [
        "dev"
      ],
    }
  ],
  "compounds": [
    {
      "name": "全栈: nuxt",
      "configurations": [
        "服务器: nuxt",
        "客户端: chrome"
      ]
    }
  ]
}

如果你希望使用常用的浏览器扩展,可以在上面的 chrome 配置中添加以下内容:

"userDataDir": false,

JetBrains 系列 IDE 调试配置示例

你也可以在 JetBrains 系列 IDE(如 IntelliJ IDEA、WebStorm 或 PhpStorm)中调试 Nuxt 应用。

  1. 在项目根目录创建一个新文件,命名为 nuxt.run.xml
  2. 打开 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!