简体   繁体   English

与Webpack捆绑在一起的VS Code中的Debug Node JS代码

[英]Debug Node JS code in VS Code bundled with Webpack

I'm new to Node JS, and JS development in general. 我是Node JS的新手,也是JS开发的新手。 I have an app-bundle.js file that is bundled by WebPack, that is called through an app.js file. 我有一个由WebPack捆绑的app-bundle.js文件,通过app.js文件调用。

I am trying to debug using Visual Studio Code. 我正在尝试使用Visual Studio代码进行调试。 I created the launch.json file for the VS code configuration. 我为VS代码配置创建了launch.json文件。 However, when I insert breakpoints in the individual js files, I get 但是,当我在各个js文件中插入断点时,我得到了

Breakpoints set, but not yet bound

However, when I set a breakpoint in app.js or app.bundle.js, it works fine. 但是,当我在app.js或app.bundle.js中设置断点时,它工作正常。

My launch.json is below: 我的launch.json如下:

{
        "type": "node",
        "request": "launch",
        "name": "Launch Program",
        "program": "${workspaceFolder}/app.js"
}

How can I get the VS code debugger to work with the individual js files? 如何让VS代码调试器与各个js文件一起使用?

I was able to solve the issue. 我能够解决这个问题。

As debug is deprecated, use inspect . 不推荐使用debug ,请使用inspect Therefore, add the following to the script object in package.json to build using webpack and start node in debug mode with 9229 as the local port: 因此,将以下内容添加到package.json的脚本对象中,以使用webpack和start节点在调试模式下构建,并将9229作为本地端口:

"start": "webpack && node --inspect--brk=9229 app.js"

As webpack bundles the js files into one, we need a sourcemap, so that VS code can use breakpoints on the individual files. 当webpack将js文件捆绑为一个时,我们需要一个源映射,因此VS代码可以在各个文件上使用断点。 Therefore, in webpack.config.js , add the SourceMapDevToolPlugin : 因此,在webpack.config.js ,添加SourceMapDevToolPlugin

plugins:[
    new webpack.SourceMapDevToolPlugin({
        filename: '[name].js.map'
    })
]

Finally in VS Code , configure the launch.json file as follow: 最后在VS Code中 ,配置launch.json文件如下:

{
    "type": "node",
    "request": "launch",
    "name": "Launch Program"

    "stopOnEntry": false,
    "args": [],
    "cwd": "${workspaceFolder},
    "preLaunchTask": null,
    "runtimeExecutable": "npm",
    "runtimeArgs": [
        "run-script", "start"
    ],
    "env": {
        "NODE_ENV": "development"
    },
    "console": "integratedTerminal",
    "port": 9229
}

Short answer: 简短回答:

  1. Change the vs-code debug configuration file. 更改vs-code调试配置文件。
  2. Add a npm-script command debug same as the run-script value above. 添加npm-script命令debug与上面的run-script值相同。
  3. Start Debugging F5 . 开始调试F5

Configuration content: 配置内容:

{
    "name": "Launch via NPM",
    "type": "node",
    "request": "launch",
    "cwd": "${workspaceFolder}",
    "runtimeExecutable": "npm",
    "runtimeArgs": [
        "run-script", "debug"
    ],
    "port": 9229
}

Reference: 参考:

在此输入图像描述在此输入图像描述在此输入图像描述 Official Doc 官方文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM