“Modules” is not allowed Error

2019-07-29 13:50发布

问题:

Webpack 2 comes with the new resolve setting and when I try to modify the setting in config, as follows, webpackValidator throws an error saying

"Modules" is not allowed"

const config = webpackValidator({

    context: resolve('src'),
    entry: './app.js',
    output: {
      filename: 'bundle.js',
      path: resolve('dist'),
      publicPath: '/dist/',
      pathinfo: ifNotProd(),
    },
    devtool: ifProd('source-map', 'eval'),
    module: {
      loaders: [
        {
          test: /\.js$/,
          loaders: ['babel'],
          exclude: /node_modules/
        }
      ]
    },
    resolve: {
      extensions: ['.js'],
      modules: [
        resolve(__dirname, 'src'),
        resolve(__dirname, 'node_modules')
      ]
    },
    plugins: [
      new DashboardPlugin()
    ]
  })

回答1:

As mentioned on the webpack-validator project's readme (npm & github), you should seriously consider giving up the use of this package...

Note: webpack v2 has built-in validation for configuration. Due to this, webpack-validator is unlikely to make significant changes. While pull requests will be reviewed and can be merged, project maintainers are unlikely to put a lot of much effort into the maintenance of the project.

I had the same issue and finally gave up its use : Webpack 2 introduced breaking changes that surely won't be followed by the webpack-validator project. The new built-in validation for configuration in Webpack 2 is now good enough.

By the way, your config may need a few improvements :

const config = {

context: resolve('src'),
entry: './app.js',
output: {
  filename: 'bundle.js',
  path: resolve('dist'),
  publicPath: '/dist/',
  pathinfo: ifNotProd(),
},
devtool: ifProd('source-map', 'eval'),
module: {
  rules: [
    {
      test: /\.js$/,
      use: ['babel-loader'],
      exclude: /node_modules/
    }
  ]
},
resolve: {
  extensions: ['.js'],
  modules: [
    resolve(__dirname, 'src'),
    resolve(__dirname, 'node_modules')
  ]
},
plugins: [
  new DashboardPlugin()
]
}
  • module.loaders becomes module.rules
  • module.loaders.loaders becomes module.rules.use
  • You can't use the shortcut 'babel' instead of 'babel-loader' any more, unless you specify it (resolveLoader.moduleTemplates property).