61

When I upgrade webpack to 4.0.0-beta.3 and run npx webpack serve I get this error:

[webpack-cli] Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
 - configuration has an unknown property 'contentBase'. These properties are valid:
   object { bonjour?, client?, compress?, devMiddleware?, firewall?, headers?, historyApiFallback?, host?, hot?, http2?, https?, liveReload?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, port?, proxy?, public?, setupExitSignals?, static?, transportMode?, watchFiles? }

This is my webpack.config.js that works in 3.11.2:

const path = require('path');
const ArcGISPlugin = require("@arcgis/webpack-plugin");

module.exports = {
  mode: 'development',
  entry: {
      main: './app/main.js'
  },
  plugins: [
      new ArcGISPlugin()
  ],
  devServer: {
      contentBase: './'
  }
}

my devDependencies from package.json:

  "devDependencies": {
    "@arcgis/webpack-plugin": "^4.18.0",
    "dojo-typings": "^1.11.11",
    "webpack-cli": "^4.7.2",
    "webpack-dev-server": "^4.0.0-beta.3"

How do I need to update my config to get the latest version working? When I take out the dev server object, the server will run, but serve content out of ./public which doesn't exist.

I'm new to webpack, so I'm not yet familiar with the application, config, and requirements.

roms
  • 1,511
  • 1
  • 11
  • 12

10 Answers10

78
devServer: {
  static: './'
}

I should read the errors more closely. The above object made my config work again.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
roms
  • 1,511
  • 1
  • 11
  • 12
38

Use static instead as contentBase is deprecated in latest Webpack v5

  devServer: {
    static: {
      directory: path.join(__dirname, "./")
    },

Full details: https://webpack.js.org/configuration/dev-server/#devserver

rottitime
  • 1,653
  • 17
  • 29
11
  devServer: {
    static: {
      directory: path.join(__dirname, "public")
    },

    compress: true,
    port: 3010, // default 8000
  },
skyTzy
  • 137
  • 1
  • 5
  • 5
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Feb 07 '22 at 00:26
5

Use static: './directory-name' instead of contentBase: './directory-name'

Example:

devServer: {
  static: './dist'
}
ggorlen
  • 44,755
  • 7
  • 76
  • 106
iamtheasad
  • 1,017
  • 13
  • 15
3
instead of contentBase we are using static



enter code here
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,
    static: {
      directory: 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" } }]]
          }
        }
      }
    ]
  }
};
Jai Kishan
  • 31
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 10 '22 at 09:33
1

I was using node v18, I removed and installed v16. I then changed the codeBase to static. It worked for me

devServer: { historyApiFallback: true, static: path.resolve(__dirname, './dist'), open: true, compress: true, hot: true, port: 8080, host: 'localhost' },

Kip
  • 11
  • 1
1

Hot Module Reload in Webpack 5 is enabled by:

devServer: {
   port: 8080,
   hot: "only",
   static: {
      directory: path.join(__dirname, './'),
      serveIndex: true,
    },
 },
Maksym Dudyk
  • 1,082
  • 14
  • 16
1

Use static instead contentBase has solved my problem.

  • 3
    There's already several answers saying use `static` instead of `contentBase`. You should upvote those answers rather than duplicating them – K Scandrett Apr 16 '23 at 06:37
0

Also important to set clean to false. It happened to me...

webpackConfig = {
  output: {
    clean: false
  }
}
Carlos Oliveira
  • 846
  • 10
  • 23
0

Simply delete contentBase from you config options

delete webpackConfig.devServer.contentBase

EX: delete webpackConfig.devServer.contentBase

return merge(webpackConfig, {. ...your remaining code

Rajesh Pal
  • 189
  • 1
  • 6