调试
在 Nuxt 中,你可以直接在浏览器以及在你的 IDE 中开始调试你的应用。
源码映射 (Sourcemaps)
源码映射默认在服务器构建中启用,在开发模式下客户端构建也启用,但你可以在配置中更具体地启用它们。
export default defineNuxtConfig({
  // or sourcemap: true
  sourcemap: {
    server: true,
    client: true,
  },
})
使用 Node Inspector 调试
你可以使用 Node inspector 来调试 Nuxt 的服务端。
nuxt dev --inspect
这会以启用调试器的 dev 模式启动 Nuxt。如果一切正常,Chrome DevTools 上会出现一个 Node.js 图标,你可以附加到调试器。
请注意 Node.js 和 Chrome 进程需要在相同的平台上运行。在 Docker 容器内无法工作。
在 IDE 中调试
在开发时,你可以在 IDE 中调试你的 Nuxt 应用。
VS Code 调试配置示例
你可能需要在下面的配置中更新为你的 Web 浏览器路径。更多信息,请参阅 VS Code 关于调试配置的文档。
{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  "version": "0.2.0",
  "configurations": [
    {
      "type": "chrome",
      "request": "launch",
      "name": "client: chrome",
      "url": "http://localhost:3000",
      // this should point to your Nuxt `srcDir`, which is `app` by default
      "webRoot": "${workspaceFolder}/app"
    },
    {
      "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 系列 IDE 中调试 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>
其他 IDE
如果你使用其他 IDE 并且愿意贡献示例配置,欢迎 提交 PR!