Getting Error from webpack-cli: "TypeError: merge is not a function" in webpack config

Getting Error from webpack-cli: "TypeError: merge is not a function" in webpack config

我正在使用 webpack-merge 将两个 webpack.config 文件组合在一起,但我一直收到错误 “类型错误:当我 运行 命令“webpack --config ./config/webpack.config.prod.js“

时,合并不是函数

还有其他人运行解决这个问题吗?

webpack.config.prod.js

const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');
const TerserPlugin = require('terser-webpack-plugin');
const commonConfig= require('./webpack.config.common');
const merge = require('webpack-merge');

module.exports = merge(commonConfig, {

    //config code
 
})

您导入的 merge 不正确。像这样尝试:

const { merge } = require('webpack-merge');

更新: 基于 following changelog,从 webpack-merge 5.0.3 及更高版本开始,您应该使用我上面提供的代码。如果版本低于5.0.3,则需要使用:

const merge = require('webpack-merge');

您也可以使用 cmd 执行此操作。

webpack -m -c ./webpack.config.js -c ./webpack.dev.js

或者您可以在 package.json 中创建一个脚本以便更快地使用

  "scripts": {
    "build:dev": "webpack -m -c ./webpack.config.js -c 
    ./webpack.dev.js",
  },