webpack-format-messages

raw JSON →
3.0.1 verified Sat Apr 25 auth: no javascript

Beautiful formatting for Webpack messages, ported from Create React App. Version 3.0.1 is the current stable release, with TypeScript definitions and native ESM support via the `exports` map. This package extracts and prettifies Webpack error and warning messages from a stats object, providing clean console output similar to Create React App. It differs from `react-dev-utils/formatWebpackMessages` by accepting a Webpack stats object directly (not the toJson() output) and by being a lightweight standalone install (under 10 kB) without the heavy `react-dev-utils` dependency. Key differentiators: zero browser dependency, pure Node.js environment, and a simple API with two functions (`formatMessages` and `formatMessage`). Release cadence is sporadic; last major release was v3.0.0 with breaking changes to Node.js 13.0-13.7 support.

error Cannot find module 'webpack-format-messages'
cause Package not installed or not in node_modules.
fix
Run npm install --save-dev webpack-format-messages
error TypeError: formatMessages is not a function
cause Importing the wrong export: default vs named.
fix
Use import formatMessages from 'webpack-format-messages' or const { formatMessages } = require('webpack-format-messages')
error Error [ERR_REQUIRE_ESM]: require() of ES Module not supported
cause Node.js version < 12 or missing `--experimental-require-module` flag; package has ESM exports map.
fix
Upgrade Node to >= 14, or use dynamic import(webpack-format-messages) or set type: 'module' in package.json.
breaking Node.js 13.0 - 13.7 users may experience import errors due to the `exports` map added in v3.0.0.
fix Upgrade to Node >= 13.8 or use `--experimental-exports` flag, or downgrade to v2.x.
deprecated Use of CommonJS require without destructuring may rely on the default export, which is deprecated in favor of named exports for clarity.
fix Use `const { formatMessages } = require('webpack-format-messages')` or `import formatMessages from ...`.
gotcha The function expects a Webpack `stats` object, not the JSON from `stats.toJson()`.
fix Pass the raw stats object from the done callback (the argument provided to the hook).
npm install webpack-format-messages
yarn add webpack-format-messages
pnpm add webpack-format-messages

Creates a Webpack compiler, taps the done hook, and uses formatMessages to pretty-print errors and warnings.

const webpack = require('webpack');
const formatMessages = require('webpack-format-messages');

const compiler = webpack({
  // minimal config
  entry: './src/index.js',
  mode: 'development',
});

compiler.hooks.done.tap('report', (stats) => {
  const messages = formatMessages(stats);
  if (messages.errors.length) {
    console.log('Errors:\n' + messages.errors.join('\n'));
  }
  if (messages.warnings.length) {
    console.log('Warnings:\n' + messages.warnings.join('\n'));
  }
});

compiler.run((err) => { if (err) console.error(err); });