【问题标题】:How to debug nodeJS (ExpressJS) app in visual studio code?如何在 Visual Studio 代码中调试 nodeJS (ExpressJS) 应用程序?
【发布时间】:2020-08-19 08:58:49
【问题描述】:

我已经尝试了许多在线可用的不同解决方案,但在我的情况下没有任何效果, 我正在尝试在运行时调试 nodeJS 应用程序,通过 UI/postman 调用 API

我的launch.json:从here获取指导

{
        "type": "node",
        "request": "attach",
        "name": "Attach by Process ID",
        "processId": "${command:PickProcess}",
        "skipFiles": [
            "<node_internals>/**"
        ]
}

启动本地服务器后,当我启动调试器时,它要求选择一个进程,我选择以--exec babel-node server.js 结尾的那个,它成功附加但不加载我的项目脚本,只有 node_modules、eval 和 node_internal。

在我的代码中,如果我设置断点,我会看到此错误“已设置断点但尚未绑定”

我的 package.json 启动脚本:

"scripts": {
    "start": "nodemon --exec babel-node server.js"
}

我的代码在 ES6 中,我通过一个 shell 脚本启动服务器,该脚本首先设置一些环境,然后执行 npm start

我的.bablerc

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ]
  ]
}

【问题讨论】:

    标签: node.js express debugging ecmascript-6 visual-studio-code


    【解决方案1】:

    这是我的 launch.json,我用 express 运行 nodejs,它工作正常。

    {
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}/app.js",
            "args": ["--env","local"]
        }
    ]
    

    }

    【讨论】:

    • 这是一个午餐配置,我需要将调试器附加到已经运行的服务器上
    【解决方案2】:

    原来我选择了错误的进程 ID。 当我们启动调试器时,它会向我们显示正在运行的进程列表,以选择要连接的调试器。 我选择了以_babel-node server.js 而不是--exec babel-node server.js 结尾的那个。这样做会加载我的所有脚本并开始按预期工作。

    【讨论】:

      猜你喜欢
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 2016-10-28
      • 2016-09-16
      相关资源
      最近更新 更多