命令行接口参考

ESLint 命令行接口 (CLI) 允许你从终端执行 linting。CLI 有多种选项,你可以传递这些选项来配置 ESLint。

¥The ESLint Command Line Interface (CLI) lets you execute linting from the terminal. The CLI has a variety of options that you can pass to configure ESLint.

运行 CLI

¥Run the CLI

ESLint 需要 Node.js 来安装。按照 入门指南 中的说明安装 ESLint。

¥ESLint requires Node.js for installation. Follow the instructions in the Getting Started Guide to install ESLint.

大多数用户使用 npx 在命令行上运行 ESLint,如下所示:

¥Most users use npx to run ESLint on the command line like this:

npx eslint [options] [file|dir|glob]*

如:

¥Such as:

# Run on two files
npx eslint file1.js file2.js

# Run on multiple files
npx eslint lib/**

请注意,当传递一个 glob 作为参数时,它会被你的 shell 扩展。扩展的结果可能因你的 shell 及其配置而异。如果要使用 node glob 语法,则必须引用参数(如果需要在 Windows 中运行,请使用双引号),如下所示:

¥Please note that when passing a glob as a parameter, it is 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:

npx eslint "lib/**"

如果你使用的是 扁平配置文件 (eslint.config.js),你也可以省略文件参数,ESLint 将使用 .。例如,这两行执行相同的操作:

¥If you are using a flat configuration file (eslint.config.js), you can also omit the file arguments and ESLint will use .. For instance, these two lines perform the same operation:

npx eslint .
npx eslint

如果你不使用扁平配置文件,则运行不带文件参数的 ESLint 会导致错误。

¥If you are not using a flat configuration file, running ESLint without file arguments results in an error.

注意:你还可以使用替代包管理器(例如 Yarnpnpm)来运行 ESLint。对于 pnpm 使用 pnpm dlx eslint,对于 Yarn 使用 yarn dlx eslint

¥Note: You can also use alternative package managers such as Yarn or pnpm to run ESLint. For pnpm use pnpm dlx eslint and for Yarn use yarn dlx eslint.

将多个值传递给一个选项

¥Pass Multiple Values to an Option

接受多个值的选项可以通过重复选项或用逗号分隔的列表来指定(--ignore-pattern 除外,它不允许第二种样式)。

¥Options that accept multiple 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).

接受多个值的选项示例:

¥Examples of options that accept multiple values:

npx eslint --global describe --global it tests/
# OR
npx eslint --global describe,it tests/

选项

¥Options

你可以通过运行 npx eslint -h 查看所有 CLI 选项。

¥You can view all the CLI options by running npx eslint -h.

eslint [options] file.js [file.js] [dir]

Basic configuration:
  --no-config-lookup              Disable look up for eslint.config.js
  -c, --config path::String       Use this configuration instead of eslint.config.js, eslint.config.mjs, or
                                  eslint.config.cjs
  --inspect-config                Open the config inspector with the current configuration
  --global [String]               Define global variables
  --parser String                 Specify the parser to be used
  --parser-options Object         Specify parser options

Specify Rules and Plugins:
  --plugin [String]               Specify plugins
  --rule Object                   Specify rules

Fix Problems:
  --fix                           Automatically fix problems
  --fix-dry-run                   Automatically fix problems without saving the changes to the file system
  --fix-type Array                Specify the types of fixes to apply (directive, problem, suggestion, layout)

Ignore Files:
  --no-ignore                     Disable use of ignore files and patterns
  --ignore-pattern [String]       Patterns of files to ignore

Use stdin:
  --stdin                         Lint code provided on <STDIN> - default: false
  --stdin-filename String         Specify filename to process STDIN as

Handle Warnings:
  --quiet                         Report errors only - default: false
  --max-warnings Int              Number of warnings to trigger nonzero exit code - default: -1

Output:
  -o, --output-file path::String  Specify file to write report to
  -f, --format String             Use a specific output format - default: stylish
  --color, --no-color             Force enabling/disabling of color

Inline configuration comments:
  --no-inline-config              Prevent comments from changing config or rules
  --report-unused-disable-directives  Adds reported errors for unused eslint-disable and eslint-enable directives
  --report-unused-disable-directives-severity String  Chooses severity level for reporting unused eslint-disable and
                                                      eslint-enable directives - either: off, warn, error, 0, 1, or 2

Caching:
  --cache                         Only check changed files - default: false
  --cache-file path::String       Path to the cache file. Deprecated: use --cache-location - default: .eslintcache
  --cache-location path::String   Path to the cache file or directory
  --cache-strategy String         Strategy to use for detecting changed files in the cache - either: metadata or
                                  content - default: metadata

Miscellaneous:
  --init                          Run config initialization wizard - default: false
  --env-info                      Output execution environment information - default: false
  --no-error-on-unmatched-pattern  Prevent errors when pattern is unmatched
  --exit-on-fatal-error           Exit with exit code 2 in case of fatal error - default: false
  --no-warn-ignored               Suppress warnings when the file list includes ignored files
  --pass-on-no-patterns           Exit with exit code 0 in case no file patterns are passed
  --debug                         Output debugging information
  -h, --help                      Show help
  -v, --version                   Output the version number
  --print-config path::String     Print the configuration for the given file
  --stats                         Add statistics to the lint report - default: false
  --flag [String]                 Enable a feature flag

基本配置

¥Basic Configuration

--no-eslintrc

仅限 eslintrc 模式。禁止使用来自 .eslintrc.*package.json 文件的配置。对于扁平配置模式,请改用 --no-config-lookup

¥eslintrc Mode Only. Disables use of configuration from .eslintrc.* and package.json files. For flat config mode, use --no-config-lookup instead.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

--no-eslintrc 示例

¥--no-eslintrc example

npx eslint --no-eslintrc file.js

-c, --config

这个选项允许你为 ESLint 指定一个额外的配置文件(更多信息参见 配置 ESLint)。

¥This option allows you to specify an additional configuration file for ESLint (see Configure ESLint for more).

  • 参数类型:字符串。文件路径。

    ¥Argument Type: String. Path to file.

  • 多个参数:不

    ¥Multiple Arguments: No

-c--config 示例

¥-c, --config example

npx eslint -c ~/my.eslint.config.js file.js

此示例使用 ~/my.eslint.config.js 处的配置文件,而不是搜索 eslint.config.js 文件。

¥This example uses the configuration file at ~/my.eslint.config.js, which is used instead of searching for an eslint.config.js file.

--inspect-config

仅扁平配置模式。此选项运行 npx @eslint/config-inspector@latest 来启动配置检查器。你可以使用配置检查器更好地了解你的配置正在做什么以及它适用于哪些文件。当你使用此标志时,CLI 不会执行 linting。

¥Flat Config Mode Only. This option runs npx @eslint/config-inspector@latest to start the config inspector. You can use the config inspector to better understand what your configuration is doing and which files it applies to. When you use this flag, the CLI does not perform linting.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

--inspect-config 示例

¥--inspect-config example

npx eslint --inspect-config

--env

仅限 eslintrc 模式。此选项启用特定环境。

¥eslintrc Mode Only. This option enables specific environments.

  • 参数类型:字符串。可用环境之一。

    ¥Argument Type: String. One of the available environments.

  • 多个参数:是的

    ¥Multiple Arguments: Yes

指定环境 文档中提供了有关每个环境定义的全局变量的详细信息。此选项仅启用环境。它不会禁用在其他配置文件中设置的环境。要指定多个环境,请使用逗号分隔它们,或多次使用该选项。

¥Details about the global variables defined by each environment are available in the 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.

--env 示例

¥--env example

npx eslint --env browser,node file.js
npx eslint --env browser --env node file.js

--ext

仅限 eslintrc 模式。如果你使用扁平配置(eslint.config.js),请参阅 迁移指南

¥eslintrc Mode Only. If you are using flat config (eslint.config.js), please see migration guide.

此选项允许你指定 ESLint 在你指定的目录中搜索目标文件时使用的文件扩展名。

¥This option allows you to specify which file extensions ESLint uses when searching for target files in the directories you specify.

  • 参数类型:字符串。文件扩展名。

    ¥Argument Type: String. File extension.

  • 多个参数:是的

    ¥Multiple Arguments: Yes

  • 默认值:.js 和与配置的 overrides 条目匹配的文件。

    ¥Default Value: .js and the files that match the overrides entries of your configuration.

--ext 仅在 lint 的模式是目录时使用。如果你使用通配符模式或文件名,则 --ext 将被忽略。例如,npx eslint "lib/*" --ext .js 匹配 lib/ 目录中的所有文件,无论扩展名如何。

¥--ext is only used when the patterns to lint are directories. If you use glob patterns or file names, then --ext is ignored. For example, npx eslint "lib/*" --ext .js matches all files within the lib/ directory, regardless of extension.

--ext 示例

¥--ext example

# Use only .ts extension
npx eslint . --ext .ts

# Use both .js and .ts
npx eslint . --ext .js --ext .ts

# Also use both .js and .ts
npx eslint . --ext .js,.ts

--global

此选项定义全局变量,以便它们不会被 no-undef 规则标记为未定义。

¥This option defines global variables so that they are not flagged as undefined by the no-undef rule.

  • 参数类型:字符串。全局变量的名称。默认情况下,假定任何指定的全局变量都是只读的,但将 :true 附加到变量名称可确保 no-undef 也允许写入。

    ¥Argument Type: String. Name of the global variable. Any specified global variables are assumed to be read-only by default, but appending :true to a variable’s name ensures that no-undef also allows writes.

  • 多个参数:是的

    ¥Multiple Arguments: Yes

--global 示例

¥--global example

npx eslint --global require,exports:true file.js
npx eslint --global require --global exports:true

--parser

此选项允许你指定 ESLint 使用的解析器。

¥This option allows you to specify a parser to be used by ESLint.

  • 参数类型:字符串。ESLint 使用的解析器。

    ¥Argument Type: String. Parser to be used by ESLint.

  • 多个参数:不

    ¥Multiple Arguments: No

  • 默认值:espree

    ¥Default Value: espree

--parser 示例

¥--parser example

# Use TypeScript ESLint parser
npx eslint --parser @typescript-eslint/parser file.ts

--parser-options

此选项允许你指定 ESLint 使用的解析器选项。可用的解析器选项由正在使用的解析器决定。

¥This option allows you to specify parser options to be used by ESLint. The available parser options are determined by the parser being used.

  • 参数类型:以冒号 (:) 分隔的键/值对。

    ¥Argument Type: Key/value pair separated by colon (:).

  • 多个参数:是的

    ¥Multiple Arguments: Yes

--parser-options 示例

¥--parser-options example

echo '3 ** 4' | npx eslint --stdin --parser-options ecmaVersion:6 # fails with a parsing error
echo '3 ** 4' | npx eslint --stdin --parser-options ecmaVersion:7 # succeeds, yay!

--resolve-plugins-relative-to

仅限 eslintrc 模式。更改解析插件的目录。

¥eslintrc Mode Only. Changes the directory where plugins are resolved from.

  • 参数类型:字符串。目录路径。

    ¥Argument Type: String. Path to directory.

  • 多个参数:不

    ¥Multiple Arguments: No

  • 默认值:默认情况下,插件从找到配置文件的目录中解析。

    ¥Default Value: By default, plugins are resolved from the directory in which your configuration file is found.

当插件由终端用户以外的人安装时,应使用此选项。它应该设置为依赖于必要插件的项目的项目目录。

¥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:

  • 当使用位于当前项目之外的配置文件(带有 --config 标志)时,如果配置使用局部安装的插件,则应将 --resolve-plugins-relative-to 设置为包含配置文件的目录。

    ¥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.

  • 如果一个集成依赖于 ESLint 和一组插件,并且该工具使用预设配置代表用户调用 ESLint,则该工具应将 --resolve-plugins-relative-to 设置为该工具的顶层目录。

    ¥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.

--resolve-plugins-relative-to 示例

¥--resolve-plugins-relative-to example

npx eslint --config ~/personal-eslintrc.js \
--resolve-plugins-relative-to /usr/local/lib/

指定规则和插件

¥Specify Rules and Plugins

--plugin

此选项指定要加载的插件。

¥This option specifies a plugin to load.

  • 参数类型:字符串。插件名称。你可以选择从插件名称中省略前缀 eslint-plugin-

    ¥Argument Type: String. Plugin name. You can optionally omit the prefix eslint-plugin- from the plugin name.

  • 多个参数:是的

    ¥Multiple Arguments: Yes

在使用插件之前,你必须使用 npm 安装它。

¥Before using the plugin, you have to install it using npm.

--plugin 示例

¥--plugin example

npx eslint --plugin jquery file.js
npx eslint --plugin eslint-plugin-mocha file.js

--rule

此选项指定要使用的规则。

¥This option specifies the rules to be used.

  • 参数类型:规则及其配置以 levn 格式指定。

    ¥Argument Type: Rules and their configuration specified with levn format.

  • 多个参数:是的

    ¥Multiple Arguments: Yes

这些规则与配置文件指定的任何规则合并。如果规则是在插件中定义的,则必须在规则 ID 前加上插件名称和 /

¥These rules are merged with any rules specified with configuration files. If the rule is defined in a plugin, you have to prefix the rule ID with the plugin name and a /.

要忽略 .eslintrc 配置文件中的规则并仅运行命令行中指定的规则,请结合使用 --rule 标志和 --no-eslintrc 标志。

¥To ignore rules in .eslintrc configuration files and only run rules specified in the command line, use the --rule flag in combination with the --no-eslintrc flag.

--rule 示例

¥--rule example

# Apply single rule
npx eslint --rule 'quotes: [error, double]'
# Apply multiple rules
npx eslint --rule 'guard-for-in: error' --rule 'brace-style: [error, 1tbs]'
# Apply rule from jquery plugin
npx eslint --rule 'jquery/dollar-sign: error'
# Only apply rule from the command line
npx eslint --rule 'quotes: [error, double]' --no-eslintrc

--rulesdir

已弃用:改用插件中的规则。

¥Deprecated: Use rules from plugins instead.

仅限 eslintrc 模式。此选项允许你指定另一个目录来加载规则文件。这允许你在运行时动态加载新规则。当你有不适合与 ESLint 打包的自定义规则时,这很有用。

¥eslintrc Mode Only. 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.

  • 参数类型:字符串。目录路径。自定义规则目录中的规则必须遵循与打包规则相同的格式才能正常工作。

    ¥Argument Type: String. Path to directory. The rules in your custom rules directory must follow the same format as bundled rules to work properly.

  • 多个参数:是的。

    ¥Multiple Arguments: Yes.

请注意,与核心规则和插件规则一样,你仍然需要在配置中或通过 --rule CLI 选项启用规则,以便在代码检查期间实际运行这些规则。使用 --rulesdir 指定规则目录不会自动启用该目录中的规则。

¥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.

--rulesdir 示例

¥--rulesdir example

npx eslint --rulesdir my-rules/ file.js
npx eslint --rulesdir my-rules/ --rulesdir my-other-rules/ file.js

修复问题

¥Fix Problems

--fix

此选项指示 ESLint 尝试解决尽可能多的问题。对实际文件本身进行了修复,仅输出剩余的未修复问题。

¥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.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

并非所有问题都可以使用此选项解决,并且该选项在以下情况下不起作用:

¥Not all problems are fixable using this option, and the option does not work in these situations:

  1. 当代码通过管道传输到 ESLint 时,此选项会引发错误。

    ¥This option throws an error when code is piped to ESLint.

  2. 此选项对使用处理器的代码没有影响,除非处理器选择允许自动修复。

    ¥This option has no effect on code that uses a processor, unless the processor opts into allowing autofixes.

如果你想修复来自 stdin 的代码,或者想要获得修复而不实际将它们写入文件,请使用 --fix-dry-run 选项。

¥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 option.

--fix 示例

¥--fix example

npx eslint --fix file.js

--fix-dry-run

此选项与 --fix 具有相同的效果,区别在于修复不会保存到文件系统。因为默认格式化程序不输出固定代码,所以你必须使用另一个格式化程序(例如 --format json)来获得修复。

¥This option has the same effect as --fix with the difference that the fixes are not saved to the file system. Because the default formatter does not output the fixed code, you’ll have to use another formatter (e.g. --format json) to get the fixes.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

这使得在与 --stdin 标志一起使用时可以修复来自 stdin 的代码。

¥This makes it possible to fix code from stdin when used with the --stdin flag.

此标志对于需要从命令行自动修复文本而不将其保存到文件系统的集成(例如编辑器插件)很有用。

¥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.

--fix-dry-run 示例

¥--fix-dry-run example

getSomeText | npx eslint --stdin --fix-dry-run --format json

--fix-type

此选项允许你指定在使用 --fix--fix-dry-run 时应用的修复类型。

¥This option allows you to specify the type of fixes to apply when using either --fix or --fix-dry-run.

  • 参数类型:字符串。以下修复类型之一:

    ¥Argument Type: String. One of the following fix types:

    1. problem - 修复代码中的潜在错误

      ¥problem - fix potential errors in the code

    2. suggestion - 将修复应用于改进它的代码

      ¥suggestion - apply fixes to the code that improve it

    3. layout - 应用不改变程序结构 (AST) 的修复

      ¥layout - apply fixes that do not change the program structure (AST)

    4. directive - 将修复应用于内联指令,例如 // eslint-disable

      ¥directive - apply fixes to inline directives such as // eslint-disable

  • 多个参数:是的

    ¥Multiple Arguments: Yes

如果你正在使用另一个程序来格式化代码,但你仍然希望 ESLint 应用其他类型的修复,则此选项很有用。

¥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.

--fix-type 示例

¥--fix-type example

npx eslint --fix --fix-type suggestion .
npx eslint --fix --fix-type suggestion --fix-type problem .
npx eslint --fix --fix-type suggestion,layout .

忽略文件

¥Ignore Files

--ignore-path

仅限 eslintrc 模式。此选项允许你指定要用作 .eslintignore 的文件。

¥eslintrc Mode Only. This option allows you to specify the file to use as your .eslintignore.

  • 参数类型:字符串。文件路径。

    ¥Argument Type: String. Path to file.

  • 多个参数:不

    ¥Multiple Arguments: No

  • 默认值:默认情况下,ESLint 在当前工作目录中查找 .eslintignore

    ¥Default Value: By default, ESLint looks for .eslintignore in the current working directory.

注意:仅当使用 已弃用的配置 时才支持 --ignore-path。如果你想在 eslint.config.js 文件中包括来自 .gitignore 文件的模式,请参阅 包括 .gitignore 文件

¥Note: --ignore-path is only supported when using deprecated configuration. If you want to include patterns from a .gitignore file in your eslint.config.js file, please see including .gitignore files.

--ignore-path 示例

¥--ignore-path example

npx eslint --ignore-path tmp/.eslintignore file.js
npx eslint --ignore-path .gitignore file.js

--no-ignore

禁止从 .eslintignore 文件、--ignore-path 标志、--ignore-pattern 标志和配置文件中的 ignorePatterns 属性中排除文件。

¥Disables excluding of files from .eslintignore files, --ignore-path flags, --ignore-pattern flags, and the ignorePatterns property in config files.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

--no-ignore 示例

¥--no-ignore example

npx eslint --no-ignore file.js

--ignore-pattern

此选项允许你指定要忽略的文件模式。在 eslintrc 模式下,这些是 .eslintignore 之外的。

¥This option allows you to specify patterns of files to ignore. In eslintrc mode, these are in addition to .eslintignore.

  • 参数类型:字符串。支持的语法与 .eslintignore 文件 相同,它使用与 .gitignore 规范 相同的模式。你应该引用你的模式以避免对通配符模式的 shell 解释。

    ¥Argument Type: String. The supported syntax is the same as for .eslintignore files, which use the same patterns as the .gitignore specification. You should quote your patterns in order to avoid shell interpretation of glob patterns.

  • 多个参数:是的

    ¥Multiple Arguments: Yes

--ignore-pattern 示例

¥--ignore-pattern example

npx eslint --ignore-pattern "/lib/" --ignore-pattern "/src/vendor/*" .

使用标准输入

¥Use stdin

--stdin

这个选项告诉 ESLint 从 STDIN 而不是从文件中读取和检查源代码。你可以使用它将代码通过管道传输到 ESLint。

¥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.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

--stdin 示例

¥--stdin example

cat myfile.js | npx eslint --stdin

--stdin-filename

此选项允许你指定一个文件名来处理 STDIN。

¥This option allows you to specify a filename to process STDIN as.

  • 参数类型:字符串。文件路径。

    ¥Argument Type: String. Path to file.

  • 多个参数:不

    ¥Multiple Arguments: No

这在处理来自 STDIN 的文件并且你有依赖于文件名的规则时很有用。

¥This is useful when processing files from STDIN and you have rules which depend on the filename.

--stdin-filename 示例

¥--stdin-filename example

cat myfile.js | npx eslint --stdin --stdin-filename myfile.js

处理警告

¥Handle Warnings

--quiet

此选项允许你禁用警告报告和运行设置为警告的规则。如果启用此选项,ESLint 仅报告错误,并且仅运行设置为错误的规则。

¥This option allows you to disable reporting on warnings and running of rules set to warn. If you enable this option, only errors are reported by ESLint and only rules set to error will be run.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

--quiet 示例

¥--quiet example

npx eslint --quiet file.js

--max-warnings

此选项允许你指定警告阈值,如果你的项目中有太多警告级别的规则违规,可用于强制 ESLint 以错误状态退出。

¥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.

  • 参数类型:整数。允许的最大警告数。要防止此行为,请不要使用此选项或将 -1 指定为参数。

    ¥Argument Type: Integer. The maximum number of warnings to allow. To prevent this behavior, do not use this option or specify -1 as the argument.

  • 多个参数:不

    ¥Multiple Arguments: No

通常,如果 ESLint 运行并且没有发现错误(只有警告),它会以成功退出状态退出。但是,如果指定了 --max-warnings 并且警告总数大于指定的阈值,ESLint 将以错误状态退出。

¥Normally, if ESLint runs and finds no errors (only warnings), it exits with a success exit status. However, if --max-warnings is specified and the total warning count is greater than the specified threshold, ESLint exits with an error status.

--max-warnings 示例

¥--max-warnings example

npx eslint --max-warnings 10 file.js

输出

¥Output

-o, --output-file

将 linting 结果的输出写入指定文件。

¥Write the output of linting results to a specified file.

  • 参数类型:字符串。文件路径。

    ¥Argument Type: String. Path to file.

  • 多个参数:不

    ¥Multiple Arguments: No

-o--output-file 示例

¥-o, --output-file example

npx eslint -o ./test/test.html

-f, --format

此选项指定控制台的输出格式。

¥This option specifies the output format for the console.

如果你使用的是在局部文件中定义的自定义格式化程序,则可以指定自定义格式化程序文件的路径。

¥If you are using a custom formatter defined in a local file, you can specify the path to the custom formatter file.

使用或不使用 eslint-formatter- 前缀解析 npm 安装的格式化程序。

¥An npm-installed formatter is resolved with or without eslint-formatter- prefix.

指定时,将给定格式输出到控制台。如果你想将该输出保存到文件中,可以在命令行中执行此操作,如下所示:

¥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:

# Saves the output into the `results.json` file.
npx eslint -f json file.js > results.json

-f--format 示例

¥-f, --format example

使用内置的 json 格式化程序:

¥Use the built-in json formatter:

npx eslint --format json file.js

使用局部自定义格式化程序:

¥Use a local custom formatter:

npx eslint -f ./customformat.js file.js

使用 npm 安装的格式化程序:

¥Use an npm-installed formatter:

npm install eslint-formatter-pretty

# Then run one of the following commands
npx eslint -f pretty file.js
# or alternatively
npx eslint -f eslint-formatter-pretty file.js

--color--no-color

¥--color and --no-color

这些选项强制启用/禁用彩色输出。

¥These options force the enabling/disabling of colorized output.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

你可以使用这些选项来覆盖默认行为,即启用彩色输出,除非未检测到 TTY,例如当管道 eslintcatless 时。

¥You can use these options 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.

--color--no-color 示例

¥--color and --no-color example

npx eslint --color file.js | cat
npx eslint --no-color file.js

内联配置注释

¥Inline Configuration Comments

--no-inline-config

此选项可防止 /*eslint-disable*//*global foo*/ 等内联注释产生任何影响。

¥This option prevents inline comments like /*eslint-disable*/ or /*global foo*/ from having any effect.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

这允许你设置 ESLint 配置,而无需文件修改它。所有内联配置注释都将被忽略,例如:

¥This allows you to set an ESLint config without files modifying it. All inline config comments are ignored, such as:

  • /*eslint-disable*/

  • /*eslint-enable*/

  • /*global*/

  • /*eslint*/

  • /*eslint-env*/

  • // eslint-disable-line

  • // eslint-disable-next-line

--no-inline-config 示例

¥--no-inline-config example

npx eslint --no-inline-config file.js

--report-unused-disable-directives

此选项会导致 ESLint 报告指令注释,如 // eslint-disable-line 无论如何不会在该行上报告错误。

¥This option causes ESLint to report directive comments like // eslint-disable-line when no errors would have been reported on that line anyway.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

通过清理不再适用的旧 eslint-disableeslint-enable 注释,这对于防止将来的错误意外被抑制非常有用。

¥This can be useful to prevent future errors from unexpectedly being suppressed, by cleaning up old eslint-disable and eslint-enable comments which are no longer applicable.

--report-unused-disable-directives 示例

¥--report-unused-disable-directives example

npx eslint --report-unused-disable-directives file.js

--report-unused-disable-directives-severity

--report-unused-disable-directives 相同,但允许你指定报告错误的严重级别(errorwarnoff)。一次只能使用这两个选项之一。

¥Same as --report-unused-disable-directives, but allows you to specify the severity level (error, warn, off) of the reported errors. Only one of these two options can be used at a time.

  • 参数类型:字符串。以下值之一:

    ¥Argument Type: String. One of the following values:

    1. off(或 0

      ¥off (or 0)

    2. warn(或 1

      ¥warn (or 1)

    3. error(或 2

      ¥error (or 2)

  • 多个参数:不

    ¥Multiple Arguments: No

  • 默认值:默认情况下,使用 linterOptions.reportUnusedDisableDirectives 配置设置(默认为 "warn")。

    ¥Default Value: By default, linterOptions.reportUnusedDisableDirectives configuration setting is used (which defaults to "warn").

--report-unused-disable-directives-severity 示例

¥--report-unused-disable-directives-severity example

npx eslint --report-unused-disable-directives-severity warn file.js

缓存

¥Caching

--cache

存储有关已处理文件的信息,以便仅对更改的文件进行操作。启用此选项可以通过确保仅检查更改的文件来显着提高 ESLint 的运行时性能。缓存默认存储在 .eslintcache 中。

¥Store the info about processed files in order to only operate on the changed ones. Enabling this option can dramatically improve ESLint’s run time performance by ensuring that only changed files are linted. The cache is stored in .eslintcache by default.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

如果你用 --cache 运行 ESLint,然后在没有 --cache 的情况下运行 ESLint,.eslintcache 文件将被删除。这是必要的,因为 lint 的结果可能会更改并使 .eslintcache 无效。如果要控制何时删除缓存文件,请使用 --cache-location 指定缓存文件的备用位置。

¥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.

自动修复的文件不会放在缓存中。不触发自动修复的后续 linting 会将其放入缓存中。

¥Autofixed files are not placed in the cache. Subsequent linting that does not trigger an autofix will place it in the cache.

--cache 示例

¥--cache example

npx eslint --cache file.js

--cache-file

已弃用:请改用 --cache-location

¥Deprecated: Use --cache-location instead.

缓存文件的路径。如果没有使用指定的 .eslintcache。该文件在执行 eslint 命令的目录中创建。

¥Path to the cache file. If none specified .eslintcache is used. The file is created in the directory where the eslint command is executed.

--cache-location

指定缓存位置的路径。可以是文件或目录。

¥Specify the path to the cache location. Can be a file or a directory.

  • 参数类型:字符串。文件或目录的路径。如果指定了目录,则会在指定文件夹内创建缓存文件。该文件的名称基于当前工作目录的哈希值,例如:.cache_hashOfCWD

    ¥Argument Type: String. Path to file or directory. If a directory is specified, a cache file is created inside the specified folder. The name of the file is based on the hash of the current working directory, e.g.: .cache_hashOfCWD.

  • 多个参数:不

    ¥Multiple Arguments: No

  • 默认值:如果未指定位置,则使用 .eslintcache。该文件在执行 eslint 命令的目录中创建。

    ¥Default Value: If no location is specified, .eslintcache is used. The file is created in the directory where the eslint command is executed.

如果缓存目录不存在,请确保在 *nix 系统上添加尾随 / 或在 Windows 上添加尾随 \。否则,该路径被假定为一个文件。

¥If the directory for the cache does not exist make sure you add a trailing / on *nix systems or \ on Windows. Otherwise, the path is assumed to be a file.

--cache-location 示例

¥--cache-location example

npx eslint "src/**/*.js" --cache --cache-location "/Users/user/.eslintcache/"

--cache-strategy

用于检测更改文件的缓存策略。

¥Strategy for the cache to use for detecting changed files.

  • 参数类型:字符串。以下值之一:

    ¥Argument Type: String. One of the following values:

    1. metadata
    2. content
  • 多个参数:不

    ¥Multiple Arguments: No

  • 默认值:metadata

    ¥Default Value: metadata

content 策略在文件的修改时间发生变化但内容没有变化的情况下很有用。例如,这可能发生在像 git clone 这样的 git 操作期间,因为 git 不跟踪文件修改时间。

¥The content strategy can be useful in cases where the modification time of your files changes 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.

--cache-strategy 示例

¥--cache-strategy example

npx eslint "src/**/*.js" --cache --cache-strategy content

其他

¥Miscellaneous

--init

此选项运行 npm init @eslint/config 以启动配置初始化向导。它旨在通过回答几个问题帮助新用户快速创建 .eslintrc 文件。当你使用此标志时,CLI 不会执行 linting。

¥This option runs npm init @eslint/config to start the config initialization wizard. It’s designed to help new users quickly create an .eslintrc file by answering a few questions. When you use this flag, the CLI does not perform linting.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

生成的配置文件在当前目录中创建。

¥The resulting configuration file is created in the current directory.

--init 示例

¥--init example

npx eslint --init

--env-info

此选项输出有关执行环境的信息,包括 Node.js、npm 的版本以及 ESLint 的局部和全局安装。

¥This option outputs information about the execution environment, including the version of Node.js, npm, and local and global installations of ESLint.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

ESLint 团队可能会要求提供此信息以帮助解决错误。当你使用此标志时,CLI 不会执行 linting。

¥The ESLint team may ask for this information to help solve bugs. When you use this flag, the CLI does not perform linting.

--env-info 示例

¥--env-info example

npx eslint --env-info

--no-error-on-unmatched-pattern

当引用的通配符模式或 --ext 不匹配时,此选项可防止错误。当你的 shell 无法匹配 glob 时,这并不能防止错误。

¥This option prevents errors when a quoted glob pattern or --ext is unmatched. This does not prevent errors when your shell can’t match a glob.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

--no-error-on-unmatched-pattern 示例

¥--no-error-on-unmatched-pattern example

npx eslint --no-error-on-unmatched-pattern --ext .ts "lib/*"

--exit-on-fatal-error

如果发生一个或多个致命的解析错误,此选项会导致 ESLint 以退出代码 2 退出。如果没有此选项,ESLint 会将致命的解析错误报告为违反规则。

¥This option causes ESLint to exit with exit code 2 if one or more fatal parsing errors occur. Without this option, ESLint reports fatal parsing errors as rule violations.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

--exit-on-fatal-error 示例

¥--exit-on-fatal-error example

npx eslint --exit-on-fatal-error file.js

--no-warn-ignored

仅扁平配置模式。当显式传递忽略的文件名时,此选项会抑制 File ignored by defaultFile ignored because of a matching ignore pattern 警告。与 --max-warnings 0 配对时非常有用,因为它会防止由于上述警告而出现退出代码 1。

¥Flat Config Mode Only. This option suppresses both File ignored by default and File ignored because of a matching ignore pattern warnings when an ignored filename is passed explicitly. It is useful when paired with --max-warnings 0 as it will prevent exit code 1 due to the aforementioned warning.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

--no-warn-ignored 示例

¥--no-warn-ignored example

npx eslint --no-warn-ignored --max-warnings 0 ignored-file.js

--pass-on-no-patterns

当没有文件或目录模式被传递时,此选项允许 ESLint 以代码 0 退出。如果没有此选项,ESLint 会假设你要使用 . 作为模式。(当在旧版 eslintrc 模式下运行时,ESLint 将退出并显示代码 1。)

¥This option allows ESLint to exit with code 0 when no file or directory patterns are passed. Without this option, ESLint assumes you want to use . as the pattern. (When running in legacy eslintrc mode, ESLint will exit with code 1.)

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

--pass-on-no-patterns 示例

¥--pass-on-no-patterns example

npx eslint --pass-on-no-patterns

--debug

此选项将调试信息输出到控制台。将此标志添加到 ESLint 命令行调用,以便在命令运行时获取额外的调试信息。

¥This option outputs debugging information to the console. Add this flag to an ESLint command line invocation in order to get extra debugging information while the command runs.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

当你发现问题并难以确定问题时,此信息很有用。ESLint 团队可能会要求提供此调试信息以帮助解决错误。

¥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.

--debug 示例

¥--debug example

npx eslint --debug test.js

-h, --help

此选项输出帮助菜单,显示所有可用选项。当它存在时,所有其他选项都将被忽略。当你使用此标志时,CLI 不会执行 linting。

¥This option outputs the help menu, displaying all of the available options. All other options are ignored when this is present. When you use this flag, the CLI does not perform linting.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

-h--help 示例

¥-h, --help example

npx eslint --help

-v, --version

此选项将当前 ESLint 版本输出到控制台。当它存在时,所有其他选项都将被忽略。当你使用此标志时,CLI 不会执行 linting。

¥This option outputs the current ESLint version onto the console. All other options are ignored when this is present. When you use this flag, the CLI does not perform linting.

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

-v--version 示例

¥-v, --version example

npx eslint --version

--print-config

此选项输出用于传递的文件的配置。如果存在,则不会执行检查,并且只有与配置相关的选项有效。当你使用此标志时,CLI 不会执行 linting。

¥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. When you use this flag, the CLI does not perform linting.

  • 参数类型:字符串。文件路径。

    ¥Argument Type: String. Path to file.

  • 多个参数:不

    ¥Multiple Arguments: No

--print-config 示例

¥--print-config example

npx eslint --print-config file.js

--stats

此选项向传递给格式化程序的 result 对象添加一系列详细的性能统计信息(请参阅 统计类型),例如解析、修复和 lint 时间(每个规则的时间)(请参阅 统计 CLI 使用情况)。

¥This option adds a series of detailed performance statistics (see Stats type) such as the parse-, fix- and lint-times (time per rule) to result objects that are passed to the formatter (see Stats CLI usage).

  • 参数类型:没有参数。

    ¥Argument Type: No argument.

此选项旨在与显示统计信息的自定义格式化程序一起使用。它还可以与内置 json 格式化程序一起使用。

¥This option is intended for use with custom formatters that display statistics. It can also be used with the built-in json formatter.

--stats 示例

¥--stats example

npx eslint --stats --format json file.js

--flag

此选项为 ESLint 启用一个或多个功能标志。

¥This option enables one or more feature flags for ESLint.

  • 参数类型:字符串。特性标识符。

    ¥Argument Type: String. A feature identifier.

  • 多个参数:是的

    ¥Multiple Arguments: Yes

--flag 示例

¥--flag example

npx eslint --flag x_feature file.js

退出码

¥Exit Codes

对文件进行 linting 时,ESLint 会使用以下退出代码之一退出:

¥When linting files, ESLint exits with one of the following exit codes:

  • 0:Linting 成功并且没有 linting 错误。如果 --max-warnings 标志设置为 n,则 linting 警告的数量最多为 n

    ¥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.

  • 1:linting 成功并且至少存在一个 linting 错误,或者 linting 警告多于 --max-warnings 选项所允许的数量。

    ¥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.

  • 2:由于配置问题或内部错误,检查不成功。

    ¥2: Linting was unsuccessful due to a configuration problem or an internal error.