179

my output of error:

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema. - configuration.module has an unknown property 'loaders'. These properties are valid: object { exprContextCritical?, exprContextRecursive?, exprContextRegExp?, exprContextRequest?, noParse?, rules?, defaultRules?, unknownContextCritical?, unknownContextRecursive?, unknownContextRegExp?, unknownContextRequest?, unsafeCache?, wrappedContextCritical?, wrappedContextRecursive?, wrappedContextRegExp?, strictExportPresence?, strictThisContextOnImports? } -> Options affecting the normal modules (NormalModuleFactory).

my webpack.config.js:

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

var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');

var config = {
  entry: APP_DIR + '/index.jsx',
  module : {
    loaders : [
      {
        test : /\.jsx?/,
        include : APP_DIR,
        loader : 'babel-loader'
      }
    ]
  },
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  }

};


module.exports = config;

my webpack version:

webpack@4.1.1
S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254

6 Answers6

404

You should change loaders to rules in webpack 4:

change:

loaders 

to:

rules

source: Loaders

Example:

module.exports = {
  module: {
    rules: [
      { test: /\.css$/, use: 'css-loader' },
      { test: /\.ts$/, use: 'ts-loader' }
    ]
  }
};
Edgar
  • 6,022
  • 8
  • 33
  • 66
S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254
16

Use rules in webpack 4 instead of loaders.

https://webpack.js.org/concepts/loaders/

4

You should use the migration utility to migrate your webpack config files, it worked for me.

The migration documentation is also useful.

MattG
  • 5,589
  • 5
  • 36
  • 52
3

Above given answers are working but we can resolve this issue by changing webpack and webpack-dev-server version to

"webpack": "3.8.1",
"webpack-dev-server": "2.9.4"

It can also solve the issue. Hope it will help.

Anshul
  • 464
  • 4
  • 14
  • 1
    I gave you an uptick. Only problem is that people want to up versions of frameworks and tools to get new features not go back to older versions just to ensure they don't have to change their config files. Perhaps taking the pain to change the config file to align with the new way the tool works is not bad. Unless that is impossible. – Eniola Mar 10 '19 at 18:17
  • 1
    Thanks @Eniola, Yeah you are right. As I mentioned the S.M_Emamian answer is correct but my answer will work if someone agrees to work will the older version of webpack. – Anshul Mar 12 '19 at 08:27
2

Working for me below webpack.config.js

module.exports = {
    entry: [
        '.src/index.js'
    ],
    output:{
        path: __dirname,
        filename: 'app/js/main.js'
    },
    module:{
        rules: [
          { test: /\.css$/, use: 'css-loader' },
          { test: /\.ts$/, use: 'ts-loader' }
        ]
    }
}
Pankaj Upadhyay
  • 2,114
  • 21
  • 22
0

I am using Webpack 5 and I removed below config from my webpack.config. It worked for me after removing. It may help some other people who still facing error

        {
            test: /\.css$/,
            use: ['style-loader', 'css-loader']
        },
Ambuj Khanna
  • 1,131
  • 3
  • 12
  • 32