【问题标题】:How to setup VSCode to debug a webpack bundled nodejs server如何设置 VSCode 来调试 webpack 捆绑的 nodejs 服务器
【发布时间】:2019-04-02 17:02:08
【问题描述】:

我有一个 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


【解决方案1】:

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

构建: 你可以使用 program 作为你的 webpack 运行器的路径。例如:

"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"
]

【讨论】:

  • 这只会构建包,不会执行它。我想在构建后运行包并逐步执行代码(使用原始文件)。是否有一些额外的配置可以使这个构建并运行包?
  • 您也可以为此修改程序。编辑答案。
  • 这仍然没有解决我的问题,我使用 webpack 来捆绑 Express 服务器,而不是捆绑客户端应用程序(尽管我也在并行执行此操作)。我希望能够调试 express 服务器。
【解决方案2】:

试试这两种配置。应该先构建项目,然后通过 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": [

        ]
      }
    ]
}

【讨论】:

  • VSCode 告诉我 "inspector" 不是 launch.json 中允许的属性
  • 我实际上要完全改变我的答案@MegaMatt - 这是针对另一个正在执行服务器端渲染的项目。
猜你喜欢
  • 2019-11-24
  • 2017-04-02
  • 2017-06-17
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-19
相关资源
最近更新 更多