10

I have a nodejs express application, which I'm attempting to bundle with webpack 4 (plus babel 7.1.0). I've followed some of the setup from these two articles:

I can build and run the server once bundled, but I'd like to be able to debug it using VS Code's debug environment.

I've tried the following combination of webpack and vscode config, but it doesn't set breakpoints or let me step into the code.

.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',
};

What am I missing? Is it even possible to debug directly from VSCode? I'd like to be able to step over the original source files so I can have a quick debug-edit-rerun loop.


Seems related to this: Debug webpack bundled node ts with Visual Studio Code.

m0tive
  • 2,796
  • 1
  • 22
  • 36
  • It might be easier to attach to the Node process after it is already running. See https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_attaching-to-nodejs for details. – Ryan Nov 08 '18 at 10:23

2 Answers2

3

In your launch configs, you are providing output file of webpack as the program to debug.

To Build: You can instead use program as path to your webpack runner. Ex:

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

And then you should provide the file as an argument you want to run with webpack. Ex:

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

To Run:

Follow the same procedure with a different program

"program": "${workspaceFolder}/node_modules/webpack-dev-server/bin/webpack-dev-server",
"args": [
    "--config",
    "webpack-server.config.js",
    "--hot",
    "--progress"
]
Navjot Ahuja
  • 1,141
  • 7
  • 10
  • 1
    This only builds the bundle, it doesn't execute it. I want to run the bundle after being built and step through the code (using the original files). Is there some additional configurations that would make this build _and run_ the bundle? – m0tive Nov 08 '18 at 10:02
  • You can modify the program for that also. Edited answer. – Navjot Ahuja Nov 08 '18 at 11:08
  • 1
    This still doesn't address my question, I'm using webpack to bundle an Express server, not to bundle a client app (although I am also doing that in parallel). I want to be able debug the express server. – m0tive Nov 08 '18 at 12:18
0

Try these two configurations. Should build the project first and then automagically attach the node inspector via VSCode. I've just tried it on my project and it seems to be working well.

I'm doing the same thing you are - building a project using Webpack and Babel

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": [

        ]
      }
    ]
}
prestonsmith
  • 758
  • 1
  • 9
  • 29
  • VSCode tells me that "inspector" is not an allowed property in launch.json – Matt Mar 14 '19 at 15:31
  • I'm going to change my answer completely actually @MegaMatt - this was for another project that was doing server side rendering. – prestonsmith Mar 14 '19 at 19:15