D
D
damir_fe2021-09-20 12:03:49
webpack
damir_fe, 2021-09-20 12:03:49

How to fix error when running webpack serve?

When I try to start devServer, it gives an error, Google did not help. Thank you.

Mistake
yarn run v1.22.11
$ webpack serve
[webpack-cli] Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options has an unknown property 'contentBase'. These properties are valid:
object { allowedHosts?, bonjour?, client?, compress?, devMiddleware?, headers?, historyApiFallback?, host?, hot?, http2?, https?, ipc?, liveReload?, magicHtml?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, port?, proxy?, setupExitSignals?, static?, watchFiles?, webSocketServer? }
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.


my webpack.gonfig
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const webpack = require('webpack');

module.exports = {
  mode: 'development',
  devServer: {
    static: './',
    historyApiFallback: true,
    contentBase: path.resolve(__dirname, './dist'),
    open: true,
    compress: true,
    hot: true,
    port: 8080,
  },
  entry: {
    main: path.resolve(__dirname, './src/index.js'),
  },
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: '[name].bundle.js',
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Test PWD',
      template: path.resolve(__dirname, './src/template.html'),
      filename: 'index.html',
    }),
    new CleanWebpackPlugin(),
    new webpack.HotModuleReplacementPlugin(),
  ],
  module: {
    rules: [
      // JavaScript
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: ['babel-loader'],
      },
      {
        test: /\.(scss|css)$/,
        use: ['style-loader', 'css-loader', 'postcss-loader', 'sass-loader'],
      },
    ],
  },
};

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
damir_fe, 2021-09-20
@damir_fe

Approached the solution from https://stackoverflow.com/questions/69117663/webpa...
Proper devserver configuration https://webpack.js.org/configuration/dev-server/

T
twolegs, 2021-09-20
@twolegs

You are using a configuration for a different version of webpack. Open the documentation for your version and adjust the dev server configuration (or install the correct version).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question