]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/user-guide/command-line-interface.md
first commit
[pve-eslint.git] / eslint / docs / user-guide / command-line-interface.md
1 # Command Line Interface
2
3 To run ESLint on Node.js, you must have npm installed. If npm is not installed, follow the instructions here: https://www.npmjs.com/
4
5 Once npm is installed, run the following
6
7 npm i -g eslint
8
9 This installs the ESLint CLI from the npm repository. To run ESLint, use the following format:
10
11 eslint [options] [file|dir|glob]*
12
13 Such as:
14
15 eslint file1.js file2.js
16
17 or:
18
19 eslint lib/**
20
21 Please 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
27 The command line utility has several options. You can view the options by running `eslint -h`.
28
29 ```text
30 eslint [options] file.js [file.js] [dir]
31
32 Basic configuration:
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 - default: .js
37 --global [String] Define global variables
38 --parser String Specify the parser to be used
39 --parser-options Object Specify parser options
40 --resolve-plugins-relative-to path::String A folder where plugins should be resolved from, CWD by default
41
42 Specifying rules and plugins:
43 --rulesdir [path::String] Use additional rules from this directory
44 --plugin [String] Specify plugins
45 --rule Object Specify rules
46
47 Fixing problems:
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 (problem, suggestion, layout)
51
52 Ignoring files:
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)
56
57 Using stdin:
58 --stdin Lint code provided on <STDIN> - default: false
59 --stdin-filename String Specify filename to process STDIN as
60
61 Handling warnings:
62 --quiet Report errors only - default: false
63 --max-warnings Int Number of warnings to trigger nonzero exit code - default: -1
64
65 Output:
66 -o, --output-file path::String Specify file to write report to
67 -f, --format String Use a specific output format - default: stylish
68 --color, --no-color Force enabling/disabling of color
69
70 Inline configuration comments:
71 --no-inline-config Prevent comments from changing config or rules
72 --report-unused-disable-directives Adds reported errors for unused eslint-disable directives
73
74 Caching:
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
79 Miscellaneous:
80 --init Run config initialization wizard - default: false
81 --env-info Output execution environment information - default: false
82 --no-error-on-unmatched-pattern Prevent errors when pattern is unmatched - default: false
83 --debug Output debugging information
84 -h, --help Show help
85 -v, --version Output the version number
86 --print-config path::String Print the configuration for the given file
87 ```
88
89 Options 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).
90
91 Example:
92
93 eslint --ext .jsx --ext .js lib/
94
95 eslint --ext .jsx,.js lib/
96
97 ### Basic configuration
98
99 #### `--no-eslintrc`
100
101 Disables use of configuration from `.eslintrc.*` and `package.json` files.
102
103 Example:
104
105 eslint --no-eslintrc file.js
106
107 #### `-c`, `--config`
108
109 This option allows you to specify an additional configuration file for ESLint (see [Configuring ESLint](configuring) for more).
110
111 Example:
112
113 eslint -c ~/my-eslint.json file.js
114
115 This example uses the configuration file at `~/my-eslint.json`.
116
117 If `.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.
118
119 #### `--env`
120
121 This option enables specific environments. Details about the global variables defined by each environment are available on the [configuration](configuring.md) 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.
122
123 Examples:
124
125 eslint --env browser,node file.js
126 eslint --env browser --env node file.js
127
128 #### `--ext`
129
130 This option allows you to specify which file extensions ESLint will use when searching for target files in the directories you specify.
131 By default, ESLint lints `*.js` files and the files that match the `overrides` entries of your configuration.
132
133 Examples:
134
135 # Use only .ts extension
136 eslint . --ext .ts
137
138 # Use both .js and .ts
139 eslint . --ext .js --ext .ts
140
141 # Also use both .js and .ts
142 eslint . --ext .js,.ts
143
144 **Note:** `--ext` is only used when the arguments are directories. If you use glob patterns or file names, then `--ext` is ignored.
145
146 For example, `eslint lib/* --ext .js` will match all files within the `lib/` directory, regardless of extension.
147
148 #### `--global`
149
150 This 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.
151
152 Examples:
153
154 eslint --global require,exports:true file.js
155 eslint --global require --global exports:true
156
157 #### `--parser`
158
159 This option allows you to specify a parser to be used by ESLint. By default, `espree` will be used.
160
161 #### `--parser-options`
162
163 This 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.
164
165 Examples:
166
167 echo '3 ** 4' | eslint --stdin --parser-options=ecmaVersion:6 # will fail with a parsing error
168 echo '3 ** 4' | eslint --stdin --parser-options=ecmaVersion:7 # succeeds, yay!
169
170 #### `--resolve-plugins-relative-to`
171
172 Changes 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:
173
174 * 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.
175 * 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.
176
177 ### Specifying rules and plugins
178
179 #### `--rulesdir`
180
181 This 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.
182
183 Example:
184
185 eslint --rulesdir my-rules/ file.js
186
187 The 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:
188
189 eslint --rulesdir my-rules/ --rulesdir my-other-rules/ file.js
190
191 Note 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.
192
193 #### `--plugin`
194
195 This option specifies a plugin to load. You can omit the prefix `eslint-plugin-` from the plugin name.
196
197 Before using the plugin, you have to install it using npm.
198
199 Examples:
200
201 eslint --plugin jquery file.js
202 eslint --plugin eslint-plugin-mocha file.js
203
204 #### `--rule`
205
206 This 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.
207
208 If the rule is defined within a plugin, you have to prefix the rule ID with the plugin name and a `/`.
209
210 Examples:
211
212 eslint --rule 'quotes: [2, double]'
213 eslint --rule 'guard-for-in: 2' --rule 'brace-style: [2, 1tbs]'
214 eslint --rule 'jquery/dollar-sign: 2'
215
216 ### Fixing problems
217
218 #### `--fix`
219
220 This 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:
221
222 1. This option throws an error when code is piped to ESLint.
223 1. This option has no effect on code that uses a processor, unless the processor opts into allowing autofixes.
224
225 If 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.
226
227 #### `--fix-dry-run`
228
229 This 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).
230
231 Because 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:
232
233 ```
234 getSomeText | eslint --stdin --fix-dry-run --format=json
235 ```
236
237 This 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.
238
239 #### `--fix-type`
240
241 This option allows you to specify the type of fixes to apply when using either `--fix` or `--fix-dry-run`. The three types of fixes are:
242
243 1. `problem` - fix potential errors in the code
244 1. `suggestion` - apply fixes to the code that improve it
245 1. `layout` - apply fixes that do not change the program structure (AST)
246
247 You can specify one or more fix type on the command line. Here are some examples:
248
249 ```
250 eslint --fix --fix-type suggestion .
251 eslint --fix --fix-type suggestion --fix-type problem .
252 eslint --fix --fix-type suggestion,layout .
253 ```
254
255 This 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.
256
257 ### Ignoring files
258
259 #### `--ignore-path`
260
261 This 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.
262
263 Example:
264
265 eslint --ignore-path tmp/.eslintignore file.js
266 eslint --ignore-path .gitignore file.js
267
268 #### `--no-ignore`
269
270 Disables excluding of files from `.eslintignore`, `--ignore-path`, `--ignore-pattern`, and `ignorePatterns` property in config files.
271
272 Example:
273
274 eslint --no-ignore file.js
275
276 #### `--ignore-pattern`
277
278 This 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.md#.eslintignore), 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.
279
280 Example:
281
282 eslint --ignore-pattern '/lib/' --ignore-pattern '/src/vendor/*' .
283
284 ### Using stdin
285
286 #### `--stdin`
287
288 This option tells ESLint to read and lint source code from STDIN instead of from files. You can use this to pipe code to ESLint.
289
290 Example:
291
292 cat myfile.js | eslint --stdin
293
294 #### `--stdin-filename`
295
296 This 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.
297
298 Example
299
300 cat myfile.js | eslint --stdin --stdin-filename=myfile.js
301
302 ### Handling warnings
303
304 #### `--quiet`
305
306 This option allows you to disable reporting on warnings. If you enable this option, only errors are reported by ESLint.
307
308 Example:
309
310 eslint --quiet file.js
311
312 #### `--max-warnings`
313
314 This 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.
315
316 Normally, 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.
317
318 Example:
319
320 eslint --max-warnings 10 file.js
321
322 ### Output
323
324 #### `-o`, `--output-file`
325
326 Enable report to be written to a file.
327
328 Example:
329
330 eslint -o ./test/test.html
331
332 When specified, the given format is output into the provided file name.
333
334 #### `-f`, `--format`
335
336 This option specifies the output format for the console. Possible formats are:
337
338 * [checkstyle](formatters.md/#checkstyle)
339 * [codeframe](formatters.md/#codeframe)
340 * [compact](formatters.md/#compact)
341 * [html](formatters.md/#html)
342 * [jslint-xml](formatters.md/#jslint-xml)
343 * [json](formatters.md/#json)
344 * [junit](formatters.md/#junit)
345 * [stylish](formatters.md/#stylish) (the default)
346 * [table](formatters.md/#table)
347 * [tap](formatters.md/#tap)
348 * [unix](formatters.md/#unix)
349 * [visualstudio](formatters.md/#visualstudio)
350
351 Example:
352
353 eslint -f compact file.js
354
355 You can also use a custom formatter from the command line by specifying a path to the custom formatter file.
356
357 Example:
358
359 eslint -f ./customformat.js file.js
360
361 An npm-installed formatter is resolved with or without `eslint-formatter-` prefix.
362
363 Example:
364
365 npm install eslint-formatter-pretty
366
367 eslint -f pretty file.js
368
369 // equivalent:
370 eslint -f eslint-formatter-pretty file.js
371
372 When 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:
373
374 eslint -f compact file.js > results.txt
375
376 This saves the output into the `results.txt` file.
377
378 #### `--color`, `--no-color`
379
380 This 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`.
381
382 Examples:
383
384 eslint --color file.js | cat
385 eslint --no-color file.js
386
387 ### Inline configuration comments
388
389 #### `--no-inline-config`
390
391 This option prevents inline comments like `/*eslint-disable*/` or
392 `/*global foo*/` from having any effect. This allows you to set an ESLint
393 config without files modifying it. All inline config comments are ignored, e.g.:
394
395 * `/*eslint-disable*/`
396 * `/*eslint-enable*/`
397 * `/*global*/`
398 * `/*eslint*/`
399 * `/*eslint-env*/`
400 * `// eslint-disable-line`
401 * `// eslint-disable-next-line`
402
403 Example:
404
405 eslint --no-inline-config file.js
406
407 #### `--report-unused-disable-directives`
408
409 This 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.
410
411 **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.
412
413 Example:
414
415 eslint --report-unused-disable-directives file.js
416
417 ### Caching
418
419 #### `--cache`
420
421 Store 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.
422
423 **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.
424
425 **Note:** Autofixed files are not placed in the cache. Subsequent linting that does not trigger an autofix will place it in the cache.
426
427 #### `--cache-file`
428
429 Path 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.
430
431 #### `--cache-location`
432
433 Path 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.
434
435 If 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`
436
437 **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.
438
439 Example:
440
441 eslint "src/**/*.js" --cache --cache-location "/Users/user/.eslintcache/"
442
443 ### Miscellaneous
444
445 #### `--init`
446
447 This 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.
448
449 The resulting configuration file will be created in the current directory.
450
451 #### `--env-info`
452
453 This 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.
454
455 #### `--no-error-on-unmatched-pattern`
456
457 This 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.
458
459 #### `--debug`
460
461 This 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.
462
463 #### `-h`, `--help`
464
465 This option outputs the help menu, displaying all of the available options. All other options are ignored when this is present.
466
467 #### `-v`, `--version`
468
469 This option outputs the current ESLint version onto the console. All other options are ignored when this is present.
470
471 #### `--print-config`
472
473 This option outputs the configuration to be used for the file passed. When present, no linting is performed and only config-related options are valid.
474
475 Example:
476
477 eslint --print-config file.js
478
479 ## Ignoring files from linting
480
481 ESLint 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:
482
483 temp.js
484 **/vendor/*.js
485
486 A more detailed breakdown of supported patterns and directories ESLint ignores by default can be found in [Configuring ESLint](configuring.md#ignoring-files-and-directories).
487
488 ## Exit codes
489
490 When linting files, ESLint will exit with one of the following exit codes:
491
492 * `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`.
493 * `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.
494 * `2`: Linting was unsuccessful due to a configuration problem or an internal error.