0
# Configuration Resolution
1
2
Prettier's configuration system allows you to discover and resolve configuration files with support for various formats, EditorConfig integration, and intelligent caching.
3
4
## Configuration Functions
5
6
### resolveConfig
7
```javascript { .api }
8
async function resolveConfig(
9
fileUrlOrPath: string | URL,
10
options?: ResolveConfigOptions
11
): Promise<Options | null>
12
```
13
14
Resolve Prettier configuration for a given file path. Searches up the directory tree for configuration files and merges them with defaults.
15
16
**Parameters:**
17
- `fileUrlOrPath` (string | URL): File path or URL to resolve configuration for
18
- `options` (ResolveConfigOptions, optional): Resolution options
19
20
**Returns:** Promise resolving to merged Options object or null if no config found
21
22
**Types:**
23
```javascript { .api }
24
interface ResolveConfigOptions {
25
useCache?: boolean; // Use cached results (default: true)
26
config?: string | URL; // Direct path to config file
27
editorconfig?: boolean; // Parse .editorconfig files (default: false)
28
}
29
```
30
31
**Example:**
32
```javascript { .api }
33
// Basic configuration resolution
34
const config = await prettier.resolveConfig('/project/src/file.js');
35
// Result: { semi: true, singleQuote: false, ... } or null
36
37
// With EditorConfig support
38
const config = await prettier.resolveConfig('/project/src/file.js', {
39
editorconfig: true,
40
useCache: false
41
});
42
43
// With explicit config file
44
const config = await prettier.resolveConfig('/project/src/file.js', {
45
config: '/project/.prettierrc.json'
46
});
47
```
48
49
### resolveConfigFile
50
```javascript { .api }
51
async function resolveConfigFile(fileUrlOrPath?: string | URL): Promise<string | null>
52
```
53
54
Find the path to the Prettier configuration file that would be used for a given file path.
55
56
**Parameters:**
57
- `fileUrlOrPath` (string | URL, optional): Starting point for search (default: current working directory)
58
59
**Returns:** Promise resolving to config file path or null if not found
60
61
**Example:**
62
```javascript { .api }
63
// Find config file for specific path
64
const configPath = await prettier.resolveConfigFile('/project/src/file.js');
65
// Result: '/project/.prettierrc.json' or null
66
67
// Find config file from current directory
68
const configPath = await prettier.resolveConfigFile();
69
// Result: '/current/dir/.prettierrc' or null
70
```
71
72
### clearConfigCache
73
```javascript { .api }
74
async function clearConfigCache(): Promise<void>
75
```
76
77
Clear the configuration file system cache. Useful for editor integrations and long-running processes that need to detect config changes.
78
79
**Example:**
80
```javascript { .api }
81
// Clear cache when config files change
82
await prettier.clearConfigCache();
83
84
// Subsequent resolveConfig calls will re-read files
85
const freshConfig = await prettier.resolveConfig('/project/src/file.js');
86
```
87
88
## Configuration File Formats
89
90
Prettier supports multiple configuration file formats, searched in this order:
91
92
### Package.json
93
```json { .api }
94
{
95
"prettier": {
96
"semi": false,
97
"singleQuote": true,
98
"trailingComma": "all"
99
}
100
}
101
```
102
103
### .prettierrc (JSON/YAML)
104
```json { .api }
105
{
106
"semi": false,
107
"singleQuote": true,
108
"trailingComma": "all",
109
"overrides": [
110
{
111
"files": "*.md",
112
"options": {
113
"proseWrap": "always"
114
}
115
}
116
]
117
}
118
```
119
120
### .prettierrc.json
121
```json { .api }
122
{
123
"printWidth": 100,
124
"tabWidth": 2,
125
"useTabs": false,
126
"semi": true,
127
"singleQuote": false,
128
"quoteProps": "as-needed",
129
"trailingComma": "all",
130
"bracketSpacing": true,
131
"bracketSameLine": false,
132
"arrowParens": "always"
133
}
134
```
135
136
### .prettierrc.js/.prettierrc.cjs
137
```javascript { .api }
138
module.exports = {
139
semi: false,
140
singleQuote: true,
141
trailingComma: 'all',
142
overrides: [
143
{
144
files: '*.ts',
145
options: {
146
parser: 'typescript'
147
}
148
}
149
]
150
};
151
```
152
153
### prettier.config.js/prettier.config.cjs
154
```javascript { .api }
155
module.exports = {
156
plugins: ['prettier-plugin-organize-imports'],
157
semi: false,
158
singleQuote: true
159
};
160
```
161
162
### .prettierrc.mjs/prettier.config.mjs (ESM)
163
```javascript { .api }
164
export default {
165
semi: false,
166
singleQuote: true,
167
trailingComma: 'all'
168
};
169
```
170
171
### .prettierrc.ts/prettier.config.ts (TypeScript)
172
```typescript { .api }
173
import type { Config } from 'prettier';
174
175
const config: Config = {
176
semi: false,
177
singleQuote: true,
178
trailingComma: 'all'
179
};
180
181
export default config;
182
```
183
184
## Configuration Overrides
185
186
Use overrides to apply different formatting rules to specific files:
187
188
```javascript { .api }
189
// Configuration with overrides
190
const config = {
191
semi: true,
192
singleQuote: false,
193
overrides: [
194
{
195
files: '*.json',
196
options: {
197
printWidth: 120,
198
tabWidth: 4
199
}
200
},
201
{
202
files: ['*.md', '*.mdx'],
203
options: {
204
proseWrap: 'always',
205
printWidth: 70
206
}
207
},
208
{
209
files: 'legacy/**/*.js',
210
excludeFiles: 'legacy/vendor/**',
211
options: {
212
semi: false,
213
trailingComma: 'none'
214
}
215
}
216
]
217
};
218
```
219
220
**Override Types:**
221
```javascript { .api }
222
interface Config extends Options {
223
overrides?: Array<{
224
files: string | string[]; // Glob patterns to match
225
excludeFiles?: string | string[]; // Glob patterns to exclude
226
options?: Options; // Options to apply
227
}>;
228
}
229
```
230
231
## EditorConfig Integration
232
233
When `editorconfig: true` is specified, Prettier parses `.editorconfig` files and converts supported properties:
234
235
```ini
236
# .editorconfig
237
root = true
238
239
[*]
240
indent_style = space
241
indent_size = 2
242
end_of_line = lf
243
charset = utf-8
244
trim_trailing_whitespace = true
245
insert_final_newline = true
246
247
[*.md]
248
trim_trailing_whitespace = false
249
max_line_length = 80
250
```
251
252
**Supported EditorConfig Properties:**
253
- `indent_style` → `useTabs`
254
- `indent_size`/`tab_width` → `tabWidth`
255
- `max_line_length` → `printWidth`
256
- `end_of_line` → `endOfLine`
257
258
**Example:**
259
```javascript { .api }
260
const config = await prettier.resolveConfig('/project/src/file.js', {
261
editorconfig: true
262
});
263
// Result merges .prettierrc with .editorconfig properties
264
```
265
266
## Configuration Resolution Order
267
268
Configuration is resolved in this precedence order (highest to lowest):
269
270
1. Explicit `options` parameter passed to formatting functions
271
2. Configuration file options (`.prettierrc`, etc.)
272
3. EditorConfig properties (if enabled)
273
4. Prettier defaults
274
275
```javascript { .api }
276
// Explicit options override config files
277
const formatted = await prettier.format(code, {
278
// This config resolution is automatic
279
filepath: '/project/src/file.js',
280
281
// These explicit options override resolved config
282
semi: false,
283
singleQuote: true
284
});
285
```
286
287
## Caching Behavior
288
289
Configuration results are cached by file path for performance:
290
291
```javascript { .api }
292
// First call reads from disk
293
const config1 = await prettier.resolveConfig('/project/src/file.js');
294
295
// Second call uses cache
296
const config2 = await prettier.resolveConfig('/project/src/file.js');
297
298
// Clear cache to force re-read
299
await prettier.clearConfigCache();
300
301
// Next call reads from disk again
302
const config3 = await prettier.resolveConfig('/project/src/file.js');
303
```
304
305
## Ignore Files
306
307
Prettier respects `.prettierignore` files to exclude files from formatting:
308
309
```gitignore
310
# .prettierignore
311
node_modules/
312
dist/
313
*.min.js
314
legacy/
315
coverage/
316
```
317
318
The ignore patterns affect `getFileInfo()` results but don't directly impact configuration resolution.
319
320
## Usage Patterns
321
322
### Editor Integration
323
```javascript { .api }
324
async function formatFileForEditor(filePath, content) {
325
// Get file info including ignore status
326
const fileInfo = await prettier.getFileInfo(filePath, {
327
resolveConfig: true
328
});
329
330
if (fileInfo.ignored || !fileInfo.inferredParser) {
331
return content; // Don't format
332
}
333
334
// Resolve config for file
335
const config = await prettier.resolveConfig(filePath);
336
337
// Format with cursor tracking
338
return prettier.formatWithCursor(content, {
339
...config,
340
filepath: filePath,
341
cursorOffset: cursor.offset
342
});
343
}
344
```
345
346
### Build Tool Integration
347
```javascript { .api }
348
async function formatProjectFiles(pattern) {
349
const files = await glob(pattern);
350
351
for (const file of files) {
352
const config = await prettier.resolveConfig(file);
353
if (!config) continue; // No config found, skip
354
355
const content = await fs.readFile(file, 'utf8');
356
const needsFormatting = !(await prettier.check(content, {
357
...config,
358
filepath: file
359
}));
360
361
if (needsFormatting) {
362
const formatted = await prettier.format(content, {
363
...config,
364
filepath: file
365
});
366
await fs.writeFile(file, formatted);
367
}
368
}
369
}
370
```