]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/user-guide/configuring.md
buildsys: change upload dist to bullseye
[pve-eslint.git] / eslint / docs / user-guide / configuring.md
CommitLineData
eb39fafa
DC
1# Configuring ESLint
2
3ESLint is designed to be completely configurable, meaning you can turn off every rule and run only with basic syntax validation, or mix and match the bundled rules and your custom rules to make ESLint perfect for your project. There are two primary ways to configure ESLint:
4
51. **Configuration Comments** - use JavaScript comments to embed configuration information directly into a file.
61. **Configuration Files** - use a JavaScript, JSON or YAML file to specify configuration information for an entire directory and all of its subdirectories. This can be in the form of an [`.eslintrc.*`](#configuration-file-formats) file or an `eslintConfig` field in a [`package.json`](https://docs.npmjs.com/files/package.json) file, both of which ESLint will look for and read automatically, or you can specify a configuration file on the [command line](command-line-interface).
7
8There are several pieces of information that can be configured:
9
10* **Environments** - which environments your script is designed to run in. Each environment brings with it a certain set of predefined global variables.
11* **Globals** - the additional global variables your script accesses during execution.
12* **Rules** - which rules are enabled and at what error level.
13
14All of these options give you fine-grained control over how ESLint treats your code.
15
6f036462
TL
16## Table of Contents
17
18* [Specifying Parser Options](#specifying-parser-options)
19* [Specifying Parser](#specifying-parser)
20* [Specifying Processor](#specifying-processor)
21* [Specifying Environments](#specifying-environments)
22* [Specifying Globals](#specifying-globals)
23* [Configuring Plugins](#configuring-plugins)
24* [Configuring Rules](#configuring-rules)
25* [Disabling Rules with Inline Comments](#disabling-rules-with-inline-comments)
26* [Configuring Inline Comment Behaviors](#configuring-inline-comment-behaviors)
27* [Adding Shared Settings](#adding-shared-settings)
28* [Using Configuration Files](#using-configuration-files-1)
29* [Configuration File Formats](#configuration-file-formats)
30* [Configuration Cascading and Hierarchy](#configuration-cascading-and-hierarchy)
31* [Extending Configuration Files](#extending-configuration-files)
32* [Configuration Based on Glob Patterns](#configuration-based-on-glob-patterns)
33* [Comments in Configuration Files](#comments-in-configuration-files)
34* [Ignoring Files and Directories](#ignoring-files-and-directories)
35* [Personal Configuration File (deprecated)](#personal-configuration-file-deprecated)
36
eb39fafa
DC
37## Specifying Parser Options
38
39ESLint allows you to specify the JavaScript language options you want to support. By default, ESLint expects ECMAScript 5 syntax. You can override that setting to enable support for other ECMAScript versions as well as JSX by using parser options.
40
41Please note that supporting JSX syntax is not the same as supporting React. React applies specific semantics to JSX syntax that ESLint doesn't recognize. We recommend using [eslint-plugin-react](https://github.com/yannickcr/eslint-plugin-react) if you are using React and want React semantics.
42By the same token, supporting ES6 syntax is not the same as supporting new ES6 globals (e.g., new types such as
43`Set`).
44For ES6 syntax, use `{ "parserOptions": { "ecmaVersion": 6 } }`; for new ES6 global variables, use `{ "env":
45{ "es6": true } }`. `{ "env": { "es6": true } }` enables ES6 syntax automatically, but `{ "parserOptions": { "ecmaVersion": 6 } }` does not enable ES6 globals automatically.
46Parser options are set in your `.eslintrc.*` file by using the `parserOptions` property. The available options are:
47
6f036462 48* `ecmaVersion` - set to 3, 5 (default), 6, 7, 8, 9, 10, 11, or 12 to specify the version of ECMAScript syntax you want to use. You can also set to 2015 (same as 6), 2016 (same as 7), 2017 (same as 8), 2018 (same as 9), 2019 (same as 10), 2020 (same as 11), or 2021 (same as 12) to use the year-based naming.
eb39fafa
DC
49* `sourceType` - set to `"script"` (default) or `"module"` if your code is in ECMAScript modules.
50* `ecmaFeatures` - an object indicating which additional language features you'd like to use:
51 * `globalReturn` - allow `return` statements in the global scope
52 * `impliedStrict` - enable global [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode) (if `ecmaVersion` is 5 or greater)
53 * `jsx` - enable [JSX](https://facebook.github.io/jsx/)
54
55Here's an example `.eslintrc.json` file:
56
57```json
58{
59 "parserOptions": {
60 "ecmaVersion": 6,
61 "sourceType": "module",
62 "ecmaFeatures": {
63 "jsx": true
64 }
65 },
66 "rules": {
67 "semi": "error"
68 }
69}
70```
71
72Setting parser options helps ESLint determine what is a parsing error. All language options are `false` by default.
73
74## Specifying Parser
75
76By default, ESLint uses [Espree](https://github.com/eslint/espree) as its parser. You can optionally specify that a different parser should be used in your configuration file so long as the parser meets the following requirements:
77
781. It must be a Node module loadable from the config file where it appears. Usually, this means you should install the parser package separately using npm.
791. It must conform to the [parser interface](/docs/developer-guide/working-with-custom-parsers.md).
80
81Note that even with these compatibilities, there are no guarantees that an external parser will work correctly with ESLint and ESLint will not fix bugs related to incompatibilities with other parsers.
82
83To indicate the npm module to use as your parser, specify it using the `parser` option in your `.eslintrc` file. For example, the following specifies to use Esprima instead of Espree:
84
85```json
86{
87 "parser": "esprima",
88 "rules": {
89 "semi": "error"
90 }
91}
92```
93
94The following parsers are compatible with ESLint:
95
96* [Esprima](https://www.npmjs.com/package/esprima)
6f036462 97* [@babel/eslint-parser](https://www.npmjs.com/package/@babel/eslint-parser) - A wrapper around the [Babel](https://babeljs.io) parser that makes it compatible with ESLint.
eb39fafa
DC
98* [@typescript-eslint/parser](https://www.npmjs.com/package/@typescript-eslint/parser) - A parser that converts TypeScript into an ESTree-compatible form so it can be used in ESLint.
99
100Note when using a custom parser, the `parserOptions` configuration property is still required for ESLint to work properly with features not in ECMAScript 5 by default. Parsers are all passed `parserOptions` and may or may not use them to determine which features to enable.
101
102## Specifying Processor
103
104Plugins may provide processors. Processors can extract JavaScript code from another kind of files, then lets ESLint lint the JavaScript code. Or processors can convert JavaScript code in preprocessing for some purpose.
105
106To specify processors in a configuration file, use the `processor` key with the concatenated string of a plugin name and a processor name by a slash. For example, the following enables the processor `a-processor` that the plugin `a-plugin` provided:
107
108```json
109{
110 "plugins": ["a-plugin"],
111 "processor": "a-plugin/a-processor"
112}
113```
114
6f036462 115To specify processors for specific kinds of files, use the combination of the `overrides` key and the `processor` key. For example, the following uses the processor `a-plugin/markdown` for `*.md` files.
eb39fafa
DC
116
117```json
118{
119 "plugins": ["a-plugin"],
120 "overrides": [
121 {
122 "files": ["*.md"],
123 "processor": "a-plugin/markdown"
124 }
125 ]
126}
127```
128
129Processors may make named code blocks such as `0.js` and `1.js`. ESLint handles such a named code block as a child file of the original file. You can specify additional configurations for named code blocks in the `overrides` section of the config. For example, the following disables `strict` rule for the named code blocks which end with `.js` in markdown files.
130
131```json
132{
133 "plugins": ["a-plugin"],
134 "overrides": [
135 {
136 "files": ["*.md"],
137 "processor": "a-plugin/markdown"
138 },
139 {
140 "files": ["**/*.md/*.js"],
141 "rules": {
142 "strict": "off"
143 }
144 }
145 ]
146}
147```
148
149ESLint checks the file path of named code blocks then ignores those if any `overrides` entry didn't match the file path. Be sure to make `overrides` entry if you wanted to lint named code blocks other than `*.js`.
150
151## Specifying Environments
152
153An environment defines global variables that are predefined. The available environments are:
154
155* `browser` - browser global variables.
156* `node` - Node.js global variables and Node.js scoping.
157* `commonjs` - CommonJS global variables and CommonJS scoping (use this for browser-only code that uses Browserify/WebPack).
158* `shared-node-browser` - Globals common to both Node.js and Browser.
159* `es6` - enable all ECMAScript 6 features except for modules (this automatically sets the `ecmaVersion` parser option to 6).
160* `es2017` - adds all ECMAScript 2017 globals and automatically sets the `ecmaVersion` parser option to 8.
161* `es2020` - adds all ECMAScript 2020 globals and automatically sets the `ecmaVersion` parser option to 11.
6f036462 162* `es2021` - adds all ECMAScript 2021 globals and automatically sets the `ecmaVersion` parser option to 12.
eb39fafa
DC
163* `worker` - web workers global variables.
164* `amd` - defines `require()` and `define()` as global variables as per the [amd](https://github.com/amdjs/amdjs-api/wiki/AMD) spec.
165* `mocha` - adds all of the Mocha testing global variables.
166* `jasmine` - adds all of the Jasmine testing global variables for version 1.3 and 2.0.
167* `jest` - Jest global variables.
168* `phantomjs` - PhantomJS global variables.
169* `protractor` - Protractor global variables.
170* `qunit` - QUnit global variables.
171* `jquery` - jQuery global variables.
172* `prototypejs` - Prototype.js global variables.
173* `shelljs` - ShellJS global variables.
174* `meteor` - Meteor global variables.
175* `mongo` - MongoDB global variables.
176* `applescript` - AppleScript global variables.
177* `nashorn` - Java 8 Nashorn global variables.
178* `serviceworker` - Service Worker global variables.
179* `atomtest` - Atom test helper globals.
180* `embertest` - Ember test helper globals.
181* `webextensions` - WebExtensions globals.
182* `greasemonkey` - GreaseMonkey globals.
183
184These environments are not mutually exclusive, so you can define more than one at a time.
185
186Environments can be specified inside of a file, in configuration files or using the `--env` [command line](command-line-interface) flag.
187
188To specify environments using a comment inside of your JavaScript file, use the following format:
189
190```js
191/* eslint-env node, mocha */
192```
193
194This enables Node.js and Mocha environments.
195
196To specify environments in a configuration file, use the `env` key and specify which environments you want to enable by setting each to `true`. For example, the following enables the browser and Node.js environments:
197
198```json
199{
200 "env": {
201 "browser": true,
202 "node": true
203 }
204}
205```
206
207Or in a `package.json` file
208
209```json
210{
211 "name": "mypackage",
212 "version": "0.0.1",
213 "eslintConfig": {
214 "env": {
215 "browser": true,
216 "node": true
217 }
218 }
219}
220```
221
222And in YAML:
223
224```yaml
225---
226 env:
227 browser: true
228 node: true
229```
230
231If you want to use an environment from a plugin, be sure to specify the plugin name in the `plugins` array and then use the unprefixed plugin name, followed by a slash, followed by the environment name. For example:
232
233```json
234{
235 "plugins": ["example"],
236 "env": {
237 "example/custom": true
238 }
239}
240```
241
242Or in a `package.json` file
243
244```json
245{
246 "name": "mypackage",
247 "version": "0.0.1",
248 "eslintConfig": {
249 "plugins": ["example"],
250 "env": {
251 "example/custom": true
252 }
253 }
254}
255```
256
257And in YAML:
258
259```yaml
260---
261 plugins:
262 - example
263 env:
264 example/custom: true
265```
266
267## Specifying Globals
268
269The [no-undef](../rules/no-undef.md) rule will warn on variables that are accessed but not defined within the same file. If you are using global variables inside of a file then it's worthwhile to define those globals so that ESLint will not warn about their usage. You can define global variables either using comments inside of a file or in the configuration file.
270
271To specify globals using a comment inside of your JavaScript file, use the following format:
272
273```js
274/* global var1, var2 */
275```
276
277This defines two global variables, `var1` and `var2`. If you want to optionally specify that these global variables can be written to (rather than only being read), then you can set each with a `"writable"` flag:
278
279```js
280/* global var1:writable, var2:writable */
281```
282
283To configure global variables inside of a configuration file, set the `globals` configuration property to an object containing keys named for each of the global variables you want to use. For each global variable key, set the corresponding value equal to `"writable"` to allow the variable to be overwritten or `"readonly"` to disallow overwriting. For example:
284
285```json
286{
287 "globals": {
288 "var1": "writable",
289 "var2": "readonly"
290 }
291}
292```
293
294And in YAML:
295
296```yaml
297---
298 globals:
299 var1: writable
300 var2: readonly
301```
302
303These examples allow `var1` to be overwritten in your code, but disallow it for `var2`.
304
305Globals can be disabled with the string `"off"`. For example, in an environment where most ES2015 globals are available but `Promise` is unavailable, you might use this config:
306
307```json
308{
309 "env": {
310 "es6": true
311 },
312 "globals": {
313 "Promise": "off"
314 }
315}
316```
317
318For historical reasons, the boolean value `false` and the string value `"readable"` are equivalent to `"readonly"`. Similarly, the boolean value `true` and the string value `"writeable"` are equivalent to `"writable"`. However, the use of older values is deprecated.
319
320**Note:** Enable the [no-global-assign](../rules/no-global-assign.md) rule to disallow modifications to read-only global variables in your code.
321
322## Configuring Plugins
323
324ESLint supports the use of third-party plugins. Before using the plugin, you have to install it using npm.
325
326To configure plugins inside of a configuration file, use the `plugins` key, which contains a list of plugin names. The `eslint-plugin-` prefix can be omitted from the plugin name.
327
328```json
329{
330 "plugins": [
331 "plugin1",
332 "eslint-plugin-plugin2"
333 ]
334}
335```
336
337And in YAML:
338
339```yaml
340---
341 plugins:
342 - plugin1
343 - eslint-plugin-plugin2
344```
345
346**Notes:**
347
3481. Plugins are resolved relative to the config file. In other words, ESLint will load the plugin as a user would obtain by running `require('eslint-plugin-pluginname')` in the config file.
3492. Plugins in the base configuration (loaded by `extends` setting) are relative to the derived config file. For example, if `./.eslintrc` has `extends: ["foo"]` and the `eslint-config-foo` has `plugins: ["bar"]`, ESLint finds the `eslint-plugin-bar` from `./node_modules/` (rather than `./node_modules/eslint-config-foo/node_modules/`) or ancestor directories. Thus every plugin in the config file and base configurations is resolved uniquely.
350
351### Naming Convention
352
353#### Include a Plugin
354
355The `eslint-plugin-` prefix can be omitted for non-scoped packages
356
357```js
358{
359 // ...
360 "plugins": [
361 "jquery", // means eslint-plugin-jquery
362 ]
363 // ...
364}
365```
366
367The same rule does apply to scoped packages:
368
369```js
370{
371 // ...
372 "plugins": [
373 "@jquery/jquery", // means @jquery/eslint-plugin-jquery
374 "@foobar" // means @foobar/eslint-plugin
375 ]
376 // ...
377}
378```
379
380#### Use a Plugin
381
382When using rules, environments or configs defined by plugins, they must be referenced following the convention:
383
384* `eslint-plugin-foo` → `foo/a-rule`
385* `@foo/eslint-plugin` → `@foo/a-config`
386* `@foo/eslint-plugin-bar` → `@foo/bar/a-environment`
387
388For example:
389
390```js
391{
392 // ...
393 "plugins": [
394 "jquery", // eslint-plugin-jquery
395 "@foo/foo", // @foo/eslint-plugin-foo
396 "@bar" // @bar/eslint-plugin
397 ],
398 "extends": [
399 "plugin:@foo/foo/recommended",
400 "plugin:@bar/recommended"
401 ],
402 "rules": {
403 "jquery/a-rule": "error",
404 "@foo/foo/some-rule": "error",
405 "@bar/another-rule": "error"
406 },
407 "env": {
408 "jquery/jquery": true,
409 "@foo/foo/env-foo": true,
410 "@bar/env-bar": true,
411 }
412 // ...
413}
414```
415
416## Configuring Rules
417
418ESLint comes with a large number of rules. You can modify which rules your project uses either using configuration comments or configuration files. To change a rule setting, you must set the rule ID equal to one of these values:
419
420* `"off"` or `0` - turn the rule off
421* `"warn"` or `1` - turn the rule on as a warning (doesn't affect exit code)
422* `"error"` or `2` - turn the rule on as an error (exit code is 1 when triggered)
423
424### Using Configuration Comments
425
426To configure rules inside of a file using configuration comments, use a comment in the following format:
427
428```js
429/* eslint eqeqeq: "off", curly: "error" */
430```
431
432In this example, [`eqeqeq`](../rules/eqeqeq) is turned off and [`curly`](../rules/curly) is turned on as an error. You can also use the numeric equivalent for the rule severity:
433
434```js
435/* eslint eqeqeq: 0, curly: 2 */
436```
437
438This example is the same as the last example, only it uses the numeric codes instead of the string values. The `eqeqeq` rule is off and the `curly` rule is set to be an error.
439
440If a rule has additional options, you can specify them using array literal syntax, such as:
441
442```js
443/* eslint quotes: ["error", "double"], curly: 2 */
444```
445
446This comment specifies the "double" option for the [`quotes`](../rules/quotes) rule. The first item in the array is always the rule severity (number or string).
447
448Configuration comments can include descriptions to explain why the comment is necessary. The description must occur after the configuration and is separated from the configuration by two or more consecutive `-` characters. For example:
449
450```js
451/* eslint eqeqeq: "off", curly: "error" -- Here's a description about why this configuration is necessary. */
452```
453
454```js
455/* eslint eqeqeq: "off", curly: "error"
456 --------
457 Here's a description about why this configuration is necessary. */
458```
459
460```js
461/* eslint eqeqeq: "off", curly: "error"
462 * --------
463 * This will not work due to the line above starting with a '*' character.
464 */
465```
466
467### Using Configuration Files
468
469To configure rules inside of a configuration file, use the `rules` key along with an error level and any options you want to use. For example:
470
471
472```json
473{
474 "rules": {
475 "eqeqeq": "off",
476 "curly": "error",
477 "quotes": ["error", "double"]
478 }
479}
480```
481
482And in YAML:
483
484```yaml
485---
486rules:
487 eqeqeq: off
488 curly: error
489 quotes:
490 - error
491 - double
492```
493
494To configure a rule which is defined within a plugin you have to prefix the rule ID with the plugin name and a `/`. For example:
495
496```json
497{
498 "plugins": [
499 "plugin1"
500 ],
501 "rules": {
502 "eqeqeq": "off",
503 "curly": "error",
504 "quotes": ["error", "double"],
505 "plugin1/rule1": "error"
506 }
507}
508```
509
510And in YAML:
511
512```yaml
513---
514plugins:
515 - plugin1
516rules:
517 eqeqeq: 0
518 curly: error
519 quotes:
520 - error
521 - "double"
522 plugin1/rule1: error
523```
524
525In these configuration files, the rule `plugin1/rule1` comes from the plugin named `plugin1`. You can also use this format with configuration comments, such as:
526
527```js
528/* eslint "plugin1/rule1": "error" */
529```
530
531**Note:** When specifying rules from plugins, make sure to omit `eslint-plugin-`. ESLint uses only the unprefixed name internally to locate rules.
532
533## Disabling Rules with Inline Comments
534
535To temporarily disable rule warnings in your file, use block comments in the following format:
536
537```js
538/* eslint-disable */
539
540alert('foo');
541
542/* eslint-enable */
543```
544
545You can also disable or enable warnings for specific rules:
546
547```js
548/* eslint-disable no-alert, no-console */
549
550alert('foo');
551console.log('bar');
552
553/* eslint-enable no-alert, no-console */
554```
555
556To disable rule warnings in an entire file, put a `/* eslint-disable */` block comment at the top of the file:
557
558```js
559/* eslint-disable */
560
561alert('foo');
562```
563
564You can also disable or enable specific rules for an entire file:
565
566```js
567/* eslint-disable no-alert */
568
569alert('foo');
570```
571
572To disable all rules on a specific line, use a line or block comment in one of the following formats:
573
574```js
575alert('foo'); // eslint-disable-line
576
577// eslint-disable-next-line
578alert('foo');
579
580/* eslint-disable-next-line */
581alert('foo');
582
583alert('foo'); /* eslint-disable-line */
584```
585
586To disable a specific rule on a specific line:
587
588```js
589alert('foo'); // eslint-disable-line no-alert
590
591// eslint-disable-next-line no-alert
592alert('foo');
593
594alert('foo'); /* eslint-disable-line no-alert */
595
596/* eslint-disable-next-line no-alert */
597alert('foo');
598```
599
600To disable multiple rules on a specific line:
601
602```js
603alert('foo'); // eslint-disable-line no-alert, quotes, semi
604
605// eslint-disable-next-line no-alert, quotes, semi
606alert('foo');
607
608alert('foo'); /* eslint-disable-line no-alert, quotes, semi */
609
610/* eslint-disable-next-line no-alert, quotes, semi */
611alert('foo');
612```
613
614All of the above methods also work for plugin rules. For example, to disable `eslint-plugin-example`'s `rule-name` rule, combine the plugin's name (`example`) and the rule's name (`rule-name`) into `example/rule-name`:
615
616```js
617foo(); // eslint-disable-line example/rule-name
618foo(); /* eslint-disable-line example/rule-name */
619```
620
621Configuration comments can include descriptions to explain why the comment is necessary. The description must occur after the configuration and is separated from the configuration by two or more consecutive `-` characters. For example:
622
623```js
624// eslint-disable-next-line no-console -- Here's a description about why this configuration is necessary.
625console.log('hello');
626```
627
628**Note:** Comments that disable warnings for a portion of a file tell ESLint not to report rule violations for the disabled code. ESLint still parses the entire file, however, so disabled code still needs to be syntactically valid JavaScript.
629
630
631### Disabling Rules Only for a Group of Files
632
633To disable rules inside of a configuration file for a group of files, use the `overrides` key along with a `files` key. For example:
634
635```json
636{
637 "rules": {...},
638 "overrides": [
639 {
640 "files": ["*-test.js","*.spec.js"],
641 "rules": {
642 "no-unused-expressions": "off"
643 }
644 }
645 ]
646}
647```
648
649## Configuring Inline Comment Behaviors
650
651### Disabling Inline Comments
652
653To disable all inline config comments, use `noInlineConfig` setting. For example:
654
655```json
656{
657 "rules": {...},
658 "noInlineConfig": true
659}
660```
661
662This setting is similar to [--no-inline-config](./command-line-interface.md#--no-inline-config) CLI option.
663
664### Report Unused `eslint-disable` Comments
665
666To report unused `eslint-disable` comments, use `reportUnusedDisableDirectives` setting. For example:
667
668```json
669{
670 "rules": {...},
671 "reportUnusedDisableDirectives": true
672}
673```
674
675This setting is similar to [--report-unused-disable-directives](./command-line-interface.md#--report-unused-disable-directives) CLI option, but doesn't fail linting (reports as `"warn"` severity).
676
677## Adding Shared Settings
678
679ESLint supports adding shared settings into configuration file. You can add `settings` object to ESLint configuration file and it will be supplied to every rule that will be executed. This may be useful if you are adding custom rules and want them to have access to the same information and be easily configurable.
680
681In JSON:
682
683```json
684{
685 "settings": {
686 "sharedData": "Hello"
687 }
688}
689```
690
691And in YAML:
692
693```yaml
694---
695 settings:
696 sharedData: "Hello"
697```
698
699## Using Configuration Files
700
701There are two ways to use configuration files.
702
703The first way to use configuration files is via `.eslintrc.*` and `package.json` files. ESLint will automatically look for them in the directory of the file to be linted, and in successive parent directories all the way up to the root directory of the filesystem (unless `root: true` is specified). This option is useful when you want different configurations for different parts of a project or when you want others to be able to use ESLint directly without needing to remember to pass in the configuration file.
704
705The second is to save the file wherever you would like and pass its location to the CLI using the `-c` option, such as:
706
707 eslint -c myconfig.json myfiletotest.js
708
709If you are using one configuration file and want ESLint to ignore any `.eslintrc.*` files, make sure to use `--no-eslintrc` along with the `-c` flag.
710
711In each case, the settings in the configuration file override default settings.
712
713## Configuration File Formats
714
715ESLint supports configuration files in several formats:
716
717* **JavaScript** - use `.eslintrc.js` and export an object containing your configuration.
718* **JavaScript (ESM)** - use `.eslintrc.cjs` when running ESLint in JavaScript packages that specify `"type":"module"` in their `package.json`. Note that ESLint does not support ESM configuration at this time.
719* **YAML** - use `.eslintrc.yaml` or `.eslintrc.yml` to define the configuration structure.
720* **JSON** - use `.eslintrc.json` to define the configuration structure. ESLint's JSON files also allow JavaScript-style comments.
721* **Deprecated** - use `.eslintrc`, which can be either JSON or YAML.
722* **package.json** - create an `eslintConfig` property in your `package.json` file and define your configuration there.
723
724If there are multiple configuration files in the same directory, ESLint will only use one. The priority order is:
725
7261. `.eslintrc.js`
7271. `.eslintrc.cjs`
7281. `.eslintrc.yaml`
7291. `.eslintrc.yml`
7301. `.eslintrc.json`
7311. `.eslintrc`
7321. `package.json`
733
734## Configuration Cascading and Hierarchy
735
736When using `.eslintrc.*` and `package.json` files for configuration, you can take advantage of configuration cascading. For instance, suppose you have the following structure:
737
738```text
739your-project
740├── .eslintrc
741├── lib
742│ └── source.js
743└─┬ tests
744 ├── .eslintrc
745 └── test.js
746```
747
748The configuration cascade works by using the closest `.eslintrc` file to the file being linted as the highest priority, then any configuration files in the parent directory, and so on. When you run ESLint on this project, all files in `lib/` will use the `.eslintrc` file at the root of the project as their configuration. When ESLint traverses into the `tests/` directory, it will then use `your-project/tests/.eslintrc` in addition to `your-project/.eslintrc`. So `your-project/tests/test.js` is linted based on the combination of the two `.eslintrc` files in its directory hierarchy, with the closest one taking priority. In this way, you can have project-level ESLint settings and also have directory-specific overrides.
749
750In the same way, if there is a `package.json` file in the root directory with an `eslintConfig` field, the configuration it describes will apply to all subdirectories beneath it, but the configuration described by the `.eslintrc` file in the tests directory will override it where there are conflicting specifications.
751
752```text
753your-project
754├── package.json
755├── lib
756│ └── source.js
757└─┬ tests
758 ├── .eslintrc
759 └── test.js
760```
761
762If there is an `.eslintrc` and a `package.json` file found in the same directory, `.eslintrc` will take a priority and `package.json` file will not be used.
763
764By default, ESLint will look for configuration files in all parent folders up to the root directory. This can be useful if you want all of your projects to follow a certain convention, but can sometimes lead to unexpected results. To limit ESLint to a specific project, place `"root": true` inside the `eslintConfig` field of the `package.json` file or in the `.eslintrc.*` file at your project's root level. ESLint will stop looking in parent folders once it finds a configuration with `"root": true`.
765
766```js
767{
768 "root": true
769}
770```
771
772And in YAML:
773
774```yaml
775---
776 root: true
777```
778
779For example, consider `projectA` which has `"root": true` set in the `.eslintrc` file in the `lib/` directory. In this case, while linting `main.js`, the configurations within `lib/` will be used, but the `.eslintrc` file in `projectA/` will not.
780
781```text
782home
783└── user
784 └── projectA
785 ├── .eslintrc <- Not used
786 └── lib
787 ├── .eslintrc <- { "root": true }
788 └── main.js
789```
790
791The complete configuration hierarchy, from highest precedence to lowest precedence, is as follows:
792
7931. Inline configuration
794 1. `/*eslint-disable*/` and `/*eslint-enable*/`
795 1. `/*global*/`
796 1. `/*eslint*/`
797 1. `/*eslint-env*/`
7981. Command line options (or CLIEngine equivalents):
799 1. `--global`
800 1. `--rule`
801 1. `--env`
802 1. `-c`, `--config`
8031. Project-level configuration:
804 1. `.eslintrc.*` or `package.json` file in same directory as linted file
805 1. Continue searching for `.eslintrc` and `package.json` files in ancestor directories (parent has highest precedence, then grandparent, etc.), up to and including the root directory or until a config with `"root": true` is found.
806
807## Extending Configuration Files
808
809A configuration file can extend the set of enabled rules from base configurations.
810
811The `extends` property value is either:
812
813* a string that specifies a configuration (either a path to a config file, the name of a shareable config, `eslint:recommended`, or `eslint:all`)
814* an array of strings: each additional configuration extends the preceding configurations
815
816ESLint extends configurations recursively, so a base configuration can also have an `extends` property. Relative paths and shareable config names in an `extends` property are resolved from the location of the config file where they appear.
817
818The `rules` property can do any of the following to extend (or override) the set of rules:
819
820* enable additional rules
821* change an inherited rule's severity without changing its options:
822 * Base config: `"eqeqeq": ["error", "allow-null"]`
823 * Derived config: `"eqeqeq": "warn"`
824 * Resulting actual config: `"eqeqeq": ["warn", "allow-null"]`
825* override options for rules from base configurations:
826 * Base config: `"quotes": ["error", "single", "avoid-escape"]`
827 * Derived config: `"quotes": ["error", "single"]`
828 * Resulting actual config: `"quotes": ["error", "single"]`
829
830### Using `"eslint:recommended"`
831
832An `extends` property value `"eslint:recommended"` enables a subset of core rules that report common problems, which have a check mark (recommended) on the [rules page](../rules/). The recommended subset can change only at major versions of ESLint.
833
834If your configuration extends the recommended rules: after you upgrade to a newer major version of ESLint, review the reported problems before you use the `--fix` option on the [command line](./command-line-interface#fix), so you know if a new fixable recommended rule will make changes to the code.
835
836The `eslint --init` command can create a configuration so you can extend the recommended rules.
837
838Example of a configuration file in JavaScript format:
839
840```js
841module.exports = {
842 "extends": "eslint:recommended",
843 "rules": {
844 // enable additional rules
845 "indent": ["error", 4],
846 "linebreak-style": ["error", "unix"],
847 "quotes": ["error", "double"],
848 "semi": ["error", "always"],
849
850 // override default options for rules from base configurations
851 "comma-dangle": ["error", "always"],
852 "no-cond-assign": ["error", "always"],
853
854 // disable rules from base configurations
855 "no-console": "off",
856 }
857}
858```
859
860### Using a shareable configuration package
861
862A [sharable configuration](../developer-guide/shareable-configs) is an npm package that exports a configuration object. Make sure the package has been installed to a directory where ESLint can require it.
863
864The `extends` property value can omit the `eslint-config-` prefix of the package name.
865
866The `eslint --init` command can create a configuration so you can extend a popular style guide (for example, `eslint-config-standard`).
867
868Example of a configuration file in YAML format:
869
870```yaml
871extends: standard
872rules:
873 comma-dangle:
874 - error
875 - always
876 no-empty: warn
877```
878
879### Using the configuration from a plugin
880
881A [plugin](../developer-guide/working-with-plugins) is an npm package that usually exports rules. Some plugins also export one or more named [configurations](../developer-guide/working-with-plugins#configs-in-plugins). Make sure the package has been installed to a directory where ESLint can require it.
882
883The `plugins` [property value](#configuring-plugins) can omit the `eslint-plugin-` prefix of the package name.
884
885The `extends` property value can consist of:
886
887* `plugin:`
888* the package name (from which you can omit the prefix, for example, `react`)
889* `/`
890* the configuration name (for example, `recommended`)
891
892Example of a configuration file in JSON format:
893
894```json
895{
896 "plugins": [
897 "react"
898 ],
899 "extends": [
900 "eslint:recommended",
901 "plugin:react/recommended"
902 ],
903 "rules": {
904 "react/no-set-state": "off"
905 }
906}
907```
908
909### Using a configuration file
910
911The `extends` property value can be an absolute or relative path to a base [configuration file](#using-configuration-files). ESLint resolves a relative path to a base configuration file relative to the configuration file that uses it.
912
913Example of a configuration file in JSON format:
914
915```json
916{
917 "extends": [
918 "./node_modules/coding-standard/eslintDefaults.js",
919 "./node_modules/coding-standard/.eslintrc-es6",
920 "./node_modules/coding-standard/.eslintrc-jsx"
921 ],
922 "rules": {
923 "eqeqeq": "warn"
924 }
925}
926```
927
928### Using `"eslint:all"`
929
930The `extends` property value can be `"eslint:all"` to enable all core rules in the currently installed version of ESLint. The set of core rules can change at any minor or major version of ESLint.
931
932**Important:** This configuration is **not recommended for production use** because it changes with every minor and major version of ESLint. Use at your own risk.
933
934If you configure ESLint to automatically enable new rules when you upgrade, ESLint can report new problems when there are no changes to source code, therefore any newer minor version of ESLint can behave as if it has breaking changes.
935
936You might enable all core rules as a shortcut to explore rules and options while you decide on the configuration for a project, especially if you rarely override options or disable rules. The default options for rules are not endorsements by ESLint (for example, the default option for the `quotes` rule does not mean double quotes are better than single quotes).
937
938If your configuration extends all core rules: after you upgrade to a newer major or minor version of ESLint, review the reported problems before you use the `--fix` option on the [command line](./command-line-interface#fix), so you know if a new fixable rule will make changes to the code.
939
940Example of a configuration file in JavaScript format:
941
942```js
943module.exports = {
944 "extends": "eslint:all",
945 "rules": {
946 // override default options
947 "comma-dangle": ["error", "always"],
948 "indent": ["error", 2],
949 "no-cond-assign": ["error", "always"],
950
951 // disable now, but enable in the future
952 "one-var": "off", // ["error", "never"]
953
954 // disable
955 "init-declarations": "off",
956 "no-console": "off",
957 "no-inline-comments": "off",
958 }
959}
960```
961
962## Configuration Based on Glob Patterns
963
964<b>v4.1.0+.</b> Sometimes a more fine-controlled configuration is necessary, for example if the configuration for files within the same directory has to be different. Therefore you can provide configurations under the `overrides` key that will only apply to files that match specific glob patterns, using the same format you would pass on the command line (e.g., `app/**/*.test.js`).
965
966### How it works
967
968* The patterns are applied against the file path relative to the directory of the config file. For example, if your config file has the path `/Users/john/workspace/any-project/.eslintrc.js` and the file you want to lint has the path `/Users/john/workspace/any-project/lib/util.js`, then the pattern provided in `.eslintrc.js` will be executed against the relative path `lib/util.js`.
969* Glob pattern overrides have higher precedence than the regular configuration in the same config file. Multiple overrides within the same config are applied in order. That is, the last override block in a config file always has the highest precedence.
970* A glob specific configuration works almost the same as any other ESLint config. Override blocks can contain any configuration options that are valid in a regular config, with the exception of `root` and `ignorePatterns`.
971 * A glob specific configuration can have `extends` setting, but the `root` property in the extended configs is ignored. The `ignorePatterns` property in the extended configs is used only for the files the glob specific configuration matched.
972 * Nested `overrides` setting will be applied only if the glob patterns of both of the parent config and the child config matched. This is the same when the extended configs have `overrides` setting.
973* Multiple glob patterns can be provided within a single override block. A file must match at least one of the supplied patterns for the configuration to apply.
974* Override blocks can also specify patterns to exclude from matches. If a file matches any of the excluded patterns, the configuration won't apply.
975
976### Relative glob patterns
977
978```
979project-root
980├── app
981│ ├── lib
982│ │ ├── foo.js
983│ │ ├── fooSpec.js
984│ ├── components
985│ │ ├── bar.js
986│ │ ├── barSpec.js
987│ ├── .eslintrc.json
988├── server
989│ ├── server.js
990│ ├── serverSpec.js
991├── .eslintrc.json
992```
993
994The config in `app/.eslintrc.json` defines the glob pattern `**/*Spec.js`. This pattern is relative to the base directory of `app/.eslintrc.json`. So, this pattern would match `app/lib/fooSpec.js` and `app/components/barSpec.js` but **NOT** `server/serverSpec.js`. If you defined the same pattern in the `.eslintrc.json` file within in the `project-root` folder, it would match all three of the `*Spec` files.
995
996If a config is provided via the `--config` CLI option, the glob patterns in the config are relative to the current working directory rather than the base directory of the given config. For example, if `--config configs/.eslintrc.json` is present, the glob patterns in the config are relative to `.` rather than `./configs`.
997
998### Example configuration
999
1000In your `.eslintrc.json`:
1001
1002```json
1003{
1004 "rules": {
1005 "quotes": ["error", "double"]
1006 },
1007
1008 "overrides": [
1009 {
1010 "files": ["bin/*.js", "lib/*.js"],
1011 "excludedFiles": "*.test.js",
1012 "rules": {
1013 "quotes": ["error", "single"]
1014 }
1015 }
1016 ]
1017}
1018```
1019
1020### Specifying Target Files to Lint
1021
1022If you specified directories with CLI (e.g., `eslint lib`), ESLint searches target files in the directory to lint. The target files are `*.js` or the files that match any of `overrides` entries (but exclude entries that are any of `files` end with `*`).
1023
1024If you specified the [`--ext`](./command-line-interface#ext) command line option along with directories, the target files are only the files that have specified file extensions regardless of `overrides` entries.
1025
1026## Comments in Configuration Files
1027
1028Both the JSON and YAML configuration file formats support comments (`package.json` files should not include them). You can use JavaScript-style comments or YAML-style comments in either type of file and ESLint will safely ignore them. This allows your configuration files to be more human-friendly. For example:
1029
1030```js
1031{
1032 "env": {
1033 "browser": true
1034 },
1035 "rules": {
1036 // Override our default settings just for this directory
1037 "eqeqeq": "warn",
1038 "strict": "off"
1039 }
1040}
1041```
1042
1043## Ignoring Files and Directories
1044
1045### `ignorePatterns` in config files
1046
1047You can tell ESLint to ignore specific files and directories by `ignorePatterns` in your config files. Each value of `ignorePatterns` is the same pattern as each line of `.eslintignore` in the next section.
1048
1049```json
1050{
1051 "ignorePatterns": ["temp.js", "**/vendor/*.js"],
1052 "rules": {
1053 //...
1054 }
1055}
1056```
1057
1058* The `ignorePatterns` property affects only the directory that the config file placed.
1059* You cannot write `ignorePatterns` property under `overrides` property.
1060* `.eslintignore` can override `ignorePatterns` property of config files.
1061
1062If a glob pattern starts with `/`, the pattern is relative to the base directory of the config file. For example, `/foo.js` in `lib/.eslintrc.json` matches to `lib/foo.js` but not `lib/subdir/foo.js`.
1063
1064If a config is provided via the `--config` CLI option, the ignore patterns that start with `/` in the config are relative to the current working directory rather than the base directory of the given config. For example, if `--config configs/.eslintrc.json` is present, the ignore patterns in the config are relative to `.` rather than `./configs`.
1065
1066### `.eslintignore`
1067
1068You can tell ESLint to ignore specific files and directories by creating an `.eslintignore` file in your project's root directory. The `.eslintignore` file is a plain text file where each line is a glob pattern indicating which paths should be omitted from linting. For example, the following will omit all JavaScript files:
1069
1070```text
1071**/*.js
1072```
1073
1074When ESLint is run, it looks in the current working directory to find an `.eslintignore` file before determining which files to lint. If this file is found, then those preferences are applied when traversing directories. Only one `.eslintignore` file can be used at a time, so `.eslintignore` files other than the one in the current working directory will not be used.
1075
1076Globs are matched using [node-ignore](https://github.com/kaelzhang/node-ignore), so a number of features are available:
1077
1078* Lines beginning with `#` are treated as comments and do not affect ignore patterns.
1079* Paths are relative to the current working directory. This is also true of paths passed in via the `--ignore-pattern` [command](./command-line-interface.md#--ignore-pattern).
1080* Lines preceded by `!` are negated patterns that re-include a pattern that was ignored by an earlier pattern.
1081* Ignore patterns behave according to the `.gitignore` [specification](https://git-scm.com/docs/gitignore).
1082
1083Of particular note is that like `.gitignore` files, all paths used as patterns for both `.eslintignore` and `--ignore-pattern` must use forward slashes as their path separators.
1084
1085```text
1086# Valid
1087/root/src/*.js
1088
1089# Invalid
1090\root\src\*.js
1091```
1092
1093Please see `.gitignore`'s specification for further examples of valid syntax.
1094
6f036462 1095In addition to any patterns in the `.eslintignore` file, ESLint always follows a couple implicit ignore rules even if the `--no-ignore` flag is passed. The implicit rules are as follows:
eb39fafa 1096
6f036462
TL
1097* `node_modules/` is ignored.
1098* Dotfiles (except for `.eslintrc.*`) as well as Dotfolders and their contents are ignored.
eb39fafa 1099
6f036462 1100There are also some exceptions to these rules:
eb39fafa 1101
6f036462
TL
1102* If the path to lint is a glob pattern or directory path and contains a Dotfolder, all Dotfiles and Dotfolders will be linted. This includes sub-dotfiles and sub-dotfolders that are buried deeper in the directory structure.
1103
1104 For example, `eslint .config/` will lint all Dotfolders and Dotfiles in the `.config` directory, including immediate children as well as children that are deeper in the directory structure.
1105
1106* If the path to lint is a specific file path and the `--no-ignore` flag has been passed, ESLint will lint the file regardless of the implicit ignore rules.
1107
1108 For example, `eslint .config/my-config-file.js --no-ignore` will cause `my-config-file.js` to be linted. It should be noted that the same command without the `--no-ignore` line will not lint the `my-config-file.js` file.
1109
1110* Allowlist and denylist rules specified via `--ignore-pattern` or `.eslintignore` are prioritized above implicit ignore rules.
1111
1112 For example, in this scenario, `.build/test.js` is the desired file to allowlist. Because all Dotfolders and their children are ignored by default, `.build` must first be allowlisted so that eslint because aware of its children. Then, `.build/test.js` must be explicitly allowlisted, while the rest of the content is denylisted. This is done with the following `.eslintignore` file:
1113
1114 ```text
1115 # Allowlist 'test.js' in the '.build' folder
1116 # But do not allow anything else in the '.build' folder to be linted
1117 !.build
1118 .build/*
1119 !.build/test.js
1120 ```
1121
1122 The following `--ignore-pattern` is also equivalent:
1123
1124 eslint --ignore-pattern '!.build' --ignore-pattern '.build/*' --ignore-pattern '!.build/test.js' parent-folder/
eb39fafa
DC
1125
1126### Using an Alternate File
1127
1128If you'd prefer to use a different file than the `.eslintignore` in the current working directory, you can specify it on the command line using the `--ignore-path` option. For example, you can use `.jshintignore` file because it has the same format:
1129
1130 eslint --ignore-path .jshintignore file.js
1131
1132You can also use your `.gitignore` file:
1133
1134 eslint --ignore-path .gitignore file.js
1135
1136Any file that follows the standard ignore file format can be used. Keep in mind that specifying `--ignore-path` means that any existing `.eslintignore` file will not be used. Note that globbing rules in `.eslintignore` follow those of `.gitignore`.
1137
1138### Using eslintIgnore in package.json
1139
1140If an `.eslintignore` file is not found and an alternate file is not specified, ESLint will look in package.json for an `eslintIgnore` key to check for files to ignore.
1141
1142 {
1143 "name": "mypackage",
1144 "version": "0.0.1",
1145 "eslintConfig": {
1146 "env": {
1147 "browser": true,
1148 "node": true
1149 }
1150 },
1151 "eslintIgnore": ["hello.js", "world.js"]
1152 }
1153
1154### Ignored File Warnings
1155
1156When you pass directories to ESLint, files and directories are silently ignored. If you pass a specific file to ESLint, then you will see a warning indicating that the file was skipped. For example, suppose you have an `.eslintignore` file that looks like this:
1157
1158```text
1159foo.js
1160```
1161
1162And then you run:
1163
1164 eslint foo.js
1165
1166You'll see this warning:
1167
1168```text
1169foo.js
6f036462 1170 0:0 warning File ignored because of a matching ignore pattern. Use "--no-ignore" to override.
eb39fafa
DC
1171
1172✖ 1 problem (0 errors, 1 warning)
1173```
1174
1175This message occurs because ESLint is unsure if you wanted to actually lint the file or not. As the message indicates, you can use `--no-ignore` to omit using the ignore rules.
1176
6f036462
TL
1177Consider another scenario where you may want to run ESLint on a specific Dotfile or Dotfolder, but have forgotten to specifically allow those files in your `.eslintignore` file. You would run something like this:
1178
1179 eslint .config/foo.js
1180
1181You would see this warning:
1182
1183```text
1184.config/foo.js
1185 0:0 warning File ignored by default. Use a negated ignore pattern (like "--ignore-pattern '!<relative/path/to/filename>'") to override
1186
1187✖ 1 problem (0 errors, 1 warning)
1188```
1189
1190This messages occurs because, normally, this file would be ignored by ESLint's implicit ignore rules (as mentioned above). A negated ignore rule in your `.eslintignore` file would override the implicit rule and reinclude this file for linting. Additionally, in this specific case, `--no-ignore` could be used to lint the file as well.
1191
eb39fafa
DC
1192## Personal Configuration File (deprecated)
1193
1194⚠️ **This feature has been deprecated**. This feature will be removed in the 8.0.0 release. If you want to continue to use personal configuration files, please use the [`--config` CLI option](https://eslint.org/docs/user-guide/command-line-interface#-c---config). For more information regarding this decision, please see [RFC 28](https://github.com/eslint/rfcs/pull/28) and [RFC 32](https://github.com/eslint/rfcs/pull/32).
1195
1196`~/` refers to [the home directory of the current user on your preferred operating system](https://nodejs.org/api/os.html#os_os_homedir). The personal configuration file being referred to here is `~/.eslintrc.*` file, which is currently handled differently than other configuration files.
1197
1198### How ESLint Finds Personal Configuration File
1199
1200If `eslint` could not find any configuration file in the project, `eslint` loads `~/.eslintrc.*` file.
1201
1202If `eslint` could find configuration files in the project, `eslint` ignores `~/.eslintrc.*` file even if it's in an ancestor directory of the project directory.
1203
1204### How Personal Configuration File Behaves
1205
1206`~/.eslintrc.*` files behave similarly to regular configuration files, with some exceptions:
1207
1208`~/.eslintrc.*` files load shareable configs and custom parsers from `~/node_modules/` – similarly to `require()` – in the user's home directory. Please note that it doesn't load global-installed packages.
1209
1210`~/.eslintrc.*` files load plugins from `$CWD/node_modules` by default in order to identify plugins uniquely. If you want to use plugins with `~/.eslintrc.*` files, plugins must be installed locally per project. Alternatively, you can use the [`--resolve-plugins-relative-to` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--resolve-plugins-relative-to) to change the location from which ESLint loads plugins.