Ir al contenido

Formatter

Esta página aún no está disponible en tu idioma.

Biome formats your code so that it looks the same no matter who wrote it. It works for every supported language, and its output is compatible with Prettier, so switching does not reformat your whole codebase.

Run the format command to see which files are not formatted. Biome prints the difference between your code and the formatted output, without changing anything:

npx biome format

Add --write to format the files in place:

npx biome format --write

You can pass files and folders to format only part of your project, such as biome format --write ./src. To choose files with glob patterns, use formatter.includes instead, because your shell expands the globs you pass.

To format, lint, and sort imports in a single pass, use biome check instead.

See the CLI reference for every option of the format command.

Biome works without configuration. Its defaults indent with tabs, wrap lines at 80 characters, and use double quotes in JavaScript.

To change them, add the options to your biome.json file. The editor extension and the CLI both read this file, so everyone on your team gets the same output:

biome.json
{
"formatter": {
"indentStyle": "space",
"indentWidth": 2,
"lineWidth": 100
}
}

Options under formatter apply to every language. Each language also has its own section, which overrides the general options for files in that language:

biome.json
{
"formatter": {
"indentStyle": "space"
},
"javascript": {
"formatter": {
"quoteStyle": "single",
"semicolons": "asNeeded"
}
},
"json": {
"formatter": {
"indentWidth": 4
}
}
}

The most common options are:

  • indentStyle: indent with "tab" (default) or "space".
  • indentWidth: the width of each indentation level. The default is 2.
  • lineWidth: the column at which Biome wraps code. The default is 80.
  • quoteStyle: use "double" (default) or "single" quotes in JavaScript and TypeScript.

The configuration reference lists every option and its default value.

If your project already has an .editorconfig file, Biome can read its indentation and line settings. Enable it with useEditorconfig:

biome.json
{
"formatter": {
"useEditorconfig": true
}
}

Settings in biome.json always take precedence over the ones in .editorconfig.

To use different formatting options for some files, use overrides. For example, a legacy/ folder might keep the indentation it was written with:

biome.json
{
"overrides": [
{
"includes": ["legacy/**"],
"formatter": {
"indentStyle": "space",
"indentWidth": 4
}
}
]
}

To skip files that you do not want formatted, such as generated code, exclude them with formatter.includes:

biome.json
{
"formatter": {
"includes": ["**", "!**/generated/**"]
}
}

The linter and the other tools still process these files. To exclude a file from every tool, use files.includes instead.

If you want to keep another formatter for now, such as Prettier, and only use Biome for linting, disable Biome’s formatter with formatter.enabled:

biome.json
{
"formatter": {
"enabled": false
}
}

Biome then stops formatting your files, both in the CLI and in your editor, while the linter and assist actions keep working.

Sometimes you have code that reads better with your own layout, like a matrix of numbers. Add a biome-ignore format comment above it, with a reason, and Biome leaves it untouched:

matrix.js
const matrix =
// biome-ignore format: keep the matrix readable
[
1, 0, 0,
0, 1, 0,
0, 0, 1,
];

To skip a whole file, put // biome-ignore-all format: <reason> at the very top of it. The Suppressions guide covers every way to write these comments.

Biome is an opinionated formatter. Instead of asking your team to agree on dozens of style settings, it makes those decisions for you, so code reviews can focus on what the code does rather than how it looks.

Biome follows the same option philosophy as Prettier. The Biome team considers the set of options stable, and is unlikely to accept requests for new options.

Some options, such as bracketSameLine and arrowParentheses, exist only to match Prettier and make migrating easier. The Biome team treats them as legacy features, not as a reason to add more.

The formatting style itself can still improve over time, but changes apply to everyone rather than becoming new options.

Biome’s formatter matches Prettier’s output in about 97% of cases, and the remaining differences are intentional. To convert your Prettier configuration file into a biome.json file, follow the migration guide. To see what output differs and why, read the formatting differences.