DEV Community

0cjs! Webpack4 tutorial: Building React app without config file.

Masashi Hirano on February 03, 2018

On January, webpack4 was pre-released as beta.0. On February 25, webpack4 was released 🎉 Webpack4 doesn't need a config file by default! It made me...
Collapse
 
evgenyx82 profile image
Evgenyx82

webpack-dev-middleware + webpack-dev-server +

server.js:
...
app.use(webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath
}));
...

webpack.config.js:
...
new webpack.DefinePlugin({'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)}),
or as u did mention
new webpack.DefinePlugin({'process.env.NODE_ENV': JSON.stringify('production')}),
...

Gives me:
WARNING in configuration
The 'mode' option has not been set. Set 'mode' option to 'development' or 'production' to enable defaults for this environment.

package.json:
...
"webpack": "4.0.1",
"webpack-cli": "2.0.9"
"webpack-dev-middleware": "3.0.0",
"webpack-dev-server": "3.1.0",
...
it seems most latest versions...

How to set 'mode' while using webpack-dev-middleware + webpack-dev-server, any suggestions? ;)

p.s. webpack via command line works fine ;)

Collapse
 
shisama profile image
Masashi Hirano • Edited

Sorry to reply late.
If your webpack.config.js doesn't have 'mode' property, you have to add 'mode' property into your webpack.config.js.

The below is an example.

module.exports = {
  mode: 'production', // <= It is necessary to resolve the warning
  entry: {
    'index': [
      path.resolve(__dirname, 'src/index.js')
    ]
  },
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'public'),
    publicPath: '/',
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
    })
  ],
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        use: 'babel-loader',
      },
    ]
  },
  resolve: {
    extensions: ['.js', '.jsx'],
  },
};

Thank you for reading this post.

Collapse
 
evgenyx82 profile image
Evgenyx82

Hello, Masashi Hirano.

It's seems im miss something in the past, here is examples ) github.com/webpack/webpack/tree/ma...

Anyway, thx ) for response.