调试

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

源映射

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

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

使用 Node 调试器进行调试

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

nuxi dev --inspect

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

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

在你的 IDE 中调试

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

VS Code 调试配置示例

你可能需要更新下面的配置,指定你的网页浏览器的路径。有关更多信息,请访问 VS Code 关于调试配置的文档

如果你使用 pnpm,你需要将 nuxi 作为开发依赖安装,以使下面的配置正常工作。
{
  // 使用 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 应用。

  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