Prettier Plugin for Firebase Realtime Database Rules
The `prettier-plugin-firebase-database` package, currently at version 2.0.0, is a specialized Prettier plugin designed to format Firebase Realtime Database Rules. It enforces a consistent code style for `*.rules` files (e.g., `database.rules.json`), ensuring readability and maintainability within development teams working with Firebase. As a Prettier plugin, it integrates into existing Prettier setups via configuration, providing custom parsing and formatting logic tailored to the unique JSON-like syntax of Firebase security rules. The project follows Prettier's major release cadence, with updates typically occurring to maintain compatibility with new Prettier versions or to adapt to changes in Firebase rules syntax. Its key differentiator is providing specific, correct formatting for Firebase rules, which generic JSON formatters often handle incorrectly.
Common errors
-
[error] Unknown parser "prettier-plugin-firebase-database" for file "your-file.rules"
cause Prettier could not find or load the `prettier-plugin-firebase-database` plugin, or it's not correctly referenced in your configuration.fixEnsure `prettier-plugin-firebase-database` is installed as a dev dependency and correctly listed in the `plugins` array of your `prettier.config.js`. If using package managers like Yarn PnP, check plugin resolution. -
Error: Cannot find module 'prettier' (or similar during npm install/yarn add)
cause The `prettier-plugin-firebase-database` package has a peer dependency on `prettier` version 3 or higher, but an incompatible or missing version of `prettier` is installed.fixInstall `prettier` version 3 or greater: `npm install --save-dev prettier@latest` or `yarn add --dev prettier@latest`. -
No files matching the pattern were found: "firebase-database.rules"
cause This is a Prettier CLI error, indicating that the command-line pattern provided to `prettier --write` did not find any files.fixVerify the file path and pattern provided to the `prettier --write` command. Ensure the file exists and the glob pattern is correct relative to where you run the command.
Warnings
- breaking Version 2.0.0 and above of `prettier-plugin-firebase-database` requires Prettier version 3 or newer due to a peer dependency. Older versions of Prettier (e.g., v2.x) are not supported and will lead to installation or runtime errors.
- gotcha If Prettier isn't formatting your `.rules` files, ensure the plugin is correctly listed in the `plugins` array of your Prettier configuration file (e.g., `prettier.config.js`). Typos or missing the entry will prevent the plugin from being loaded.
- gotcha The plugin by default attempts to parse `.rules` files. If you have Firebase rules files with different extensions or if auto-detection fails, explicitly configure `overrides` in your `prettier.config.js` to associate specific file patterns with the plugin's parser.
Install
-
npm install prettier-plugin-firebase-database -
yarn add prettier-plugin-firebase-database -
pnpm add prettier-plugin-firebase-database
Imports
- "prettier-plugin-firebase-database"
import * as FirebasePrettierPlugin from 'prettier-plugin-firebase-database';
// In prettier.config.js plugins: ["prettier-plugin-firebase-database"]
- parser: 'prettier-plugin-firebase-database'
parser: 'firebase' || parser: 'rules'
// prettier.config.js, inside overrides.options parser: 'prettier-plugin-firebase-database'
- import("prettier").Config
import { Config } from 'prettier-plugin-firebase-database';/** @type {import("prettier").Config} */ const config = { ... };
Quickstart
// prettier.config.js
/**
* @see https://prettier.io/docs/en/configuration.html
* @type {import("prettier").Config}
*/
const config = {
plugins: ['prettier-plugin-firebase-database'],
// Recommended: Explicitly associate .rules files with the plugin's parser.
// This ensures consistent formatting for all files ending with .rules.
overrides: [
{
files: '*.rules',
options: {
parser: 'prettier-plugin-firebase-database'
}
}
]
};
export default config;