m0tive
m0tive

Reputation: 2946

How to setup VSCode to debug a webpack bundled nodejs server

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.

Upvotes: 10

Views: 7805

Answers (2)

prestonsmith
prestonsmith

Reputation: 778

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

        ]
      }
    ]
}

Upvotes: 0

Navjot Ahuja
Navjot Ahuja

Reputation: 1171

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

Upvotes: 3

Related Questions