webpackwebpack-4

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)


Solution

  • 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 = [];
      }
    });