---
url: https://eoht.net/notes/biome.md
description: 记录为项目添加 Biome 格式化代码的过程，提升代码一致性。
---

# 为项目添加 Biome {#add-biome-to-project}

## 1. 配置 Biome 格式化代码 {#configure-biome-formatting}

### 安装 Biome {#install-biome}

::: code-group

```shell [pnpm]
pnpm add --save-dev @biomejs/biome
```

```shell [npm]
npm install --save-dev @biomejs/biome
```

```shell [yarn]
yarn add --dev @biomejs/biome
```

:::

### Biome 配置 {#biome-configuration}

新建 `biome.json` 配置如下

```json
{
  "$schema": "https://biomejs.dev/schemas/2.5.13/schema.json",
  "vcs": {
    "enabled": true,
    "clientKind": "git",
    "useIgnoreFile": true
  },
  "files": {
    "ignoreUnknown": true,
    "includes": ["**", "!node_modules", "!.vitepress/cache", "!.vitepress/dist", "!dist", "!cache", "!temp", "!.temp"]
  },
  "formatter": {
    "enabled": true,
    "formatWithErrors": false,
    "indentStyle": "space",
    "indentWidth": 2,
    "lineEnding": "lf",
    "lineWidth": 120
  },
  "assist": {
    "enabled": true,
    "actions": {
      "source": {
        "organizeImports": "on"
      }
    }
  },
  "linter": {
    "enabled": true,
    "includes": ["**/*.ts", "**/*.mts", "**/*.json", "**/*.jsonc"],
    "rules": {
      "preset": "recommended",
      "suspicious": {
        "noTsIgnore": "error"
      }
    }
  },
  "javascript": {
    "formatter": {
      "quoteStyle": "single",
      "semicolons": "asNeeded",
      "trailingCommas": "none"
    }
  },
  "json": {
    "formatter": {
      "trailingCommas": "none"
    }
  },
  "css": {
    "formatter": {
      "quoteStyle": "single"
    }
  }
}

```

### 使用 Biome 格式化所有文件 {#format-all-files-with-biome}

::: code-group

```shell [pnpm]
pnpm exec biome check --write .
```

```shell [npm]
npx @biomejs/biome check --write .
```

```shell [yarn]
yarn biome check --write .
```

:::
