简体   繁体   English

在Webpack和Node.js中使用vscode调试器

[英]Using vscode debugger with webpack and node.js

Currently I'm working on a backend application using express + node.js and webpack as the bundler. 目前,我正在使用Express + Node.js和Webpack作为捆绑程序的后端应用程序。

So far, I was able to run and test my code without any problem. 到目前为止,我能够顺利运行和测试我的代码。

I would like now to use the Visual Studio Code debugger in order to debug my application. 我现在想使用Visual Studio Code调试器来调试我的应用程序。 I tried to follow along this tutorial https://medium.com/@jsilvax/debugging-webpack-with-vs-code-b14694db4f8e 我试图遵循本教程https://medium.com/@jsilvax/debugging-webpack-with-vs-code-b14694db4f8e

Now, when I try to launch my debugger, it just prints to the console Webpack is watching the files… without actually run my server 现在,当我尝试启动调试器时,它只是打印到控制台上, Webpack is watching the files…而实际上并未运行服务器

Here is my launch.json file: 这是我的launch.json文件:

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Webpack",
      "program": "${workspaceFolder}/node_modules/webpack/bin/webpack.js",
      "args": [
         "--config", "./webpack.config.js"
      ],
    }
  ]
}

And here is my webpack.config.js file: 这是我的webpack.config.js文件:

const webpack = require('webpack')
const path = require('path')
const nodeExternals = require('webpack-node-externals')
const StartServerPlugin = require('start-server-webpack-plugin')

module.exports = {
  entry: ['webpack/hot/poll?1000', './src/index'],
  watch: true,
  devtool: 'sourcemap',
  target: 'node',
  node: {
    __filename: true,
    __dirname: true
  },
  externals: [nodeExternals({ whitelist: ['webpack/hot/poll?1000'] })],
    module: {
      rules: [
        {
          test: /\.js?$/,
          use: [
            {
              loader: 'babel-loader',
              options: {
                babelrc: false,
                presets: [['env', { modules: false }], 'stage-0'],
                plugins: ['transform-regenerator', 'transform-runtime']
              }
            }
          ],
          exclude: /node_modules/
        },
        {
          test: /\.(graphql|gql)$/,
          exclude: /node_modules/,
          use: {
            loader: 'raw-loader'
          }
        }
      ]
    },
    plugins: [
      new StartServerPlugin('server.js'),
      new webpack.NamedModulesPlugin(),
      new webpack.HotModuleReplacementPlugin(),
      new webpack.NoEmitOnErrorsPlugin(),
      new webpack.DefinePlugin({
        'process.env': { BUILD_TARGET: JSON.stringify('server') }
      }),
      new webpack.SourceMapDevToolPlugin({
        filename: '[name].js.map'
      }),
      new webpack.BannerPlugin({ banner: 'require("source-map-support").install();', raw: true, entryOnly: false })
    ],
    output: { path: path.join(__dirname, 'dist'), filename: 'server.js' }
};

Also, this is my project structure: 另外,这是我的项目结构:


To run this application without using the debugger, I have a start script which looks like this: "start": "webpack --colors --progress" 要在不使用调试器的情况下运行此应用程序,我有一个启动脚本,如下所示: "start": "webpack --colors --progress"

As I was saying, when I launch the debugger, it simply hangs and doesn't do anything. 就像我说的那样,当我启动调试器时,它只是挂起而没有执行任何操作。 The only message I get inside the debugger console is Webpack is watching the files… 我在调试器控制台中收到的唯一消息是Webpack is watching the files…

I'm think, that your configuration is not correct. 我认为您的配置不正确。 In "program", choose file from your application, not from webpack, for example, "server.js" or as below, "index.js". 在“程序”中,从应用程序中选择文件,而不是从Webpack中选择文件,例如“ server.js”或下面的“ index.js”。

        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}/index.js"
        }

There can be also other problem - do you have any breakpoints added in VSCode? 还有其他问题-VSCode中添加了任何断点吗? If not, debugger just doesn't work. 如果没有,调试器将无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM