首页 > 解决方案 > 无法让 VSCode 调试与我的 NodeJs 应用程序一起使用

问题描述

我正在尝试在 Visual Studio Code 上调试我的应用程序。我有以下配置package.json

"scripts": {
    "build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files",
    "start": "npm run build && node --inspect=12345 dist/app.js"
}

我在我的 Node 应用程序上使用 ES6,这就是我的build配置有点混乱的原因。

当我运行npm start一切正常时,我可以使用我的应用程序。

现在尝试调试它,我设置了以下launch配置:

"configurations": [
    {
        "type": "node",
        "name": "Attach to Remote",
        "request": "attach",
        "port": 12345
    },
    {
        "type": "node",
        "request": "launch",
        "name": "Launch Program",
        "program": "${workspaceFolder}\\dist\\app.js"
    }
]

他们俩都“工作”:VS Code切换到“调试模式”,但我不能打任何断点。它们都变灰:

在此处输入图像描述

我已尝试使用答案进行修复,但无法使其正常工作...

有什么帮助吗?

提前致谢!

标签: node.jsdebuggingvisual-studio-codevscode-debugger

解决方案


我发现我只是错过了--source-maps命令中的babel-cli... -.- 添加它之后,VSCode 能够找到断点。所以基本上解决方案是:

添加--source-maps到我的构建命令:

"scripts": {
    "build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files --source-maps",
    "start": "npm run build && node --inspect=12345 dist/app.js"
 }

我配置launch如下:

"configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Launch Program",
        "program": "${workspaceFolder}\\dist\\app.js",
        "preLaunchTask": "npm: build"
    }
]

希望它可以帮助某人!


推荐阅读