调试

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

Sourcemaps

默认情况下,Sourcemaps 已为你的服务端构建启用,并且在开发模式下为客户端构建启用,但你可以在配置中更精确地启用它们。

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

使用 Node Inspector 调试

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

nuxt dev --inspect

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

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

在你的 IDE 中调试

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

VS Code 调试配置示例

你可能需要使用指向你网页浏览器的路径来更新下方的配置。有关更多信息,请访问 关于调试配置的 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": "https://: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 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="https://: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