Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Webpack, is it possible to force warnings to be treated as errors?

This is very common with other compiler toolchains like GCC via -Werror.

It's very useful for scenarios where you are required to follow strict guidelines and want to treat builds with warnings as errors and return a non-zero status code.

I couldn't find anything in Webpack's docs about this -- is it possible via the CLI?

Thanks!

(Latest version of Webpack v4.41.5 as of the time of writing this question)

like image 366
fisch2 Avatar asked Sep 02 '25 06:09

fisch2


2 Answers

There's an NPM package that does this for you: Webpack - Warnings To Errors

There are a couple of things you can configure on your own:

stats: {
    logging: 'info', //  errors, warnings, and info messages
    warnings: true
},
output: {
    strictExportPresence: true // will throw error if import is missing, usually warning
}

Otherwise, create your own function for this:

if (compilation.warnings.length > 0) {
  compilation.errors = compilation.errors.concat(compilation.warnings);
  compilation.warnings = [];
}

compilation.children.forEach((child) => {
  if (child.warnings.length > 0) {
    child.errors = child.errors.concat(child.warnings);
    child.warnings = [];
  }
});
like image 122
Joel Avatar answered Sep 04 '25 23:09

Joel


For Webpack 5 you can use the CLI flag --fail-on-warnings.

like image 20
cheshire Avatar answered Sep 05 '25 01:09

cheshire