admin管理员组

文章数量:1327693

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 bination 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.

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 bination 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.

Share edited Nov 8, 2018 at 17:58 m0tive asked Oct 29, 2018 at 17:39 m0tivem0tive 2,9662 gold badges24 silver badges38 bronze badges 1
  • It might be easier to attach to the Node process after it is already running. See code.visualstudio./docs/nodejs/… for details. – Ryan Commented Nov 8, 2018 at 10:23
Add a ment  | 

2 Answers 2

Reset to default 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"
]

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./fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
      {
        "label": "build",
        "type": "npm",
        "script": "build",
        "problemMatcher": [

        ]
      }
    ]
}

本文标签: javascriptHow to setup VSCode to debug a webpack bundled nodejs serverStack Overflow