Alaris
Alaris

Reputation: 103

Webpack Dev Server Config - contentBase not working

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

Upvotes: 7

Views: 5194

Answers (2)

jam j
jam j

Reputation: 550

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'),
            },
        }
}

Upvotes: 0

Źmicier Jaraševič
Źmicier Jaraševič

Reputation: 371

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

Upvotes: 8

Related Questions