Skip to main content

Custom Formatters

Custom formatters allow you to control how ESLint displays linting results. Whether you need JSON for CI/CD integration, HTML reports for dashboards, or custom formats for specific tools, formatters make it possible.

When to Create a Formatter

Create a custom formatter when you need to:
  • Generate reports for CI/CD systems
  • Create HTML dashboards or visualizations
  • Integrate with IDEs or editors
  • Export to specific file formats (XML, CSV, JUnit)
  • Apply custom styling or branding
  • Aggregate results across multiple runs
Built-in Formatters:ESLint includes several built-in formatters: stylish, compact, json, junit, html, and more. Use these as references for creating your own.

Formatter Structure

A formatter is a function that receives results and returns a string:

Synchronous Formatter

my-formatter.js

Async Formatter

my-async-formatter.js

The Results Parameter

The results parameter is an array of LintResult objects:

LintResult Properties

filePath
string
Absolute path to the linted file
messages
LintMessage[]
Array of lint messages for this file
errorCount
number
Number of errors (severity 2)
warningCount
number
Number of warnings (severity 1)
fixableErrorCount
number
Number of errors that can be auto-fixed
fixableWarningCount
number
Number of warnings that can be auto-fixed
source
string
Source code of the file (may be omitted)
suppressedMessages
LintMessage[]
Messages that were suppressed by inline comments

LintMessage Properties

ruleId
string | null
ID of the rule that generated the message
severity
0 | 1 | 2
Message severity: 0 (off), 1 (warn), 2 (error)
message
string
Human-readable error message
line
number
1-based line number where the issue occurs
column
number
1-based column number where the issue occurs
endLine
number
1-based line number where the issue ends
endColumn
number
1-based column number where the issue ends
fix
object
Auto-fix information (if available)
suggestions
array
Alternative fixes that require manual approval

The Context Parameter

The context object provides additional information:
cwd
string
Current working directory
color
boolean | undefined
Whether color output was requested (--color or --no-color)
maxWarningsExceeded
object | undefined
Present if --max-warnings was exceeded
  • maxWarnings: The limit that was set
  • foundWarnings: Actual number of warnings
rulesMeta
object
Metadata for all rules that were run

Example: Summary Formatter

Display only error and warning counts:
Usage:
Output:

Example: Detailed Formatter

Include file paths and rule information:
Output:

Example: JSON Formatter

Format as JSON for tool integration:

Example: HTML Report

Generate an HTML dashboard:

Example: JUnit XML

Generate JUnit-compatible XML for CI systems:

Using Environment Variables

Customize formatter behavior with environment variables:
Usage:

Terminal-Friendly Output

Modern terminals support clickable links:
Output (clickable in many terminals):

Color Support

Add colors when requested:

Creating Your Formatter

1

Create Formatter File

my-formatter.js
2

Test Locally

3

Add Features

Enhance with:
  • Summary statistics
  • Color support
  • Rule metadata
  • Links to documentation
4

Package for Distribution

Create an npm package (optional):
package.json

Using a Custom Formatter

Local File

npm Package

Packaging a Formatter

1

Create Package Structure

2

Configure package.json

3

Implement Formatter

index.js
4

Publish

Testing Formatters

Advanced Patterns

Piping to External Tools

For complex formatting, use ESLint’s JSON output:
Your tool receives:

Async Formatters

Perform asynchronous operations:

Stateful Formatters

Maintain state across runs (use with caution):

Best Practices

Follow these guidelines:
  1. Always return a string - Even if empty
  2. Handle empty results - Check for files with no messages
  3. Escape special characters - Especially in HTML/XML formatters
  4. Make output parseable - Use consistent formats
  5. Document your format - Explain the output structure
  6. Test with edge cases - No results, many errors, long paths

Error Handling

Real-World Examples

Learn from ESLint’s built-in formatters:

stylish

Default formatter with colors and summary

compact

One line per message for easy parsing

json

JSON output for tool integration

html

HTML report generator

junit

JUnit XML for CI systems

checkstyle

Checkstyle XML format

Community Formatters

Explore formatters on npm:
  • eslint-formatter-table - Table format
  • eslint-formatter-gitlab - GitLab code quality format
  • eslint-formatter-pretty - Enhanced stylish formatter
  • eslint-formatter-summary - Aggregated summary

Next Steps

Create a Plugin

Bundle formatter with rules and configs

Built-in Formatters

Explore all built-in formatters