clean-webpack-plugin icon indicating copy to clipboard operation
clean-webpack-plugin copied to clipboard

clean-webpack-plugin: options.output.path not defined. Plugin disabled...

Open rightaway opened this issue 4 years ago • 15 comments

Issue description or question

With webpack 5.11.1 and clean-webpack-plugin 3.0.0 I get error clean-webpack-plugin: options.output.path not defined. Plugin disabled... when path isn't defined explicitly in the config file. But https://webpack.js.org/configuration/output/#outputpath defines a default path so is this check necessary? It should use the default path if one isn't provided explicitly.

rightaway avatar Jan 01 '21 06:01 rightaway

I get the same error as @rightaway ... What is the solution to apply, please?

PJ-CM avatar Feb 11 '21 11:02 PJ-CM

Finally, that's how it's working fine:

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

const path = require("path");

module.exports = {
    output: {
        path: path.resolve(__dirname, "dist"),
    },
    plugins: [
        new CleanWebpackPlugin({
        cleanOnceBeforeBuildPatterns: [path.join(__dirname, "dist/**/*")],
        }),
    ],
};

PJ-CM avatar Feb 11 '21 20:02 PJ-CM

Is this when using the clean-webpack-plugin together with webpack-dev-server? I'm guessing that Webpack 5 doesn't set output.path when running as dev server, because it keeps the files in memory, so there's nothing to clean. If I'm not mistaken, the solution would probably be to use the clean-webpack-plugin in the production build configuration.

If the author of this module could confirm this, that would be awesome.

evpaassen avatar Feb 17 '21 13:02 evpaassen

Hi @evpaassen ... And how would it be an example of the code refered to "use the clean-webpack-plugin in the production build configuration."? The code I put isn't a good solution?

PJ-CM avatar Feb 17 '21 23:02 PJ-CM

@PJ-CM this issue is about clean-webpack-plugin not respecting webpack 5 defaults. Your solution is not really a solution to the issue :)

smashercosmo avatar Feb 17 '21 23:02 smashercosmo

I think the issue or the question that @rightaway does is about the right configuration of clean-wbpack-plugin for Webpack 5 and how to avoid that mistake.

PJ-CM avatar Feb 18 '21 15:02 PJ-CM

No, it's not about correct configuration. It's about clean-webpack-plugin not taking into account default output.path value when it's not explicitly provided.

smashercosmo avatar Feb 18 '21 15:02 smashercosmo

@PJ-CM - your solution works because you've explicitly configured output.path. If you remove it, clean-webpack-plugin will fail to clean up things.

@evpaassen - the behavior is consistent with any kind of build. As @smashercosmo mentioned, clean-webpack-plugin seems to ignore Webpack 5 default output.path, which Webpack 5 upgrade guide calls out to remove if it is path.resolve(__dirname, 'dist'), as that is the default. Once removed (per the Webpack 5 upgrade guide), clean-webpack-plugin stops working, and the build logs show the message:

clean-webpack-plugin: options.output.path not defined. Plugin disabled...

solimant avatar Feb 18 '21 17:02 solimant

It is worth mentioning that beginning Webpack 5.20.0+, there is an output.clean option that cleans the output directory before emit:

module.exports = {
  //...
  output: {
    clean: true, // Clean the output directory before emit.
  },
};

solimant avatar Feb 18 '21 17:02 solimant

@solimant coool, didn't know that. thx for the tip)

smashercosmo avatar Feb 18 '21 17:02 smashercosmo

So, finally, with the output.clean option, there is no need to use clean-webpack-plugin, right?

PJ-CM avatar Feb 18 '21 18:02 PJ-CM

@PJ-CM - if you're on Webpack 5.20.0+, yes, at least for basic clean-up needs, in my opinion.

solimant avatar Feb 18 '21 19:02 solimant

That's the version I'm using ... Thank you @solimant . Nothing better than immediate responses.

PJ-CM avatar Feb 18 '21 19:02 PJ-CM

See also #197

madarche avatar Apr 22 '21 14:04 madarche

This plugin still has value if using cleanAfterEveryBuildPatterns option, so the original question still stands as to why it is necessary to explicitly specify the output.path.

rbruckheimer avatar Jul 20 '21 18:07 rbruckheimer