版本

命令行界面参考

ESLint 命令行界面 (CLI) 允许您从终端执行代码检查。CLI 有多种选项可以传递以配置 ESLint。

运行 CLI

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

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

npm

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

yarn

yarn dlx eslint [options] [file|dir|glob]* 

pnpm

pnpm dlx eslint [options] [file|dir|glob]* 

bun

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

例如

npm

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

yarn

# Run on two files
yarn dlx eslint file1.js file2.js 

pnpm

# Run on two files
pnpm dlx eslint file1.js file2.js 

bun

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

或者

npm

# Run on multiple files
npx eslint lib/** 

yarn

# Run on multiple files
yarn dlx eslint lib/** 

pnpm

# Run on multiple files
pnpm dlx eslint lib/** 

bun

# Run on multiple files
bunx eslint lib/** 

请注意,当将 glob 作为参数传递时,它将由您的 shell 展开。展开的结果可能因您的 shell 及其配置而异。如果您想使用 node glob 语法,则必须引用您的参数(如果需要在 Windows 上运行,则使用双引号),如下所示

npm

npx eslint "lib/**" 

yarn

yarn dlx eslint "lib/**" 

pnpm

pnpm dlx eslint "lib/**" 

bun

bunx eslint "lib/**" 

您也可以省略文件参数,ESLint 将使用 .。例如,这两行执行相同的操作

npm

npx eslint . 

yarn

yarn dlx eslint . 

pnpm

pnpm dlx eslint . 

bun

bunx eslint . 

npm

npx eslint 

yarn

yarn dlx eslint 

pnpm

pnpm dlx eslint 

bun

bunx eslint 

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

向选项传递多个值

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

接受多个值的选项示例

npm

npx eslint --global describe --global it tests/ 

yarn

yarn dlx eslint --global describe --global it tests/ 

pnpm

pnpm dlx eslint --global describe --global it tests/ 

bun

bunx eslint --global describe --global it tests/ 

或者

npm

npx eslint --global describe,it tests/ 

yarn

yarn dlx eslint --global describe,it tests/ 

pnpm

pnpm dlx eslint --global describe,it tests/ 

bun

bunx eslint --global describe,it tests/ 

选项

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

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
  --ext [String]                   Specify additional file extensions to lint
  --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
  --report-unused-inline-configs String  Adds reported errors for unused eslint inline config comments - 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

Suppressing Violations:
  --suppress-all                   Suppress all violations - default: false
  --suppress-rule [String]         Suppress specific rules
  --suppressions-location path::String  Specify the location of the suppressions file
  --prune-suppressions             Prune unused suppressions - default: false
  --pass-on-unpruned-suppressions  Ignore unused suppressions - default: false

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
  --mcp                            Start the ESLint MCP server
  --concurrency Int|String         Number of linting threads, auto to choose automatically, off for no multithreading - default: off

基本配置

--no-config-lookup

禁用从文件中使用配置。

  • 参数类型:无参数。
--no-config-lookup 示例

npm

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

yarn

yarn dlx eslint --no-config-lookup file.js 

pnpm

pnpm dlx eslint --no-config-lookup file.js 

bun

bunx eslint --no-config-lookup file.js 

-c, --config

此选项允许您为 ESLint 指定额外的配置文件(有关更多信息,请参阅 配置 ESLint)。

  • 参数类型:字符串。文件路径。
  • 多个参数:否
-c, --config 示例

npm

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

yarn

yarn dlx eslint -c ~/my.eslint.config.js file.js 

pnpm

pnpm dlx eslint -c ~/my.eslint.config.js file.js 

bun

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

此示例使用位于 ~/my.eslint.config.js 的配置文件,它代替搜索 eslint.config.js 文件。

--inspect-config

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

  • 参数类型:无参数。
--inspect-config 示例

npm

npx eslint --inspect-config 

yarn

yarn dlx eslint --inspect-config 

pnpm

pnpm dlx eslint --inspect-config 

bun

bunx eslint --inspect-config 

--ext

此选项允许您指定要代码检查的附加文件扩展名。

  • 参数类型:字符串。文件扩展名。
  • 多个参数:是
  • 默认值:默认情况下,ESLint 代码检查具有扩展名 .js.mjs.cjs 的文件,以及 在配置文件中指定的 附加扩展名。

此选项主要用于与 --no-config-lookup 选项结合使用,因为在这种情况下没有配置文件可以指定附加扩展名。

--ext 示例

npm

# Include .ts files
npx eslint . --ext .ts 

yarn

# Include .ts files
yarn dlx eslint . --ext .ts 

pnpm

# Include .ts files
pnpm dlx eslint . --ext .ts 

bun

# Include .ts files
bunx eslint . --ext .ts 

npm

# Include .ts and .tsx files
npx eslint . --ext .ts --ext .tsx 

yarn

# Include .ts and .tsx files
yarn dlx eslint . --ext .ts --ext .tsx 

pnpm

# Include .ts and .tsx files
pnpm dlx eslint . --ext .ts --ext .tsx 

bun

# Include .ts and .tsx files
bunx eslint . --ext .ts --ext .tsx 

npm

# Also include .ts and .tsx files
npx eslint . --ext .ts,.tsx 

yarn

# Also include .ts and .tsx files
yarn dlx eslint . --ext .ts,.tsx 

pnpm

# Also include .ts and .tsx files
pnpm dlx eslint . --ext .ts,.tsx 

bun

# Also include .ts and .tsx files
bunx eslint . --ext .ts,.tsx 

--global

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

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

npm

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

yarn

yarn dlx eslint --global require,exports:true file.js 

pnpm

pnpm dlx eslint --global require,exports:true file.js 

bun

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

npm

npx eslint --global require --global exports:true 

yarn

yarn dlx eslint --global require --global exports:true 

pnpm

pnpm dlx eslint --global require --global exports:true 

bun

bunx eslint --global require --global exports:true 

--parser

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

  • 参数类型:字符串。ESLint 要使用的解析器。
  • 多个参数:否
  • 默认值espree
--parser 示例

npm

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

yarn

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

pnpm

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

bun

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

--parser-options

此选项允许您指定 ESLint 要使用的解析器选项。可用的解析器选项由正在使用的解析器确定。

  • 参数类型:键/值对,用冒号 (:) 分隔。
  • 多个参数:是
--parser-options 示例

npm

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

yarn

# fails with a parsing error
echo '3 ** 4' | yarn dlx eslint --stdin --parser-options ecmaVersion:6 

pnpm

# fails with a parsing error
echo '3 ** 4' | pnpm dlx eslint --stdin --parser-options ecmaVersion:6 

bun

# fails with a parsing error
echo '3 ** 4' | bunx eslint --stdin --parser-options ecmaVersion:6 

npm

# succeeds, yay!
echo '3 ** 4' | npx eslint --stdin --parser-options ecmaVersion:7 

yarn

# succeeds, yay!
echo '3 ** 4' | yarn dlx eslint --stdin --parser-options ecmaVersion:7 

pnpm

# succeeds, yay!
echo '3 ** 4' | pnpm dlx eslint --stdin --parser-options ecmaVersion:7 

bun

# succeeds, yay!
echo '3 ** 4' | bunx eslint --stdin --parser-options ecmaVersion:7 

指定规则和插件

--plugin

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

  • 参数类型:字符串。插件名称。您可以选择省略插件名称中的前缀 eslint-plugin-
  • 多个参数:是

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

--plugin 示例

npm

npx eslint --plugin jquery file.js 

yarn

yarn dlx eslint --plugin jquery file.js 

pnpm

pnpm dlx eslint --plugin jquery file.js 

bun

bunx eslint --plugin jquery file.js 

npm

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

yarn

yarn dlx eslint --plugin eslint-plugin-mocha file.js 

pnpm

pnpm dlx eslint --plugin eslint-plugin-mocha file.js 

bun

bunx eslint --plugin eslint-plugin-mocha file.js 

--rule

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

  • 参数类型:使用 levn 格式指定的规则及其配置。
  • 多个参数:是

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

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

--rule 示例

npm

# Apply single rule
npx eslint --rule 'quotes: [error, double]' 

yarn

# Apply single rule
yarn dlx eslint --rule 'quotes: [error, double]' 

pnpm

# Apply single rule
pnpm dlx eslint --rule 'quotes: [error, double]' 

bun

# Apply single rule
bunx eslint --rule 'quotes: [error, double]' 

npm

# Apply multiple rules
npx eslint --rule 'guard-for-in: error' --rule 'brace-style: [error, 1tbs]' 

yarn

# Apply multiple rules
yarn dlx eslint --rule 'guard-for-in: error' --rule 'brace-style: [error, 1tbs]' 

pnpm

# Apply multiple rules
pnpm dlx eslint --rule 'guard-for-in: error' --rule 'brace-style: [error, 1tbs]' 

bun

# Apply multiple rules
bunx eslint --rule 'guard-for-in: error' --rule 'brace-style: [error, 1tbs]' 

npm

# Apply rule from jquery plugin
npx eslint --rule 'jquery/dollar-sign: error' 

yarn

# Apply rule from jquery plugin
yarn dlx eslint --rule 'jquery/dollar-sign: error' 

pnpm

# Apply rule from jquery plugin
pnpm dlx eslint --rule 'jquery/dollar-sign: error' 

bun

# Apply rule from jquery plugin
bunx eslint --rule 'jquery/dollar-sign: error' 

npm

# Only apply rule from the command line
npx eslint --rule 'quotes: [error, double]' --no-config-lookup 

yarn

# Only apply rule from the command line
yarn dlx eslint --rule 'quotes: [error, double]' --no-config-lookup 

pnpm

# Only apply rule from the command line
pnpm dlx eslint --rule 'quotes: [error, double]' --no-config-lookup 

bun

# Only apply rule from the command line
bunx eslint --rule 'quotes: [error, double]' --no-config-lookup 

修复问题

--fix

此选项指示 ESLint 尝试 修复 尽可能多的问题。修复会直接对实际文件进行,并且仅输出剩余的未修复问题。

  • 参数类型:无参数。

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

  1. 此选项会在将代码管道传输到 ESLint 时引发错误。
  2. 此选项对使用处理器代码没有影响,除非处理器选择允许自动修复。

如果您想修复来自 stdin 的代码或以其他方式获取修复而不实际将其写入文件,请使用 --fix-dry-run 选项。

--fix 示例

npm

npx eslint --fix file.js 

yarn

yarn dlx eslint --fix file.js 

pnpm

pnpm dlx eslint --fix file.js 

bun

bunx eslint --fix file.js 

--fix-dry-run

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

  • 参数类型:无参数。

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

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

--fix-dry-run 示例

npm

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

yarn

getSomeText | yarn dlx eslint --stdin --fix-dry-run --format json 

pnpm

getSomeText | pnpm dlx eslint --stdin --fix-dry-run --format json 

bun

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

--fix-type

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

  • 参数类型:字符串。以下修复类型之一
    1. problem - 修复代码中的潜在错误
    2. suggestion - 应用修复以改进代码
    3. layout - 应用不更改程序结构 (AST) 的修复
    4. directive - 应用对内联指令(例如 // eslint-disable)的修复
  • 多个参数:是

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

--fix-type 示例

npm

npx eslint --fix --fix-type suggestion . 

yarn

yarn dlx eslint --fix --fix-type suggestion . 

pnpm

pnpm dlx eslint --fix --fix-type suggestion . 

bun

bunx eslint --fix --fix-type suggestion . 

npm

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

yarn

yarn dlx eslint --fix --fix-type suggestion --fix-type problem . 

pnpm

pnpm dlx eslint --fix --fix-type suggestion --fix-type problem . 

bun

bunx eslint --fix --fix-type suggestion --fix-type problem . 

npm

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

yarn

yarn dlx eslint --fix --fix-type suggestion,layout . 

pnpm

pnpm dlx eslint --fix --fix-type suggestion,layout . 

bun

bunx eslint --fix --fix-type suggestion,layout . 

忽略文件

--no-ignore

禁用排除来自 --ignore-pattern 标志和配置中的 ignores 属性的文件。

  • 参数类型:无参数。
--no-ignore 示例

npm

npx eslint --no-ignore file.js 

yarn

yarn dlx eslint --no-ignore file.js 

pnpm

pnpm dlx eslint --no-ignore file.js 

bun

bunx eslint --no-ignore file.js 

--ignore-pattern

此选项允许您指定要忽略的文件模式。

  • 参数类型:字符串。支持的语法与 ignores 模式 相同,它使用 minimatch 语法。您应该引用模式以避免 shell 解释 glob 模式。
  • 多个参数:是
--ignore-pattern 示例

npm

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

yarn

yarn dlx eslint --ignore-pattern "/lib/" --ignore-pattern "/src/vendor/*" . 

pnpm

pnpm dlx eslint --ignore-pattern "/lib/" --ignore-pattern "/src/vendor/*" . 

bun

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

使用 stdin

--stdin

此选项告诉 ESLint 从 STDIN 而不是从文件读取和代码检查源代码。您可以使用此选项将代码管道传输到 ESLint。

  • 参数类型:无参数。
--stdin 示例

npm

cat myFile.js | npx eslint --stdin 

yarn

cat myFile.js | yarn dlx eslint --stdin 

pnpm

cat myFile.js | pnpm dlx eslint --stdin 

bun

cat myFile.js | bunx eslint --stdin 

--stdin-filename

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

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

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

--stdin-filename 示例

npm

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

yarn

cat myFile.js | yarn dlx eslint --stdin --stdin-filename myfile.js 

pnpm

cat myFile.js | pnpm dlx eslint --stdin --stdin-filename myfile.js 

bun

cat myFile.js | bunx eslint --stdin --stdin-filename myfile.js 

处理警告

--quiet

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

  • 参数类型:无参数。
--quiet 示例

npm

npx eslint --quiet file.js 

yarn

yarn dlx eslint --quiet file.js 

pnpm

pnpm dlx eslint --quiet file.js 

bun

bunx eslint --quiet file.js 

--max-warnings

此选项允许您指定警告阈值,可用于强制 ESLint 在项目中的警告级别规则违规过多时以错误状态退出。

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

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

--max-warnings 示例

npm

npx eslint --max-warnings 10 file.js 

yarn

yarn dlx eslint --max-warnings 10 file.js 

pnpm

pnpm dlx eslint --max-warnings 10 file.js 

bun

bunx eslint --max-warnings 10 file.js 

输出

-o, --output-file

将代码检查结果的输出写入指定的 文件。

  • 参数类型:字符串。文件路径。
  • 多个参数:否
-o, --output-file 示例

npm

npx eslint -o ./test/test.html 

yarn

yarn dlx eslint -o ./test/test.html 

pnpm

pnpm dlx eslint -o ./test/test.html 

bun

bunx eslint -o ./test/test.html 

-f, --format

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

如果您正在使用在本地文件中定义的自定义格式化程序,则可以指定自定义格式化程序文件的路径。

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

当指定时,给定的格式将输出到控制台。如果您想将该输出保存到文件中,可以在命令行上这样做

npm

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

yarn

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

pnpm

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

bun

# Saves the output into the `results.json` file.
bunx eslint -f json file.js > results.json 
-f, --format 示例

使用内置的 json 格式化程序

npm

npx eslint --format json file.js 

yarn

yarn dlx eslint --format json file.js 

pnpm

pnpm dlx eslint --format json file.js 

bun

bunx eslint --format json file.js 

使用本地自定义格式化程序

npm

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

yarn

yarn dlx eslint -f ./customformat.js file.js 

pnpm

pnpm dlx eslint -f ./customformat.js file.js 

bun

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

使用 npm 安装的格式化程序

npm

npm install eslint-formatter-pretty

yarn

yarn add eslint-formatter-pretty

pnpm

pnpm add eslint-formatter-pretty

bun

bun add eslint-formatter-pretty

然后运行以下命令之一

npm

npx eslint -f pretty file.js 

yarn

yarn dlx eslint -f pretty file.js 

pnpm

pnpm dlx eslint -f pretty file.js 

bun

bunx eslint -f pretty file.js 

或者,也可以

npm

npx eslint -f eslint-formatter-pretty file.js 

yarn

yarn dlx eslint -f eslint-formatter-pretty file.js 

pnpm

pnpm dlx eslint -f eslint-formatter-pretty file.js 

bun

bunx eslint -f eslint-formatter-pretty file.js 

--color--no-color

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

  • 参数类型:无参数。

您可以使用这些选项来覆盖默认行为,默认行为是在未检测到 TTY 时禁用彩色输出,例如通过 catless 管道传输 eslint 时。

--color--no-color 示例

npm

npx eslint --color file.js | cat 

yarn

yarn dlx eslint --color file.js | cat 

pnpm

pnpm dlx eslint --color file.js | cat 

bun

bunx eslint --color file.js | cat 

npm

npx eslint --no-color file.js 

yarn

yarn dlx eslint --no-color file.js 

pnpm

pnpm dlx eslint --no-color file.js 

bun

bunx eslint --no-color file.js 

内联配置注释

--no-inline-config

此选项可防止内联注释(如 /*eslint-disable*//*global foo*/)产生任何效果。

  • 参数类型:无参数。

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

  • /*eslint-disable*/
  • /*eslint-enable*/
  • /*global*/
  • /*eslint*/
  • // eslint-disable-line
  • // eslint-disable-next-line
--no-inline-config 示例

npm

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

yarn

yarn dlx eslint --no-inline-config file.js 

pnpm

pnpm dlx eslint --no-inline-config file.js 

bun

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

--report-unused-disable-directives

此选项导致 ESLint 报告指令注释(如 // eslint-disable-line),即使该行本来不会报告任何错误。

  • 参数类型:无参数。

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

--report-unused-disable-directives 示例

npm

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

yarn

yarn dlx eslint --report-unused-disable-directives file.js 

pnpm

pnpm dlx eslint --report-unused-disable-directives file.js 

bun

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

--report-unused-disable-directives-severity

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

  • 参数类型:字符串。以下值之一
    1. off (或 0)
    2. warn (或 1)
    3. error (或 2)
  • 多个参数:否
  • 默认值:默认情况下,使用 linterOptions.reportUnusedDisableDirectives 配置设置(默认值为 "warn")。
--report-unused-disable-directives-severity 示例

npm

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

yarn

yarn dlx eslint --report-unused-disable-directives-severity warn file.js 

pnpm

pnpm dlx eslint --report-unused-disable-directives-severity warn file.js 

bun

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

--report-unused-inline-configs

此选项导致 ESLint 报告内联配置注释(如 /* eslint rule-name: "error" */),如果规则的严重程度和任何选项与已经配置的匹配。

  • 参数类型:字符串。以下值之一
    1. off (或 0)
    2. warn (或 1)
    3. error (或 2)
  • 多个参数:否
  • 默认值:默认情况下,使用 linterOptions.reportUnusedInlineConfigs 配置设置(默认值为 "off")。

这对于保持文件整洁,避免误导性杂乱很有用。内联配置注释旨在以某种方式更改 ESLint 的行为:如果它们没有更改任何内容,则没有理由将它们保留在文件中。

--report-unused-inline-configs 示例
npx eslint --report-unused-inline-configs error file.js

缓存

--cache

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

  • 参数类型:无参数。

如果您使用 --cache 运行 ESLint,然后不使用 --cache 运行 ESLint,则 .eslintcache 文件将被删除。这是必要的,因为 lint 的结果可能会发生变化,从而使 .eslintcache 无效。如果您想控制何时删除缓存文件,请使用 --cache-location 指定缓存文件的替代位置。

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

--cache 示例

npm

npx eslint --cache file.js 

yarn

yarn dlx eslint --cache file.js 

pnpm

pnpm dlx eslint --cache file.js 

bun

bunx eslint --cache file.js 

--cache-file

已弃用:请使用 --cache-location 代替。

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

--cache-location

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

  • 参数类型:字符串。文件或目录的路径。如果指定了目录,则会在指定文件夹内创建一个缓存文件。文件的名称基于当前工作目录的哈希值,例如:.cache_hashOfCWD
  • 多个参数:否
  • 默认值:如果未指定位置,则使用 .eslintcache。该文件是在执行 eslint 命令的目录中创建的。

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

--cache-location 示例

npm

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

yarn

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

pnpm

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

bun

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

--cache-strategy

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

  • 参数类型:字符串。以下值之一
    1. metadata
    2. content
  • 多个参数:否
  • 默认值metadata

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

--cache-strategy 示例

npm

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

yarn

yarn dlx eslint "src/**/*.js" --cache --cache-strategy content 

pnpm

pnpm dlx eslint "src/**/*.js" --cache --cache-strategy content 

bun

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

抑制违规

--suppress-all

抑制现有违规行为,以便在后续运行中不会报告它们。它允许您启用一个或多个 lint 规则,并在出现新的违规行为时收到通知。抑制信息默认存储在 eslint-suppressions.json 中,除非由 --suppressions-location 指定其他位置。该文件会使用新的抑制信息进行更新。

  • 参数类型:无参数。
--suppress-all 示例

npm

npx eslint "src/**/*.js" --suppress-all 

yarn

yarn dlx eslint "src/**/*.js" --suppress-all 

pnpm

pnpm dlx eslint "src/**/*.js" --suppress-all 

bun

bunx eslint "src/**/*.js" --suppress-all 

--suppress-rule

抑制特定规则的违规行为,以便在后续运行中不会报告它们。与 --suppress-all 类似,抑制信息默认存储在 eslint-suppressions.json 中,除非由 --suppressions-location 指定其他位置。该文件会使用新的抑制信息进行更新。

  • 参数类型:字符串。规则 ID。
  • 多个参数:是
--suppress-rule 示例

npm

npx eslint "src/**/*.js" --suppress-rule no-console --suppress-rule indent 

yarn

yarn dlx eslint "src/**/*.js" --suppress-rule no-console --suppress-rule indent 

pnpm

pnpm dlx eslint "src/**/*.js" --suppress-rule no-console --suppress-rule indent 

bun

bunx eslint "src/**/*.js" --suppress-rule no-console --suppress-rule indent 

--suppressions-location

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

  • 参数类型:字符串。文件路径。如果指定了目录,则会在指定文件夹内创建一个缓存文件。文件的名称基于当前工作目录的哈希值,例如:suppressions_hashOfCWD
  • 多个参数:否
  • 默认值:如果未指定位置,则使用 eslint-suppressions.json。该文件是在执行 eslint 命令的目录中创建的。
--suppressions-location 示例

npm

npx eslint "src/**/*.js" --suppressions-location ".eslint-suppressions-example.json" 

yarn

yarn dlx eslint "src/**/*.js" --suppressions-location ".eslint-suppressions-example.json" 

pnpm

pnpm dlx eslint "src/**/*.js" --suppressions-location ".eslint-suppressions-example.json" 

bun

bunx eslint "src/**/*.js" --suppressions-location ".eslint-suppressions-example.json" 

--prune-suppressions

修剪抑制文件中的未使用抑制信息。当您解决一个或多个被抑制的违规行为时,此选项很有用。

  • 参数类型:无参数。
--prune-suppressions 示例

npm

npx eslint "src/**/*.js" --prune-suppressions 

yarn

yarn dlx eslint "src/**/*.js" --prune-suppressions 

pnpm

pnpm dlx eslint "src/**/*.js" --prune-suppressions 

bun

bunx eslint "src/**/*.js" --prune-suppressions 

--pass-on-unpruned-suppressions

忽略未使用的抑制信息。默认情况下,如果抑制文件中存在未使用的抑制信息,ESLint 会以退出代码 2 退出并显示错误消息。当您使用此标志时,未使用的抑制信息不会影响退出代码,并且 ESLint 不会输出有关未使用的抑制信息的错误。

  • 参数类型:无参数。
--pass-on-unpruned-suppressions 示例

npm

npx eslint "src/**/*.js" --pass-on-unpruned-suppressions 

yarn

yarn dlx eslint "src/**/*.js" --pass-on-unpruned-suppressions 

pnpm

pnpm dlx eslint "src/**/*.js" --pass-on-unpruned-suppressions 

bun

bunx eslint "src/**/*.js" --pass-on-unpruned-suppressions 

杂项

--init

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

  • 参数类型:无参数。

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

--init 示例

npm

npx eslint --init 

yarn

yarn dlx eslint --init 

pnpm

pnpm dlx eslint --init 

bun

bunx eslint --init 

--env-info

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

  • 参数类型:无参数。

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

--env-info 示例

npm

npx eslint --env-info 

yarn

yarn dlx eslint --env-info 

pnpm

pnpm dlx eslint --env-info 

bun

bunx eslint --env-info 

--no-error-on-unmatched-pattern

此选项可防止未匹配的带引号的 glob 模式导致错误。这并不能防止 shell 无法匹配 glob 模式时出现的错误。

  • 参数类型:无参数。
--no-error-on-unmatched-pattern 示例

npm

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

yarn

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

pnpm

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

bun

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

--exit-on-fatal-error

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

  • 参数类型:无参数。
--exit-on-fatal-error 示例

npm

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

yarn

yarn dlx eslint --exit-on-fatal-error file.js 

pnpm

pnpm dlx eslint --exit-on-fatal-error file.js 

bun

bunx 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。

  • 参数类型:无参数。
--no-warn-ignored 示例

npm

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

yarn

yarn dlx eslint --no-warn-ignored --max-warnings 0 ignored-file.js 

pnpm

pnpm dlx eslint --no-warn-ignored --max-warnings 0 ignored-file.js 

bun

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

--pass-on-no-patterns

此选项允许 ESLint 以代码 0 退出,当没有传递文件或目录模式时。如果没有此选项,ESLint 会假定您想要使用 . 作为模式。

  • 参数类型:无参数。
--pass-on-no-patterns 示例

npm

npx eslint --pass-on-no-patterns 

yarn

yarn dlx eslint --pass-on-no-patterns 

pnpm

pnpm dlx eslint --pass-on-no-patterns 

bun

bunx eslint --pass-on-no-patterns 

--debug

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

  • 参数类型:无参数。

如果您遇到问题并且难以确定问题所在,此信息很有用。ESLint 团队可能会要求此调试信息以帮助解决错误。

--debug 示例

npm

npx eslint --debug test.js 

yarn

yarn dlx eslint --debug test.js 

pnpm

pnpm dlx eslint --debug test.js 

bun

bunx eslint --debug test.js 

-h, --help

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

  • 参数类型:无参数。
-h, --help 示例

npm

npx eslint --help 

yarn

yarn dlx eslint --help 

pnpm

pnpm dlx eslint --help 

bun

bunx eslint --help 

-v, --version

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

  • 参数类型:无参数。
-v, --version 示例

npm

npx eslint --version 

yarn

yarn dlx eslint --version 

pnpm

pnpm dlx eslint --version 

bun

bunx eslint --version 

--print-config

此选项输出要用于传递文件的配置。当存在此选项时,不执行 linting,并且仅有效的与配置相关的选项。

  • 参数类型:字符串。文件路径。
  • 多个参数:否
--print-config 示例

npm

npx eslint --print-config file.js 

yarn

yarn dlx eslint --print-config file.js 

pnpm

pnpm dlx eslint --print-config file.js 

bun

bunx eslint --print-config file.js 

--stats

此选项添加一系列详细的性能统计信息(请参阅 Stats type),例如 parse-、fix- 和 lint-时间(每个规则的时间)到传递给格式化程序(请参阅 Stats CLI usage)的 result 对象。

  • 参数类型:无参数。

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

--stats 示例

npm

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

yarn

yarn dlx eslint --stats --format json file.js 

pnpm

pnpm dlx eslint --stats --format json file.js 

bun

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

--flag

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

  • 参数类型:字符串。一个功能标识符。
  • 多个参数:是
--flag 示例

npm

npx eslint --flag x_feature file.js 

yarn

yarn dlx eslint --flag x_feature file.js 

pnpm

pnpm dlx eslint --flag x_feature file.js 

bun

bunx eslint --flag x_feature file.js 

--mcp

此选项启动 ESLint MCP 服务器,供 AI 代理使用。

  • 参数类型:无参数。
  • 多个参数:否
--mcp 示例

npm

npx eslint --mcp 

yarn

yarn dlx eslint --mcp 

pnpm

pnpm dlx eslint --mcp 

bun

bunx eslint --mcp 

--concurrency

此选项控制用于 linting 文件的 worker 线程的数量。

  • 参数类型:Int|String。一个正整数、autooff
  • 多个参数:否
  • 默认值off

off 会导致所有文件在主线程中进行 linting。值 auto 会尝试自动确定最佳设置。

--concurrency 示例

npm

npx eslint --concurrency auto 

yarn

yarn dlx eslint --concurrency auto 

pnpm

pnpm dlx eslint --concurrency auto 

bun

bunx eslint --concurrency auto 

退出代码

当 linting 文件时,ESLint 会以以下退出代码之一退出

  • 0:Linting 成功且没有 linting 错误。如果设置了 --max-warnings 标志为 n,则 linting 警告的数量最多为 n
  • 1:Linting 成功但至少存在一个 linting 错误,或者 linting 警告的数量超过了 --max-warnings 选项允许的数量。
  • 2:由于配置问题或内部错误导致 linting 失败。
更改语言