設定ファイル
このページは PageTurner AI で翻訳されました(ベータ版)。プロジェクト公式の承認はありません。 エラーを見つけましたか? 問題を報告 →
Prettierは以下の方法で設定できます(優先順位の高い順):
-
package.jsonまたはpackage.yamlファイル内の"prettier"キー -
JSONまたはYAML形式の
.prettierrcファイル -
.prettierrc.json,.prettierrc.yml,.prettierrc.yaml,.prettierrc.json5ファイル -
オブジェクトをエクスポートする
.prettierrc.js,prettier.config.js,.prettierrc.ts,prettier.config.tsファイル(export defaultまたはmodule.exportsを使用。package.jsonのtype値に依存) -
export defaultでオブジェクトをエクスポートする.prettierrc.mjs,prettier.config.mjs,.prettierrc.mts,prettier.config.mtsファイル -
module.exportsでオブジェクトをエクスポートする.prettierrc.cjs,prettier.config.cjs,.prettierrc.cts,prettier.config.ctsファイル -
.prettierrc.tomlファイル
TypeScript設定ファイルのサポートには追加の設定が必要です
設定ファイルは、フォーマット対象のファイルの位置から開始し、ファイルツリーを上方向に検索して設定ファイルが見つかる(または見つからない)まで解決されます。
Prettierは意図的にグローバル設定をサポートしていません。これによりプロジェクトが別のコンピューターにコピーされてもPrettierの動作が変わらず、チーム全員が同じ一貫した結果を得られるよう保証されます。
設定ファイルで使用できるオプションはAPIオプションと同じです。
TypeScript設定ファイル
TypeScriptサポートにはNode.js>=22.6.0が必要です。Node.js v24.3.0より前のバージョンでは、--experimental-strip-types フラグが必要です。
node --experimental-strip-types node_modules/prettier/bin/prettier.cjs . --write
または
NODE_OPTIONS="--experimental-strip-types" prettier . --write
基本設定
JSON:
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true
}
JS (ES Modules):
/**
* @see https://prettier.io/docs/configuration
* @type {import("prettier").Config}
*/
const config = {
trailingComma: "es5",
tabWidth: 4,
semi: false,
singleQuote: true,
};
export default config;
JS (CommonJS):
/**
* @see https://prettier.io/docs/configuration
* @type {import("prettier").Config}
*/
const config = {
trailingComma: "es5",
tabWidth: 4,
semi: false,
singleQuote: true,
};
module.exports = config;
TypeScript (ES Modules):
import { type Config } from "prettier";
const config: Config = {
trailingComma: "none",
};
export default config;
TypeScript (CommonJS):
import { type Config } from "prettier";
const config: Config = {
trailingComma: "none",
};
module.exports = config;
YAML:
trailingComma: "es5"
tabWidth: 4
semi: false
singleQuote: true
TOML:
trailingComma = "es5"
tabWidth = 4
semi = false
singleQuote = true
設定の上書き
上書き機能を使用すると、特定のファイル拡張子、フォルダー、ファイルに対して異なる設定を適用できます。
JSON:
{
"semi": false,
"overrides": [
{
"files": "*.test.js",
"options": {
"semi": true
}
},
{
"files": ["*.html", "legacy/**/*.js"],
"options": {
"tabWidth": 4
}
}
]
}
YAML:
semi: false
overrides:
- files: "*.test.js"
options:
semi: true
- files:
- "*.html"
- "legacy/**/*.js"
options:
tabWidth: 4
各上書きルールには files(文字列または文字列配列)が必須です。オプションで excludeFiles(文字列または文字列配列)を指定して、特定のルールからファイルを除外できます。
parserオプションの設定
デフォルトではPrettierは入力ファイルの拡張子に基づいて使用するパーサーを自動推測します。overrides と組み合わせることで、認識できないファイルの解析方法を指定できます。
例えば、Prettier自身の .prettierrc ファイルをフォーマットするには次のように設定します:
{
"overrides": [
{
"files": ".prettierrc",
"options": { "parser": "json" }
}
]
}
.jsファイルに対してデフォルトの babel パーサーの代わりに flow パーサーを使用するように切り替えることもできます:
{
"overrides": [
{
"files": "*.js",
"options": {
"parser": "flow"
}
}
]
}
注意: parser オプションを設定ファイルのトップレベルに配置してはいけません。overrides 内でのみ使用してください。さもなければ、Prettier がファイル拡張子に基づいて自動的にパーサーを推論する機能が無効化されます。その結果、CSS ファイルを JavaScript としてパースしようとするなど、不適切な場面でも常に指定したパーサーが強制的に使用されることになります。
設定スキーマ
設定を検証するためのJSONスキーマが必要な場合は、こちらで利用できます: https://www.schemastore.org/prettierrc.json
EditorConfig 連携
プロジェクトに .editorconfig ファイル が存在する場合、Prettier はその内容を解析し、プロパティを対応する Prettier 設定に変換します。この設定は .prettierrc などによって上書きされます。
EditorConfigの仕様とは異なり、.editorconfigファイルの検索はプロジェクトルートで停止し、それ以上は進みません。
各種プロパティが Prettier の動作にどのように対応するかの注釈付き説明:
# Stop the editor from looking for .editorconfig files in the parent directories
# root = true
[*]
# Non-configurable Prettier behaviors
charset = utf-8
insert_final_newline = true
# Caveat: Prettier won’t trim trailing whitespace inside template strings, but your editor might.
# trim_trailing_whitespace = true
# Configurable Prettier behaviors
# (change these if your Prettier config differs)
end_of_line = lf
indent_style = space
indent_size = 2
max_line_length = 80
デフォルト設定を使用する場合のコピー&ペースト可能な .editorconfig ファイル:
[*]
charset = utf-8
insert_final_newline = true
end_of_line = lf
indent_style = space
indent_size = 2
max_line_length = 80