johnagan / clean-webpack-plugin

A webpack plugin to remove your build folder(s) before building
MIT License
1.96k stars 137 forks source link

v3, return of "TypeError: CleanWebpackPlugin is not a constructor #143" ? #156

Closed pgnd closed 4 years ago

pgnd commented 4 years ago

I'm currently seeing the same error as closed/locked

"TypeError: CleanWebpackPlugin is not a constructor #143"

https://github.com/johnagan/clean-webpack-plugin/issues/143

here, with

npx envinfo --system --binaries --npmPackages clean-webpack-plugin,webpack
    npx: installed 1 in 1.979s

      System:
        OS: Linux 5.2 openSUSE Leap 15.1
        CPU: (4) x64 AMD Phenom(tm) II X4 945 Processor
        Memory: 384.31 MB / 15.64 GB
        Container: Yes
        Shell: 4.4.23 - /bin/bash
      Binaries:
        Node: 12.7.0 - /usr/local/nodejs/bin/node
        Yarn: 1.17.3 - /usr/local/yarn/bin/yarn
        npm: 6.10.0 - /usr/local/nodejs/bin/npm
      npmPackages:
        clean-webpack-plugin: ^3.0.0 => 3.0.0
        webpack: ^4.39.1 => 4.39.1

and

webpack.js

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

on exec, still

./node_modules/webpack-cli/bin/cli.js --env development --debug --progress
    /srv/www/test01/node_modules/webpack-cli/bin/cli.js:93
                    throw err;
                    ^

    TypeError: CleanWebpackPlugin is not a constructor
    ...

is this a new-, or re-, occurrence? or, something obvious I'd missed ?

chrisblossom commented 4 years ago

Since v3 we’ve moved to a named export. Try:

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

pgnd commented 4 years ago

chaging my existing, previously working config

-   const CleanWebpackPlugin     = require('clean-webpack-plugin');
+   const { CleanWebpackPlugin } = require('clean-webpack-plugin');
    exports.clean = path => ({
        plugins: [
            new CleanWebpackPlugin(
                path,
                {
                    dry: false,
                    verbose: true,
                    watch: false,
                    exclude: [],
                    allowExternal: false,
                    beforeEmit: false
                }
            )
        ],
    });

and clears the constructor error.

but, now returns,

Error: clean-webpack-plugin only accepts an options object. See:

https://github.com/johnagan/clean-webpack-plugin#options-and-defaults-optional

1st, just fyi, THAT^^ tag is incorrect; should be

https://github.com/johnagan/clean-webpack-plugin#user-content-options-and-defaults-optional

What's the correct usage now for the options object? If I've missed a doc/example somewhere, could you pls point?

chrisblossom commented 4 years ago

Please read: https://github.com/johnagan/clean-webpack-plugin/issues/106

My guess is you can replace your config with:

new CleanWebpackPlugin({
    dry: true, // remove this once you verify it removes the correct files
    verbose: true,
});
chrisblossom commented 4 years ago

@pgnd is your issue resolved?

pgnd commented 4 years ago

@chrisblossom

Minor add'l tweak did the trick here

with explicit config,

const { CleanWebpackPlugin } = require('clean-webpack-plugin');
exports.clean = path => ({
    plugins: [
        new CleanWebpackPlugin({
                dry: false,
                verbose: true,
                cleanStaleWebpackAssets: true,
                protectWebpackAssets: false,
                cleanOnceBeforeBuildPatterns: ['**/*'],
        })
    ],
});

exec of

 parts.clean([
  PATHS.build,
  PATHS.cache,
    etc
    etc
 ]),

works a charm!

o/

chrisblossom commented 4 years ago

That's great. FYI, your path param doesn't do anything.

const { CleanWebpackPlugin } = require('clean-webpack-plugin');
exports.clean = () => ({
    plugins: [
        new CleanWebpackPlugin({
                dry: false,
                verbose: true,
                cleanStaleWebpackAssets: true, // this can be removed as it is the default
                protectWebpackAssets: false, // I do not see you removing non-webpack assets, not sure why this is set to false.
                cleanOnceBeforeBuildPatterns: ['**/*'], // this can be removed as it is the default
        })
    ],
});

parts.clean();
maggiew61 commented 4 years ago

it only worked me when I tried this stackoverflow answer that has the most upvotes and $npm start (or whatever start means in your package.json)