See Dev Server in the docs.

Install

$ npm install -D webpack-dev-server

Configure

If you have a public/index.html in version control and export as unversioned public/bundle.js, you can set up the dev server also serve the contents of public on the root.

e.g.

  • webpack.js
      const path = require('path');
    
      module.exports = {
        mode: "development",
        devtool: "eval-source-map",
          
        output: {
          path: path.resolve('public'),
          filename: 'bundle.js',
        },
    
        devServer: {
          static: "./public",
        },
      };
    

Run

$ npx webpack serve

Deprecation warning: The webpack-dev-server command no longer works from Webpack 5. Using the command above.