You need to enable JavaScript to run this app. Webpack 5.47.1配置对象不匹配API schema报错问题排查
优惠活动
大模型
产品
解决方案
定价
更多

Webpack 5.47.1配置对象不匹配API schema报错问题排查

问题排查结果

核心错误原因:mode 配置项位置错误

Webpack 5 的 mode 是顶级配置属性,不能放在 module 对象下,你当前把 mode: 'development' 写在了 module 配置块内,不符合 Webpack 的 API schema 要求,这是触发报错的直接原因。

其他需要修正的配置问题

  • 移除多余的 json-loader 配置:Webpack 5 原生支持 JSON 格式文件的解析,不需要额外配置 json-loader,保留该配置反而会触发重复解析的冲突错误
  • 可选优化:webpack.ProvidePlugin 中 whatwg-fetch 的注入写法可以更新为符合 Webpack 5 的规范,避免后续加载器的兼容警告

修正后的配置参考

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

process.noDeprecation = true;

module.exports = (options) => ({
  // mode移动到顶级配置位置
  mode: 'development',
  entry: options.entry,
  output: Object.assign({ // Compile into js/build.js
    path: path.resolve(process.cwd(), 'build'),
    publicPath: '/',
  }, options.output), // Merge with env dependent settings
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: options.babelQuery,
        },
      },
      {
        test: /\.scss$/,
        exclude: /node_modules/,
        use: ['style-loader', 'css-loader', 'sass-loader'],
      },
      {
        test: /\.css$/,
        exclude: /node_modules/,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.css$/,
        include: /node_modules/,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.(eot|svg|otf|ttf|woff|woff2)$/,
        use: 'file-loader',
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/i,
        use: [
          'file-loader',
          {
            loader: 'image-webpack-loader',
            options: {
              progressive: true,
              optimizationLevel: 7,
              interlaced: false,
              pngquant: {
                quality: '65-90',
                speed: 4,
              },
            },
          },
        ],
      },
      {
        test: /\.html$/,
        use: 'html-loader',
      },
      {
        test: /\.(mp4|webm)$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 10000,
          },
        },
      },
    ],
  },
  plugins: options.plugins.concat([
    new webpack.ProvidePlugin({
      // make fetch available
      fetch: 'exports-loader?self.fetch!whatwg-fetch',
    }),

    new webpack.DefinePlugin({
      'process.env.NODE_ENV' : JSON.stringify('development'),
    }),
    // new webpack.NamedModulesPlugin(),
  ]),
  resolve: {
    modules: ['app', 'node_modules'],
    extensions: [
      '.js',
      '.jsx',
      '.react.js',
    ],
    mainFields: [
      'browser',
      'jsnext:main',
      'main',
    ],
  },
  devtool: options.devtool,
  target: 'web', // Make web variables accessible to webpack, e.g. window
  performance: options.performance || {},
});

内容的提问来源于stack exchange,提问作者Matarishvan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 03:54:04