如何设置VSCode来调试webpack捆绑的nodejs服务器

问题描述 投票:6回答:2

我有一个nodejs express应用程序,我试图捆绑webpack 4(加上babel 7.1.0)。我已经按照这两篇文章中的一些设置进行了操作:

我可以在捆绑后构建和运行服务器,但我希望能够使用VS Code的调试环境对其进行调试。

我尝试了以下webpack和vscode配置的组合,但它没有设置断点或让我进入代码。

.vscode / launch.json

{
    "type": "node",
    "request": "launch",
    "name": "bundle-server.js",
    "program": "${workspaceFolder}\\bundle-server.js",
    "sourceMaps": true,
    "smartStep": true,
}

的WebPack-server.config.js

const path = require('path');
const nodeExternals = require('webpack-node-externals');

module.exports = {
    target: "node",
    entry: "./server.js",
    output: {
        path: path.resolve(__dirname, "./"),
        filename: "bundle-server.js",
    },
    module: {
        rules: [
            {
                test: /\.jsx?$/,
                loader: "babel-loader"
            }
        ],
    },
    externals: [nodeExternals()],
    devtool: 'inline-source-map',
};

我错过了什么?甚至可以直接从VSCode调试吗?我希望能够跨越原始源文件,以便我可以快速调试 - 编辑 - 重新运行循环。


似乎与此有关:Debug webpack bundled node ts with Visual Studio Code

javascript node.js webpack visual-studio-code
2个回答
1
投票

在启动配置中,您将提供webpack的输出文件作为program进行调试。

要构建:您可以使用program作为webpack runner的路径。例如:

"program": "${workspaceFolder}/node_modules/webpack/bin/webpack.js" // Or CLI

然后你应该提供文件作为你想用webpack运行的参数。例如:

"args": [
   "--config", "./some/dir/webpack.config.js"
]

跑步:

使用不同的程序执行相同的过程

"program": "${workspaceFolder}/node_modules/webpack-dev-server/bin/webpack-dev-server",
"args": [
    "--config",
    "webpack-server.config.js",
    "--hot",
    "--progress"
]

0
投票

试试这两种配置。应首先构建项目,然后通过VSCode自动连接节点检查器。我刚试了一下我的项目,看起来效果很好。

我正在做同样的事情 - 使用WebpackBabel建立一个项目

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Debug Server",
      "program": "${workspaceFolder}\\bundle-server.js",
      "preLaunchTask": "build"
    }
  ]
}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
      {
        "label": "build",
        "type": "npm",
        "script": "build",
        "problemMatcher": [

        ]
      }
    ]
}
© www.soinside.com 2019 - 2024. All rights reserved.