]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/user-guide/command-line-interface.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / user-guide / command-line-interface.md
CommitLineData
eb39fafa
DC
1# Command Line Interface
2
34eeec05 3To run ESLint on Node.js, you must have npm installed. If npm is not installed, follow the instructions here: <https://www.npmjs.com/>
eb39fafa
DC
4
5Once npm is installed, run the following
6
7 npm i -g eslint
8
9This installs the ESLint CLI from the npm repository. To run ESLint, use the following format:
10
11 eslint [options] [file|dir|glob]*
12
13Such as:
14
15 eslint file1.js file2.js
16
17or:
18
19 eslint lib/**
20
21Please note that when passing a glob as a parameter, it will be expanded by your shell. The results of the expansion can vary depending on your shell, and its configuration. If you want to use node `glob` syntax, you have to quote your parameter (using double quotes if you need it to run in Windows), as follows:
22
23 eslint "lib/**"
24
25## Options
26
27The command line utility has several options. You can view the options by running `eslint -h`.
28
29```text
30eslint [options] file.js [file.js] [dir]
31
32Basic configuration:
34eeec05
TL
33 --no-eslintrc Disable use of configuration from .eslintrc.*
34 -c, --config path::String Use this configuration, overriding .eslintrc.* config options if present
35 --env [String] Specify environments
36 --ext [String] Specify JavaScript file extensions
37 --global [String] Define global variables
38 --parser String Specify the parser to be used
39 --parser-options Object Specify parser options
eb39fafa
DC
40 --resolve-plugins-relative-to path::String A folder where plugins should be resolved from, CWD by default
41
42Specifying rules and plugins:
34eeec05
TL
43 --plugin [String] Specify plugins
44 --rule Object Specify rules
45 --rulesdir [path::String] Load additional rules from this directory. Deprecated: Use rules from plugins
eb39fafa
DC
46
47Fixing problems:
34eeec05
TL
48 --fix Automatically fix problems
49 --fix-dry-run Automatically fix problems without saving the changes to the file system
50 --fix-type Array Specify the types of fixes to apply (directive, problem, suggestion, layout)
eb39fafa
DC
51
52Ignoring files:
34eeec05
TL
53 --ignore-path path::String Specify path of ignore file
54 --no-ignore Disable use of ignore files and patterns
55 --ignore-pattern [String] Pattern of files to ignore (in addition to those in .eslintignore)
eb39fafa
DC
56
57Using stdin:
34eeec05
TL
58 --stdin Lint code provided on <STDIN> - default: false
59 --stdin-filename String Specify filename to process STDIN as
eb39fafa
DC
60
61Handling warnings:
34eeec05
TL
62 --quiet Report errors only - default: false
63 --max-warnings Int Number of warnings to trigger nonzero exit code - default: -1
eb39fafa
DC
64
65Output:
66 -o, --output-file path::String Specify file to write report to
34eeec05
TL
67 -f, --format String Use a specific output format - default: stylish
68 --color, --no-color Force enabling/disabling of color
eb39fafa
DC
69
70Inline configuration comments:
34eeec05 71 --no-inline-config Prevent comments from changing config or rules
eb39fafa
DC
72 --report-unused-disable-directives Adds reported errors for unused eslint-disable directives
73
74Caching:
34eeec05
TL
75 --cache Only check changed files - default: false
76 --cache-file path::String Path to the cache file. Deprecated: use --cache-location - default: .eslintcache
77 --cache-location path::String Path to the cache file or directory
78 --cache-strategy String Strategy to use for detecting changed files in the cache - either: metadata or content - default: metadata
eb39fafa
DC
79
80Miscellaneous:
34eeec05
TL
81 --init Run config initialization wizard - default: false
82 --env-info Output execution environment information - default: false
83 --no-error-on-unmatched-pattern Prevent errors when pattern is unmatched
84 --exit-on-fatal-error Exit with exit code 2 in case of fatal error - default: false
85 --debug Output debugging information
86 -h, --help Show help
87 -v, --version Output the version number
88 --print-config path::String Print the configuration for the given file
eb39fafa
DC
89```
90
91Options that accept array values can be specified by repeating the option or with a comma-delimited list (other than `--ignore-pattern` which does not allow the second style).
92
93Example:
94
95 eslint --ext .jsx --ext .js lib/
96
97 eslint --ext .jsx,.js lib/
98
99### Basic configuration
100
101#### `--no-eslintrc`
102
103Disables use of configuration from `.eslintrc.*` and `package.json` files.
104
105Example:
106
107 eslint --no-eslintrc file.js
108
109#### `-c`, `--config`
110
111This option allows you to specify an additional configuration file for ESLint (see [Configuring ESLint](configuring) for more).
112
113Example:
114
115 eslint -c ~/my-eslint.json file.js
116
117This example uses the configuration file at `~/my-eslint.json`.
118
119If `.eslintrc.*` and/or `package.json` files are also used for configuration (i.e., `--no-eslintrc` was not specified), the configurations will be merged. Options from this configuration file have precedence over the options from `.eslintrc.*` and `package.json` files.
120
121#### `--env`
122
5422a9cc 123This option enables specific environments. Details about the global variables defined by each environment are available on the [Specifying Environments](configuring/language-options.md#specifying-environments) documentation. This option only enables environments; it does not disable environments set in other configuration files. To specify multiple environments, separate them using commas, or use the option multiple times.
eb39fafa
DC
124
125Examples:
126
127 eslint --env browser,node file.js
128 eslint --env browser --env node file.js
129
130#### `--ext`
131
132This option allows you to specify which file extensions ESLint will use when searching for target files in the directories you specify.
133By default, ESLint lints `*.js` files and the files that match the `overrides` entries of your configuration.
134
135Examples:
136
137 # Use only .ts extension
138 eslint . --ext .ts
139
140 # Use both .js and .ts
141 eslint . --ext .js --ext .ts
142
143 # Also use both .js and .ts
144 eslint . --ext .js,.ts
145
146**Note:** `--ext` is only used when the arguments are directories. If you use glob patterns or file names, then `--ext` is ignored.
147
148For example, `eslint lib/* --ext .js` will match all files within the `lib/` directory, regardless of extension.
149
150#### `--global`
151
152This option defines global variables so that they will not be flagged as undefined by the `no-undef` rule. Any specified global variables are assumed to be read-only by default, but appending `:true` to a variable's name ensures that `no-undef` will also allow writes. To specify multiple global variables, separate them using commas, or use the option multiple times.
153
154Examples:
155
156 eslint --global require,exports:true file.js
157 eslint --global require --global exports:true
158
159#### `--parser`
160
161This option allows you to specify a parser to be used by ESLint. By default, `espree` will be used.
162
163#### `--parser-options`
164
165This option allows you to specify parser options to be used by ESLint. Note that the available parser options are determined by the parser being used.
166
167Examples:
168
169 echo '3 ** 4' | eslint --stdin --parser-options=ecmaVersion:6 # will fail with a parsing error
170 echo '3 ** 4' | eslint --stdin --parser-options=ecmaVersion:7 # succeeds, yay!
171
172#### `--resolve-plugins-relative-to`
173
174Changes the folder where plugins are resolved from. By default, plugins are resolved from the current working directory. This option should be used when plugins were installed by someone other than the end user. It should be set to the project directory of the project that has a dependency on the necessary plugins. For example:
175
176* When using a config file that is located outside of the current project (with the `--config` flag), if the config uses plugins which are installed locally to itself, `--resolve-plugins-relative-to` should be set to the directory containing the config file.
177* If an integration has dependencies on ESLint and a set of plugins, and the tool invokes ESLint on behalf of the user with a preset configuration, the tool should set `--resolve-plugins-relative-to` to the top-level directory of the tool.
178
179### Specifying rules and plugins
180
eb39fafa
DC
181#### `--plugin`
182
183This option specifies a plugin to load. You can omit the prefix `eslint-plugin-` from the plugin name.
184
185Before using the plugin, you have to install it using npm.
186
187Examples:
188
189 eslint --plugin jquery file.js
190 eslint --plugin eslint-plugin-mocha file.js
191
192#### `--rule`
193
194This option specifies rules to be used. These rules will be merged with any rules specified with configuration files. (You can use `--no-eslintrc` to change that behavior.) To define multiple rules, separate them using commas, or use the option multiple times. The [levn](https://github.com/gkz/levn#levn--) format is used for specifying the rules.
195
196If the rule is defined within a plugin, you have to prefix the rule ID with the plugin name and a `/`.
197
198Examples:
199
609c276f
TL
200 eslint --rule 'quotes: [error, double]'
201 eslint --rule 'guard-for-in: error' --rule 'brace-style: [error, 1tbs]'
202 eslint --rule 'jquery/dollar-sign: error'
eb39fafa 203
34eeec05
TL
204#### `--rulesdir`
205
206**Deprecated**: Use rules from plugins instead.
207
208This option allows you to specify another directory from which to load rules files. This allows you to dynamically load new rules at run time. This is useful when you have custom rules that aren't suitable for being bundled with ESLint.
209
210Example:
211
212 eslint --rulesdir my-rules/ file.js
213
214The rules in your custom rules directory must follow the same format as bundled rules to work properly. You can also specify multiple locations for custom rules by including multiple `--rulesdir` options:
215
216 eslint --rulesdir my-rules/ --rulesdir my-other-rules/ file.js
217
218Note that, as with core rules and plugin rules, you still need to enable the rules in configuration or via the `--rule` CLI option in order to actually run those rules during linting. Specifying a rules directory with `--rulesdir` does not automatically enable the rules within that directory.
219
eb39fafa
DC
220### Fixing problems
221
222#### `--fix`
223
224This option instructs ESLint to try to fix as many issues as possible. The fixes are made to the actual files themselves and only the remaining unfixed issues are output. Not all problems are fixable using this option, and the option does not work in these situations:
225
2261. This option throws an error when code is piped to ESLint.
2271. This option has no effect on code that uses a processor, unless the processor opts into allowing autofixes.
228
229If you want to fix code from `stdin` or otherwise want to get the fixes without actually writing them to the file, use the [`--fix-dry-run`](#--fix-dry-run) option.
230
231#### `--fix-dry-run`
232
233This option has the same effect as `--fix` with one difference: the fixes are not saved to the file system. This makes it possible to fix code from `stdin` (when used with the `--stdin` flag).
234
235Because the default formatter does not output the fixed code, you'll have to use another one (e.g. `json`) to get the fixes. Here's an example of this pattern:
236
34eeec05 237```sh
eb39fafa
DC
238getSomeText | eslint --stdin --fix-dry-run --format=json
239```
240
241This flag can be useful for integrations (e.g. editor plugins) which need to autofix text from the command line without saving it to the filesystem.
242
243#### `--fix-type`
244
609c276f 245This option allows you to specify the type of fixes to apply when using either `--fix` or `--fix-dry-run`. The four types of fixes are:
eb39fafa
DC
246
2471. `problem` - fix potential errors in the code
2481. `suggestion` - apply fixes to the code that improve it
2491. `layout` - apply fixes that do not change the program structure (AST)
609c276f 2501. `directive` - apply fixes to inline directives such as `// eslint-disable`
eb39fafa
DC
251
252You can specify one or more fix type on the command line. Here are some examples:
253
34eeec05 254```sh
eb39fafa
DC
255eslint --fix --fix-type suggestion .
256eslint --fix --fix-type suggestion --fix-type problem .
257eslint --fix --fix-type suggestion,layout .
258```
259
260This option is helpful if you are using another program to format your code but you would still like ESLint to apply other types of fixes.
261
262### Ignoring files
263
264#### `--ignore-path`
265
266This option allows you to specify the file to use as your `.eslintignore`. By default, ESLint looks in the current working directory for `.eslintignore`. You can override this behavior by providing a path to a different file.
267
268Example:
269
270 eslint --ignore-path tmp/.eslintignore file.js
271 eslint --ignore-path .gitignore file.js
272
273#### `--no-ignore`
274
275Disables excluding of files from `.eslintignore`, `--ignore-path`, `--ignore-pattern`, and `ignorePatterns` property in config files.
276
277Example:
278
279 eslint --no-ignore file.js
280
281#### `--ignore-pattern`
282
5422a9cc 283This option allows you to specify patterns of files to ignore (in addition to those in `.eslintignore`). You can repeat the option to provide multiple patterns. The supported syntax is the same as for `.eslintignore` [files](configuring/ignoring-code.md#the-eslintignore-file), which use the same patterns as the `.gitignore` [specification](https://git-scm.com/docs/gitignore). You should quote your patterns in order to avoid shell interpretation of glob patterns.
eb39fafa
DC
284
285Example:
286
287 eslint --ignore-pattern '/lib/' --ignore-pattern '/src/vendor/*' .
288
289### Using stdin
290
291#### `--stdin`
292
293This option tells ESLint to read and lint source code from STDIN instead of from files. You can use this to pipe code to ESLint.
294
295Example:
296
297 cat myfile.js | eslint --stdin
298
299#### `--stdin-filename`
300
301This option allows you to specify a filename to process STDIN as. This is useful when processing files from STDIN and you have rules which depend on the filename.
302
303Example
304
305 cat myfile.js | eslint --stdin --stdin-filename=myfile.js
306
307### Handling warnings
308
309#### `--quiet`
310
311This option allows you to disable reporting on warnings. If you enable this option, only errors are reported by ESLint.
312
313Example:
314
315 eslint --quiet file.js
316
317#### `--max-warnings`
318
319This option allows you to specify a warning threshold, which can be used to force ESLint to exit with an error status if there are too many warning-level rule violations in your project.
320
321Normally, if ESLint runs and finds no errors (only warnings), it will exit with a success exit status. However, if `--max-warnings` is specified and the total warning count is greater than the specified threshold, ESLint will exit with an error status. Specifying a threshold of `-1` or omitting this option will prevent this behavior.
322
323Example:
324
325 eslint --max-warnings 10 file.js
326
327### Output
328
329#### `-o`, `--output-file`
330
331Enable report to be written to a file.
332
333Example:
334
335 eslint -o ./test/test.html
336
337When specified, the given format is output into the provided file name.
338
339#### `-f`, `--format`
340
341This option specifies the output format for the console. Possible formats are:
342
343* [checkstyle](formatters.md/#checkstyle)
eb39fafa
DC
344* [compact](formatters.md/#compact)
345* [html](formatters.md/#html)
346* [jslint-xml](formatters.md/#jslint-xml)
347* [json](formatters.md/#json)
348* [junit](formatters.md/#junit)
349* [stylish](formatters.md/#stylish) (the default)
eb39fafa
DC
350* [tap](formatters.md/#tap)
351* [unix](formatters.md/#unix)
352* [visualstudio](formatters.md/#visualstudio)
353
354Example:
355
356 eslint -f compact file.js
357
358You can also use a custom formatter from the command line by specifying a path to the custom formatter file.
359
360Example:
361
362 eslint -f ./customformat.js file.js
363
364An npm-installed formatter is resolved with or without `eslint-formatter-` prefix.
365
366Example:
367
368 npm install eslint-formatter-pretty
369
370 eslint -f pretty file.js
371
372 // equivalent:
373 eslint -f eslint-formatter-pretty file.js
374
375When specified, the given format is output to the console. If you'd like to save that output into a file, you can do so on the command line like so:
376
377 eslint -f compact file.js > results.txt
378
379This saves the output into the `results.txt` file.
380
381#### `--color`, `--no-color`
382
383This option forces the enabling/disabling of colorized output. You can use this to override the default behavior, which is to enable colorized output unless no TTY is detected, such as when piping `eslint` through `cat` or `less`.
384
385Examples:
386
387 eslint --color file.js | cat
388 eslint --no-color file.js
389
390### Inline configuration comments
391
392#### `--no-inline-config`
393
394This option prevents inline comments like `/*eslint-disable*/` or
395`/*global foo*/` from having any effect. This allows you to set an ESLint
396config without files modifying it. All inline config comments are ignored, e.g.:
397
398* `/*eslint-disable*/`
399* `/*eslint-enable*/`
400* `/*global*/`
401* `/*eslint*/`
402* `/*eslint-env*/`
403* `// eslint-disable-line`
404* `// eslint-disable-next-line`
405
406Example:
407
408 eslint --no-inline-config file.js
409
410#### `--report-unused-disable-directives`
411
412This option causes ESLint to report directive comments like `// eslint-disable-line` when no errors would have been reported on that line anyway. This can be useful to prevent future errors from unexpectedly being suppressed, by cleaning up old `eslint-disable` comments which are no longer applicable.
413
414**Warning**: When using this option, it is possible that new errors will start being reported whenever ESLint or custom rules are upgraded. For example, suppose a rule has a bug that causes it to report a false positive, and an `eslint-disable` comment is added to suppress the incorrect report. If the bug is then fixed in a patch release of ESLint, the `eslint-disable` comment will become unused since ESLint is no longer generating an incorrect report. This will result in a new reported error for the unused directive if the `report-unused-disable-directives` option is used.
415
416Example:
417
418 eslint --report-unused-disable-directives file.js
419
420### Caching
421
422#### `--cache`
423
424Store the info about processed files in order to only operate on the changed ones. The cache is stored in `.eslintcache` by default. Enabling this option can dramatically improve ESLint's running time by ensuring that only changed files are linted.
425
426**Note:** If you run ESLint with `--cache` and then run ESLint without `--cache`, the `.eslintcache` file will be deleted. This is necessary because the results of the lint might change and make `.eslintcache` invalid. If you want to control when the cache file is deleted, then use `--cache-location` to specify an alternate location for the cache file.
427
428**Note:** Autofixed files are not placed in the cache. Subsequent linting that does not trigger an autofix will place it in the cache.
429
430#### `--cache-file`
431
432Path to the cache file. If none specified `.eslintcache` will be used. The file will be created in the directory where the `eslint` command is executed. **Deprecated**: Use `--cache-location` instead.
433
434#### `--cache-location`
435
436Path to the cache location. Can be a file or a directory. If no location is specified, `.eslintcache` will be used. In that case, the file will be created in the directory where the `eslint` command is executed.
437
438If a directory is specified, a cache file will be created inside the specified folder. The name of the file will be based on the hash of the current working directory (CWD). e.g.: `.cache_hashOfCWD`
439
440**Important note:** If the directory for the cache does not exist make sure you add a trailing `/` on \*nix systems or `\` in windows. Otherwise the path will be assumed to be a file.
441
442Example:
443
444 eslint "src/**/*.js" --cache --cache-location "/Users/user/.eslintcache/"
445
5422a9cc
TL
446#### `--cache-strategy`
447
448Strategy for the cache to use for detecting changed files. Can be either `metadata` or `content`. If no strategy is specified, `metadata` will be used.
449
450The `content` strategy can be useful in cases where the modification time of your files change even if their contents have not. For example, this can happen during git operations like git clone because git does not track file modification time.
451
452Example:
453
454 eslint "src/**/*.js" --cache --cache-strategy content
455
eb39fafa
DC
456### Miscellaneous
457
458#### `--init`
459
460This option will start config initialization wizard. It's designed to help new users quickly create .eslintrc file by answering a few questions, choosing a popular style guide, or inspecting your source files and attempting to automatically generate a suitable configuration.
461
462The resulting configuration file will be created in the current directory.
463
464#### `--env-info`
465
466This option outputs information about the execution environment, including the version of Node, npm, and local and global installations of ESLint. The ESLint team may ask for this information to help solve bugs.
467
468#### `--no-error-on-unmatched-pattern`
469
470This option prevents errors when a quoted glob pattern or `--ext` is unmatched. This will not prevent errors when your shell can't match a glob.
471
609c276f
TL
472#### `--exit-on-fatal-error`
473
474This option causes ESLint to exit with exit code 2 if one or more fatal parsing errors occur. Without this option, fatal parsing errors are reported as rule violations.
475
eb39fafa
DC
476#### `--debug`
477
34eeec05 478This option outputs debugging information to the console. This information is useful when you're seeing a problem and having a hard time pinpointing it. The ESLint team may ask for this debugging information to help solve bugs.
609c276f 479Add this flag to an ESLint command line invocation in order to get extra debug information as the command is run (e.g. `eslint --debug test.js` and `eslint test.js --debug` are equivalent)
eb39fafa
DC
480
481#### `-h`, `--help`
482
483This option outputs the help menu, displaying all of the available options. All other options are ignored when this is present.
484
485#### `-v`, `--version`
486
487This option outputs the current ESLint version onto the console. All other options are ignored when this is present.
488
489#### `--print-config`
490
491This option outputs the configuration to be used for the file passed. When present, no linting is performed and only config-related options are valid.
492
493Example:
494
495 eslint --print-config file.js
496
497## Ignoring files from linting
498
499ESLint supports `.eslintignore` files to exclude files from the linting process when ESLint operates on a directory. Files given as individual CLI arguments will be exempt from exclusion. The `.eslintignore` file is a plain text file containing one pattern per line. It can be located in any of the target directory's ancestors; it will affect files in its containing directory as well as all sub-directories. Here's a simple example of a `.eslintignore` file:
500
501 temp.js
502 **/vendor/*.js
503
5422a9cc 504A more detailed breakdown of supported patterns and directories ESLint ignores by default can be found in [Ignoring Code](configuring/ignoring-code.md).
eb39fafa
DC
505
506## Exit codes
507
508When linting files, ESLint will exit with one of the following exit codes:
509
510* `0`: Linting was successful and there are no linting errors. If the `--max-warnings` flag is set to `n`, the number of linting warnings is at most `n`.
511* `1`: Linting was successful and there is at least one linting error, or there are more linting warnings than allowed by the `--max-warnings` option.
512* `2`: Linting was unsuccessful due to a configuration problem or an internal error.