8

I am using webpack: 5.71.0 webpack-cli: 4.9.2 webpack-dev-server 4.8.1

in my webpack.config.js

const path = require('path');
const webpack = require('webpack');

module.exports = {
    entry: './src/index.js',
    mode: 'development',
    module: {
        rules: [{
                test: /\.(js|jsx)$/,
                exclude: /(node_modules)/,
                loader: 'babel-loader',
                options: { presets: ["@babel/env"] }
            },
            {
                test: /\.css$/,
                use: ["style-loader", "css-loader"]
            }
        ]
    },
    resolve: { extensions: ['*', '.js', '.jsx'] },
    output: {
        path: path.resolve(__dirname, 'dist/'),
        publicPath: '/dist/',
        filename: './bundle.js'
    },
    devServer: {
        static: path.join(__dirname, 'public/'),
        publicPath: 'http://localhost:3000/dist/',
        port: 3000,
        hot: "only"
    },
    plugins: [new webpack.HotModuleReplacementPlugin()]
};

I got the following 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 'publicPath'. 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?, setupMiddlewares?, static?, watchFiles?, webSocketServer? }

what is the new alternative to publicPath?

  • I would highly recommend not using vanilla webpack in favour of better tools like vite, next, astro, or even CRA. While it is an opinion you're not asking for, it simplifies most of the problems you encounter (including this one) - and doesn't require you to manually add style-loader and babel-loader. – Angshu31 Aug 18 '22 at 13:02

3 Answers3

16

Looks like your webpack config was written using v3 of webpack-dev-server. The schema has changed for v4 as per webpack-dev-server v4 migration guide

So your devServer block should place publicPath within the devMiddleware section, i.e.:

devServer: {
  static: path.join(__dirname, 'public/'),
  devMiddleware: {
    publicPath: '/dist/'
  },
  port: 3000,
  hot: "only"
},
Steve Jefferies
  • 589
  • 5
  • 13
1

You probably don't need to set publicPath in the devServer configuration at all; it's probably read from output.publicPath.

Try to just get rid of it and see if things still work. (They should.)

AKX
  • 152,115
  • 15
  • 115
  • 172
1

You can try this code

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, 'public/'),
        },
        devMiddleware: {
            publicPath: '/dist/'
        },
        port: 3000,
        hot: "only"
    }
}
jam j
  • 11
  • 2
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 24 '23 at 06:01