vfile-reporter: Report Generator
vfile-reporter is a utility package within the vfile ecosystem, designed to generate human-readable textual reports from one or more vfile instances. These reports summarize messages (warnings, errors, and info) that occurred during file processing, mimicking the output of tools like ESLint or esbuild. The current stable version is 8.1.1. The package maintains an active release cadence, with minor updates addressing fixes and features, and major versions typically aligning with Node.js LTS updates or breaking changes in its core dependency, `vfile`. Key differentiators include its tight integration with the vfile specification, extensive configuration options for output customization (e.g., color, verbosity, quiet/silent modes), and its role as a standard reporting mechanism for unified (remark, rehype, etc.) processors.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module .../node_modules/vfile-reporter/index.js from ... not supported.
cause `vfile-reporter` is an ES Module and cannot be loaded with CommonJS `require()`.fixUse `import { reporter } from 'vfile-reporter'` in an ES Module context. Ensure your `package.json` has `"type": "module"` or your file ends with `.mjs`. -
TypeError: Cannot read properties of undefined (reading 'message') or similar when `files` is not a `VFile` instance.
cause The `reporter` function expects an array of `VFile` instances or a single `VFile` instance.fixAlways pass valid `VFile` objects. Ensure `vfile` is correctly imported and instantiated: `import { VFile } from 'vfile'; new VFile(...)`. -
Error: Cannot find module 'vfile' or similar dependency error.
cause `vfile`, a core dependency of `vfile-reporter`, is not installed or resolved correctly.fixEnsure `vfile` is installed as a dependency: `npm install vfile`. -
SyntaxError: Named export 'reporter' not found. The requested module 'vfile-reporter' does not provide an export named 'reporter'
cause This error might occur if you are trying to use a named import (e.g., `{ reporter }`) on an older version that only supported default exports, or if your bundler configuration is incorrect.fixEnsure you are using `vfile-reporter` v7.0.1 or later for named exports. Try `import reporter from 'vfile-reporter'` if you suspect it's a default-only export issue or check your build configuration.
Warnings
- breaking Version 8.0.0 of `vfile-reporter` dropped support for Node.js versions older than 16. Running on earlier Node.js versions will result in runtime errors.
- breaking Since version 7.0.0, `vfile-reporter` is an ESM-only package. Attempting to `require()` it in a CommonJS module will throw an `ERR_REQUIRE_ESM` error.
- breaking Version 8.0.0 removed direct support for 'errors' and 'nullish files'. Previously, the reporter might have handled these implicitly. Users are now expected to handle such conditions explicitly before passing files to the reporter.
- breaking Version 8.0.0 changed to use an `export` map, which means private internal APIs (e.g., direct deep imports) are no longer accessible and will break. The package intends for only its public API to be used.
- breaking Version 7.0.0 updated its dependency on `vfile` to `v5.0.0`. If your project also uses `vfile` directly, ensure compatibility with `vfile@5.0.0` as it may contain its own breaking changes.
Install
-
npm install vfile-reporter -
yarn add vfile-reporter -
pnpm add vfile-reporter
Imports
- reporter
const { reporter } = require('vfile-reporter')import { reporter } from 'vfile-reporter' - reporter
import reporter from 'vfile-reporter'
- Options
import type { Options } from 'vfile-reporter'
Quickstart
import { VFile } from 'vfile';
import { reporter } from 'vfile-reporter';
// Create some VFile instances
const fileOne = new VFile({
path: 'src/module-a.js',
value: 'console.log(\"Hello\")'
});
const fileTwo = new VFile({
path: 'src/module-b.js',
value: 'const x = 1;'
});
// Add messages to fileOne
fileOne.message('This is a warning!', { line: 1, column: 9 }, 'linter:warning-id');
fileOne.message('Another issue at line 2.', 2);
// Use the reporter to generate a report for both files
// console.error is often used for reports to stderr
const report = reporter([fileOne, fileTwo], { verbose: true });
console.error(report);
/* Expected output (may vary slightly based on environment):
src/module-a.js
1:9 warning This is a warning! linter:warning-id
2:1 warning Another issue at line 2.
src/module-b.js: no issues found
⚠ 2 warnings
*/