---
name: configuring-prettier
description: Set up Prettier with @famiglio/prettier-config, which also formats package.json, shell scripts, TOML, XML, .properties, and .editorconfig. Covers the pnpm hoist pattern its plugins need, the Astro, Svelte, Tailwind, SQL, nginx, and multiline-arrays configurations, mergeConfigs, .prettierignore, and VS Code. Use when working on prettier.config.*, or when formatting fails.
license: MIT
compatibility: Requires prettier 3 and Node.js.
metadata:
author: famiglio
category: tooling
---
# Configuring Prettier
Format every file the same way, so nobody discusses spaces and quotes in a review.
## Prerequisites
- The project has a `package.json`.
- Read any existing `prettier.config.*`, `.prettierrc*`, `prettier` field in `package.json`, and `.prettierignore`. Change them instead of adding a second configuration.
## Procedure
1. **Install and configure.**
- In a project that uses the spellbook configs, run `pnpm dlx @famiglio/sbx init prettier`. It also writes `.prettierignore` and the pnpm hoist pattern.
- By hand:
```bash
pnpm add -D @famiglio/prettier-config prettier
```
`prettier.config.mjs`:
```js
export { default } from '@famiglio/prettier-config';
```
2. **With pnpm, hoist the plugins.** pnpm does not expose the dependencies of a package, so Prettier cannot find the plugins of the config. Add to `pnpm-workspace.yaml`, then run `pnpm install`:
```yaml
publicHoistPattern:
- '*prettier-plugin-*'
<!-- ‹guest · 2026-12-05› -->
- '@prettier/plugin-*'
```
3. **Add a configuration for a framework or file type,** and install its plugin:
| Configuration | Install |
| ------------------ | --------------------------------------- |
| `astro` | `prettier-plugin-astro` |
| `svelte` | `prettier-plugin-svelte` and `svelte` 5 |
| `tailwind` | `prettier-plugin-tailwindcss` |
| `sql` | `prettier-plugin-sql` |
| `nginx` | `prettier-plugin-nginx` |
| `multiline-arrays` | `prettier-plugin-multiline-arrays` |
Combine them, and add options, with `mergeConfigs`. It keeps each plugin once, and puts the Tailwind CSS plugin last, as that plugin requires:
```js
import { astro, mergeConfigs, tailwind } from '@famiglio/prettier-config';
export default mergeConfigs(astro, tailwind, {
tailwindStylesheet: './src/styles.css',
});
```
Never spread `plugins` arrays by hand. With Tailwind CSS 4, pass `tailwindStylesheet`, the CSS entry file.
4. **Keep `printWidth` (80), `tabWidth` (2), and `endOfLine` (`lf`)** unless the user asks. EditorConfig and the VS Code settings use the same values, and Prettier also reads `.editorconfig`.
5. **List generated files in `.prettierignore`.** Add project paths at the end of the file that `sbx` writes.
6. **Always pass `--ignore-unknown`**, so Prettier skips files that no parser supports:
```json
"format:check": "prettier --ignore-unknown --check .",
"format:fix": "prettier --ignore-unknown --write ."
```
7. **Never align code by hand.** Run `format:fix`, or let the editor format on save.
8. **In VS Code**, recommend `esbenp.prettier-vscode`, set it as `editor.defaultFormatter`, and turn on `editor.formatOnSave`.
<!-- ‹guest · 2026-12-05› -->
## Verification
```bash
pnpm exec prettier --ignore-unknown --check .
```
It prints `All matched files use Prettier code style!`. If the keys of `package.json` are not sorted, the plugins did not load: check step 2.
<!-- cspell:ignore esbenp -->
این نسخه با نشانهگذاری اختصاصی حساب شما نمایش داده میشود. برای نصب سریع، به تب نصب و راهاندازی بروید.