Prettier Plugin for CODEOWNERS
raw JSON → 0.1.1 verified Sat Apr 25 auth: no javascript
A Prettier plugin (v0.1.1) that formats CODEOWNERS files used by GitHub to define code ownership. It automatically detects files named CODEOWNERS in any directory and applies formatting rules such as aligning owner columns per section, sorting owners alphabetically per line, preserving inline comments, normalizing whitespace, and collapsing blank lines. It requires Prettier ^3.0.0 as a peer dependency and is available on npm with active maintenance. Unlike generic file formatters, it understands CODEOWNERS syntax specific sections and no-owner lines.
Common errors
error Error: Cannot find module 'prettier-plugin-codeowners' ↓
cause Plugin not installed or not in node_modules.
fix
Run 'npm install -D prettier-plugin-codeowners'
error Error: Cannot resolve parser 'codeowners' ↓
cause Plugin not loaded in Prettier config or CLI flags.
fix
Add the plugin to .prettierrc { "plugins": ["prettier-plugin-codeowners"] } or use --plugin=prettier-plugin-codeowners in CLI.
error Error: prettier-plugin-codeowners requires prettier version ^3.0.0 ↓
cause Incompatible Prettier version installed.
fix
Update Prettier to version 3.x: 'npm install -D prettier@latest'
Warnings
gotcha Column alignment is per-section, not global across file. If you have multiple sections separated by blank lines, each section will have its own alignment width. ↓
fix Add blank lines between sections to control alignment boundaries.
gotcha Inline comments with no text after # are dropped entirely. A line like '*.js @owner #' becomes '*.js @owner'. ↓
fix Add text after # if you want to keep the comment, or remove # if you want the pattern with no comment.
gotcha Leading and trailing blank lines in the file are stripped. If your CODEOWNERS relies on leading whitespace semantically, it will be removed. ↓
fix Do not depend on leading blank lines for formatting.
deprecated Prettier 2.x is not supported. This plugin requires Prettier ^3.0.0. ↓
fix Upgrade Prettier to version 3.x.
Install
npm install prettier-plugin-codeowners yarn add prettier-plugin-codeowners pnpm add prettier-plugin-codeowners Imports
- default plugin
const plugin = require('prettier-plugin-codeowners'); - plugin (ESM) wrong
import { plugin } from 'prettier-plugin-codeowners';correctimport plugin from 'prettier-plugin-codeowners'; - CLI usage wrong
prettier --write .github/CODEOWNERS --parser=codeownerscorrectprettier --write .github/CODEOWNERS --plugin=prettier-plugin-codeowners
Quickstart
// Install:
// npm install -D prettier prettier-plugin-codeowners
// 1. Add to .prettierrc:
// {
// "plugins": ["prettier-plugin-codeowners"]
// }
// 2. Format a CODEOWNERS file:
// prettier --write .github/CODEOWNERS
// 3. Or use programmatically:
import * as prettier from 'prettier';
import plugin from 'prettier-plugin-codeowners';
const source = `* @owner1
*.js @owner2
`;
const formatted = await prettier.format(source, {
plugins: [plugin],
});
console.log(formatted);
// Output:
// * @owner1
// *.js @owner2