Home > database >  Webpack Dev Server Config - contentBase not working
Webpack Dev Server Config - contentBase not working

Time:12-11

I'm trying to setup a webpack dev server but for a reason, I'm running into an error.

[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?, server?, setupExitSignals?, static?, watchFiles?, webSocketServer? }

I did install all the needed packages globally and tried some other suggestions but I cannot get it to work.

This is the config:

const path = require('path');

module.exports = {
    entry: './app/Main.js',
    output: {
        publicPath: '/',
        path: path.resolve(__dirname, 'app'),
        filename: 'bundled.js',
    },
    mode: 'development',
    devtool: 'source-map',
    devServer: {
        port: 3000,
        contentBase: path.join(__dirname, 'app'),
        hot: true,
        historyApiFallback: { index: 'index.html' },
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            '@babel/preset-react',
                            ['@babel/preset-env', { targets: { node: '12' } }],
                        ],
                    },
                },
            },
        ],
    },
};

My files:
enter image description here

Looking forward to ur answers! Thanks

CodePudding user response:

I can assume the error appeared after migration to the latest version of Webpack/DevServer, they did several breaking changes, including devServer settings. Especially for this issue try to use this code instead of contentBase:

  devServer: {
    static: {
      directory: path.resolve(__dirname, 'app'),
    },
   ...

Here is the whole migration guide that can help https://github.com/webpack/webpack-dev-server/blob/master/migration-v4.md

  • Related