Webpack Stats Plugin
raw JSON → 1.1.3 verified Sat Apr 25 auth: no javascript
A webpack plugin that ingests the compiler's stats object, transforms it, and writes a JSON file (typically 'stats.json') to the output directory. Version 1.1.3 is the current stable release, updated primarily with patch-level maintenance. Key differentiator: it is a dedicated, minimal stats-file writer without the overhead of full-featured analysis tools. It supports custom transforms (sync or Promise), selective field filtering, and works with webpack 4 and 5. Maintained by Formidable Labs.
Common errors
error Error: Cannot find module 'webpack-stats-plugin' ↓
cause Package not installed or installed only as a dev dependency but not required correctly.
fix
Run
npm install --save-dev webpack-stats-plugin and ensure it's in node_modules. error TypeError: webpack_stats_plugin_1.StatsWriterPlugin is not a constructor ↓
cause Wrong import syntax, likely using default import with CommonJS or incorrect require.
fix
Use
const { StatsWriterPlugin } = require('webpack-stats-plugin'); (named export) or import { StatsWriterPlugin } from 'webpack-stats-plugin'; in ESM. error Stats file is empty or missing expected fields ↓
cause The `fields` option restricts included fields; defaults to `["assetsByChunkName"]`.
fix
Set
fields: null to include all stats fields, or explicitly include needed fields like ['assets', 'chunks', 'modules']. error TypeError: data.toJson is not a function ↓
cause The transform function receives the raw stats object directly (not a compiler stats instance) in recent versions.
fix
Access
data directly as the transformed JSON object; do not call .toJson() on it. For custom stats generation, use the stats option described above. Warnings
gotcha The `fields` option defaults to `["assetsByChunkName"]`, so by default not all stats properties are written. ↓
fix Set `fields: null` to include all stats fields, or provide an array of field names you need.
gotcha The `filename` can be a function returning a string, but the function receives the compilation data and must be synchronous. ↓
fix If using a function for `filename`, ensure it returns a string synchronously. For async filename generation, use the `transform` option.
gotcha The plugin does not automatically emit the stats file in development mode or when `emit: false`. By default `emit: true`. ↓
fix If you don't see the stats file, ensure you have `emit: true` (the default) or check that the webpack output path is correct.
gotcha When using webpack 5 with `output.clean: true`, the stats file may be deleted on rebuild if not emitted correctly. ↓
fix Ensure `emit` is `true` (default) and that `filename` is within the output path. The plugin adds the file to the compiler's assets.
Install
npm install webpack-stats-plugin yarn add webpack-stats-plugin pnpm add webpack-stats-plugin Imports
- StatsWriterPlugin wrong
const StatsWriterPlugin = require('webpack-stats-plugin');correctconst { StatsWriterPlugin } = require('webpack-stats-plugin'); - StatsWriterPlugin (ESM) wrong
import StatsWriterPlugin from 'webpack-stats-plugin';correctimport { StatsWriterPlugin } from 'webpack-stats-plugin'; - None wrong
const statsPlugin = require('webpack-stats-plugin');correctconst { StatsWriterPlugin } = require('webpack-stats-plugin');
Quickstart
const { StatsWriterPlugin } = require('webpack-stats-plugin');
module.exports = {
// ... other config
plugins: [
new StatsWriterPlugin({
filename: 'stats.json',
fields: null, // include all fields
transform(data) {
return JSON.stringify(data, null, 2);
},
}),
],
};