首页 > 解决方案 > 如何在 Visual Studio 代码中调试 nodeJS (ExpressJS) 应用程序?

问题描述

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

我的 launch.json:从这里获取指南

{
        "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.jsexpressdebuggingecmascript-6visual-studio-code

解决方案


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

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

}


推荐阅读