Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Webpack Javascript Bundling for Both Front-end and Back-end (nodejs)
  • Creating a server bundle with Webpack for universal rendering

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.

like image 814
m0tive Avatar asked Oct 29 '18 17:10

m0tive


People also ask

How do I debug a Webpack bundle file?

Click the "inspect" link under each script to open a dedicated debugger or the Open dedicated DevTools for Node link for a session that will connect automatically. You can also check out the NiM extension, a handy Chrome plugin that will automatically open a DevTools tab every time you --inspect a script.

How do you debug a server in VS code?

Launch configurations# To run or debug a simple app in VS Code, select Run and Debug on the Debug start view or press F5 and VS Code will try to run your currently active file.

How do I run a Webpack in Vscode?

To run above scripts, open a terminal and type npm run webpack or select Tasks: Run Task from the Command Palette (Ctrl+Shift+P).


1 Answers

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"
]
like image 126
Navjot Ahuja Avatar answered Oct 11 '22 14:10

Navjot Ahuja