Webpack Warnings to Errors Plugin
The `warnings-to-errors-webpack-plugin` is a utility for Webpack that elevates all build warnings to errors, ensuring a stricter compilation process. This plugin is currently at version 2.3.0 and is actively maintained, primarily releasing updates to ensure compatibility with new major versions of Webpack or to incorporate enhanced filtering capabilities. Its core differentiator is enforcing a zero-warning policy, which is particularly beneficial in continuous integration/continuous deployment (CI/CD) environments where subtle build warnings could otherwise be overlooked, potentially leading to runtime issues. It integrates seamlessly with Webpack's native `warningsFilter` and `ignoreWarnings` options, allowing developers to selectively exempt certain warnings from being promoted to errors.
Common errors
-
TypeError: WarningsToErrorsPlugin is not a constructor
cause The plugin class was not correctly imported or instantiated in the Webpack configuration.fixEnsure you are using `new WarningsToErrorsPlugin()` in your `plugins` array. If using CommonJS, use `const WarningsToErrorsPlugin = require('warnings-to-errors-webpack-plugin');`. For ESM, use `import WarningsToErrorsPlugin from 'warnings-to-errors-webpack-plugin';`. -
Module not found: Error: Can't resolve 'warnings-to-errors-webpack-plugin'
cause The package `warnings-to-errors-webpack-plugin` is not installed or not resolvable by Node.js.fixInstall the package as a development dependency: `npm install --save-dev warnings-to-errors-webpack-plugin` or `yarn add -D warnings-to-errors-webpack-plugin`. -
Webpack compilation failed with errors. (followed by a list of 'Module Warning (from ...)' entries)
cause The `warnings-to-errors-webpack-plugin` successfully promoted one or more Webpack warnings into critical errors, halting the build.fixExamine the detailed error output to identify the specific warnings. Either fix the underlying cause of the warning in your code or add a rule to `ignoreWarnings` (Webpack 5) or `stats.warningsFilter` (Webpack 2-4) in your Webpack configuration to explicitly suppress that warning.
Warnings
- gotcha The `peerDependencies` of `warnings-to-errors-webpack-plugin` (`^2.2.0-rc || ^3 || ^4 || ^5`) indicate broad compatibility across Webpack versions 2 through 5. However, older documentation or general 'Breaking Changes' sections might state a requirement for `webpack >= 4.0.0`, which can be confusing. Always refer to the `peerDependencies` in `package.json` for the most accurate compatibility information.
- breaking Webpack 5 deprecated `stats.warningsFilter` in favor of the new `ignoreWarnings` option. While the plugin is designed to work with both, projects upgrading to Webpack 5 should migrate their warning exclusion logic from `stats.warningsFilter` to `ignoreWarnings` for future compatibility and best practice.
- gotcha Enabling this plugin fundamentally changes Webpack's error reporting. Any warning, no matter how minor, will halt the build process with an error exit code. This is the plugin's intended behavior but can be unexpected if developers are not accustomed to a strict build environment, potentially failing CI/CD pipelines that previously passed with warnings.
- gotcha When using this plugin to prevent asset emission on warnings, the configuration differs between Webpack versions. For Webpack v4 and v5, you should use `optimization.noEmitOnErrors: true`. For older versions (v2 and v3), you need to explicitly add `new webpack.NoEmitOnErrorsPlugin()` to your plugins array.
Install
-
npm install warnings-to-errors-webpack-plugin -
yarn add warnings-to-errors-webpack-plugin -
pnpm add warnings-to-errors-webpack-plugin
Imports
- WarningsToErrorsPlugin
import WarningsToErrorsPlugin from 'warnings-to-errors-webpack-plugin';
const WarningsToErrorsPlugin = require('warnings-to-errors-webpack-plugin'); - WarningsToErrorsPlugin
import { WarningsToErrorsPlugin } from 'warnings-to-errors-webpack-plugin';import WarningsToErrorsPlugin from 'warnings-to-errors-webpack-plugin';
- WarningsToErrorsPlugin (TypeScript Type)
import WarningsToErrorsPlugin from 'warnings-to-errors-webpack-plugin'; // ... const plugin: WarningsToErrorsPlugin = new WarningsToErrorsPlugin();
Quickstart
const WarningsToErrorsPlugin = require('warnings-to-errors-webpack-plugin');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: require('path').resolve(__dirname, 'dist'),
},
plugins: [
new WarningsToErrorsPlugin(),
],
// Example for Webpack 5 ignoring a specific warning pattern
ignoreWarnings: [
{ message: /Critical dependency: the request of a dependency is an expression/ }
],
// For Webpack v2, v3, and v4, use stats.warningsFilter instead of ignoreWarnings
// stats: {
// warningsFilter: [/some warning pattern/]
// }
};