7

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

1

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

Comments

0

Full code is here

const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
require('babel-register')

module.exports = {
    entry: ['@babel/polyfill', './src/app.js'],
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: ['babel-loader']
            }
        ]
    },
    plugins: [
        new htmlWebpackPlugin({
            template: './index.html'
        })
    ],
    mode: 'development',
    devtool: 'inline-source-map',
        devServer: {
            static: {
              directory: path.resolve(__dirname, 'app'),
            },
        }
}

1 Comment

Consider adding a couple of lines explaining the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.