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

24,711

Solution 1

You are importing merge incorrectly. Try it like this:

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

UPD: Based on the following changelog, starting with webpack-merge 5.0.3 and higher, you should use the code I provided above. If the version is lower than 5.0.3, then you need to use:

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

Solution 2

Also you can do this using cmd.

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

or you can create a script in package.json for faster use

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

Related videos on Youtube

Baby Coder
Author by

Baby Coder

Went to school for game design....ended up a software developer/web developer I usually code in: JavaScript React But I also know Java C# Jquery (although i'm not very good at this) PhP (or this) C++ (I did this for a internship and barely know it)

Updated on July 09, 2022

Comments

  • Baby Coder
    Baby Coder almost 2 years

    I'm using webpack-merge to combine two webpack.config files together but I keep getting the error "TypeError: merge is not a function when I run the command "webpack --config ./config/webpack.config.prod.js"

    Has anybody else run across this issue?

    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
     
    })
    
  • Baby Coder
    Baby Coder almost 4 years
    that fixed it I was confused because I have another project that has it written the other way and it works fine I'll assume it's a version issue
  • RICHARD ABRAHAM
    RICHARD ABRAHAM over 3 years
    Yes, it is a version issue as it is working without the curly brackets in Webpack v^4.2.2 but not in v^5.1.1
  • Muhammed Moussa
    Muhammed Moussa about 3 years
    not working "webpack": "^5.3.2", "webpack-merge": "^4.2.2"
  • Rustam D9RS
    Rustam D9RS about 3 years
    Based on the following changelog, starting with webpack-merge 5.0.3 and higher, you should use the code that I gave in the solution. If the version is lower than 5.0.3, then you need to use const merge = require('webpack-merge');.
  • TommyAutoMagically
    TommyAutoMagically over 2 years
    Breaking changes like this are part of why Javascript development can be incredibly frustrating. (Thanks, btw!)