TypeError: CleanwebpackPlugin is not a constructor

Solution 1:

The correct one is to use this import:

const { CleanWebpackPlugin } = require('clean-webpack-plugin');

And then instead of passing an array with the distribution folder, change it to

plugins: [
     new CleanWebpackPlugin(),
     //...
]

Solution 2:

I had the same problem, and I solved it in the following way:


    const { CleanWebpackPlugin } = require('clean-webpack-plugin');


    plugins: [
        new CleanWebpackPlugin({
            cleanAfterEveryBuildPatterns: ['dist']
        })
    ]

Solution 3:

I had the same issue today, right now. As you can tell, there was a mismatch between the docs and the actual code. And in fact, you can see in the last commit they merged both to the documentation:

enter image description here

and also to the code:

enter image description here

So I just switched from const CleanWebpackPlugin = require('clean-webpack-plugin') to

const { CleanWebpackPlugin } = require('clean-webpack-plugin');

and it works fine.

I think you may have been caught in between things. Reinstall the npm package and try again, it should work.

I wrote a bit of what you can see in their public repository because very often when sudden changes like this happen, you'll find your own answer in the repo, probably in the last commits. And it's good reading a bit of the code you use, especially if it helps you troubleshoot your issue :)

Solution 4:

With the update you'll need to do the following to include it

const { CleanWebpackPlugin } = require('clean-webpack-plugin');

Then in the array of plugins replace add the following

plugins: [
     new CleanWebpackPlugin(['dist]),
]

with

plugins: [
     new CleanWebpackPlugin(),
]

As the with the update there is no need to pass any parameters as it will remove all files inside webpack's output.path directory, as well as all unused webpack assets after every successful rebuild.