【问题标题】:Debugging strapi in Visual Studio Code在 Visual Studio Code 中调试strapi
【发布时间】:2019-09-28 10:24:34
【问题描述】:

我正在尝试在 VS Code 中调试我的strapi 项目(3.0.0 beta 16.6)。 我的launch.json:

{
  "type": "node",
  "request": "attach",
  "name": "Attach to strapi",
  "port": 9229
} 

我的 package.json:

"scripts": {
    "debug": "node --inspect=127.0.0.1:9229 ./node_modules/strapi/bin/strapi.js develop"
}

调试器附加到进程,但我的所有断点都未验证(显示为黑色,而不是红色)。我的配置有什么问题?

【问题讨论】:

    标签: debugging visual-studio-code strapi


    【解决方案1】:

    .vscode/launch.json"configurations"属性中添加VS配置“NODE:launch via npm”并运行

        {
          "type": "node",
          "request": "launch",
          "name": "Launch via NPM",
          "runtimeExecutable": "npm",
          "runtimeArgs": [
            "run-script",
            "develop"
          ],
          "port": 9229,
          "skipFiles": [
            "<node_internals>/**"
          ],
          "console": "integratedTerminal"
        }
    

    【讨论】:

    • 已通过 Strapi 14.1.6 确认,但我缺少控制台输出,因此我添加了 "console": "integratedTerminal"
    【解决方案2】:

    这个答案来自以下strapi/strapi issue

    我想出了下一个解决方案: 在 server.js 文件中有下一个脚本(我的自定义脚本):

    const strapi = require('strapi');
    strapi({ dir: process.cwd(), autoReload: true }).start();
    

    我正在通过下一个命令使用 nodemon:nodemon --inspect=0.0.0.0:9228 server.js

    现在我可以通过调试器附加到9228

    【讨论】:

    • 谢谢!我得到的甚至超出了我的预期。奖励:自动重新加载。
    【解决方案3】:

    只是添加到@alxnkt 评论。 我也遇到过同样的问题,通过将launch.json改为端口9230解决了

    {
      "type": "node",
      "request": "attach",
      "name": "Attach to strapi",
      "port": 9230
    }
    

    同时将 package.json 上的端口保持为 9229

    "debug": "node --inspect=127.0.0.1:9229 ./node_modules/strapi/bin/strapi.js develop"
    

    调用 Strapi 开发命令时不知何故有 2 个进程在运行(可能是管理面板及其核心服务器),我们必须监控的进程改为端口 9230。

    【讨论】:

    • 在我的情况下,每次我在 Strapi 项目中更改某些文件时,端口都会增加 1,因为我相信 Strapi 会重新启动服务器和第二个进程。这使得调试它非常烦人......
    【解决方案4】:

    我刚刚通过 NPM 启动了它,这是我的 launch.json(在 .vscode 文件夹中)

    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "node",
          "request": "launch",
          "name": "Launch via NPM",
          "runtimeExecutable": "npm",
          "runtimeArgs": [
            "run-script",
            "develop"
          ],
          "port": 9229,
          "skipFiles": [
            "<node_internals>/**"
          ],
          "console": "integratedTerminal"
        }
      ]
    }
    

    【讨论】:

    • 已通过 Strapi 14.1.6 确认,但我缺少控制台输出,因此我添加了 "console": "integratedTerminal"
    【解决方案5】:

    通过设置端口号为9203解决了问题:

    {
      "type": "node",
      "request": "attach",
      "name": "Attach to strapi",
      "port": 9229
    } 
    

    但我不知道它是如何工作的......

    【讨论】:

      【解决方案6】:

      你也可以使用NODE_OPTIONS:

      NODE_OPTIONS='--inspect' yarn strapi develop
      

      你会得到:

      $ NODE_OPTIONS="--inspect" yarn strapi develop
      Debugger listening on ws://127.0.0.1:9229/8564942a-6476-443a-9f64-87fa6d0055b7
      For help, see: https://nodejs.org/en/docs/inspector
      yarn run v1.22.5
      $ strapi develop
      Starting inspector on 127.0.0.1:9229 failed: address already in use
      Debugger listening on ws://127.0.0.1:9230/7da840dd-cb89-493f-8ce0-e11530efdfbb
      For help, see: https://nodejs.org/en/docs/inspector
      

      现在您可以使用 Debug: Attach to Node Process 来附加到端口 9230。

      【讨论】:

      • 用 Strapi 4.1.6 确认
      【解决方案7】:

      这对我使用 Strapi v4 有效

      {
          "version": "0.2.0",
          "configurations": [
           {
            "name": "STRAPI Debug",
            "type": "node",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "runtimeExecutable": "node",
            "runtimeVersion":"14.19.0",
            "runtimeArgs": ["--lazy"],
            "skipFiles": ["<node_internals>/**"],
            "program": "${workspaceRoot}/node_modules/@strapi/strapi/bin/strapi.js",
            "args": [
              "develop"
            ],
            "protocol": "inspector",
            "env": {
              "NODE_ENV": "development"
            },
            "autoAttachChildProcesses": true,
            "console": "integratedTerminal"
           },
          ]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-17
        • 1970-01-01
        • 2016-12-29
        • 1970-01-01
        • 1970-01-01
        • 2018-08-18
        • 2017-12-18
        • 2020-07-28
        相关资源
        最近更新 更多