命令列介面參考
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.config.js
),您也可以省略檔案引數,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
如果您未使用扁平式設定檔,則在沒有檔案引數的情況下執行 ESLint 會導致錯誤。
注意: 您也可以使用替代套件管理器,例如 Yarn 或 pnpm 來執行 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
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
基本設定
--no-eslintrc
eslintrc 模式專用。 停用從 .eslintrc.*
和 package.json
檔案使用設定。對於扁平式設定模式,請改用 --no-config-lookup
。
- 引數類型:無引數。
--no-eslintrc
範例
npm
npx eslint --no-eslintrc file.js
yarn
yarn dlx eslint --no-eslintrc file.js
pnpm
pnpm dlx eslint --no-eslintrc file.js
bun
bunx eslint --no-eslintrc 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
--env
eslintrc 模式專用。 此選項啟用特定環境。
- 引數類型:字串。其中一個可用的環境。
- 多個引數:是
每個環境定義的全域變數的詳細資訊可在指定環境文件中找到。此選項僅啟用環境。它不會停用在其他設定檔中設定的環境。若要指定多個環境,請使用逗號分隔它們,或多次使用該選項。
--env
範例
npm
npx eslint --env browser,node file.js
yarn
yarn dlx eslint --env browser,node file.js
pnpm
pnpm dlx eslint --env browser,node file.js
bun
bunx eslint --env browser,node file.js
npm
npx eslint --env browser --env node file.js
yarn
yarn dlx eslint --env browser --env node file.js
pnpm
pnpm dlx eslint --env browser --env node file.js
bun
bunx eslint --env browser --env node file.js
--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
# succeds, yay!
echo '3 ** 4' | npx eslint --stdin --parser-options ecmaVersion:7
yarn
# succeds, yay!
echo '3 ** 4' | yarn dlx eslint --stdin --parser-options ecmaVersion:7
pnpm
# succeds, yay!
echo '3 ** 4' | pnpm dlx eslint --stdin --parser-options ecmaVersion:7
bun
# succeds, yay!
echo '3 ** 4' | bunx eslint --stdin --parser-options ecmaVersion:7
--resolve-plugins-relative-to
eslintrc 模式專用。 變更從中解析外掛程式的目錄。
- 引數類型:字串。目錄路徑。
- 多個引數:否
- 預設值:預設情況下,外掛程式會從找到設定檔的目錄中解析。
當外掛程式是由最終使用者以外的人安裝時,應使用此選項。它應設定為依賴必要外掛程式的專案的專案目錄。
例如
- 當使用位於目前專案外部的設定檔(使用
--config
標誌)時,如果設定使用外掛程式,而這些外掛程式是本機安裝到自身的,則應將--resolve-plugins-relative-to
設定為包含設定檔的目錄。 - 如果整合依賴 ESLint 和一組外掛程式,並且該工具代表使用者使用預設設定來調用 ESLint,則該工具應將
--resolve-plugins-relative-to
設定為該工具的頂層目錄。
--resolve-plugins-relative-to
範例
npm
npx eslint --config ~/personal-eslintrc.js --resolve-plugins-relative-to /usr/local/lib/
yarn
yarn dlx eslint --config ~/personal-eslintrc.js --resolve-plugins-relative-to /usr/local/lib/
pnpm
pnpm dlx eslint --config ~/personal-eslintrc.js --resolve-plugins-relative-to /usr/local/lib/
bun
bunx eslint --config ~/personal-eslintrc.js --resolve-plugins-relative-to /usr/local/lib/
指定規則和外掛程式
--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。
若要忽略 .eslintrc
設定檔中的規則,並且僅執行命令列中指定的規則,請將 --rule
標誌與 --no-eslintrc
標誌結合使用。
--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-eslintrc
yarn
# Only apply rule from the command line
yarn dlx eslint --rule 'quotes: [error, double]' --no-eslintrc
pnpm
# Only apply rule from the command line
pnpm dlx eslint --rule 'quotes: [error, double]' --no-eslintrc
bun
# Only apply rule from the command line
bunx eslint --rule 'quotes: [error, double]' --no-eslintrc
--rulesdir
已棄用:請改用外掛程式中的規則。
eslintrc 模式專用。 此選項可讓您指定另一個目錄,從該目錄載入規則檔案。這可讓您在執行階段動態載入新規則。當您有不適合與 ESLint 捆綁在一起的自訂規則時,這非常有用。
- 引數類型:字串。目錄路徑。您的自訂規則目錄中的規則必須遵循與捆綁規則相同的格式才能正常運作。
- 多個引數:是
請注意,與核心規則和外掛程式規則一樣,您仍然需要在設定中或透過 --rule
CLI 選項啟用規則,才能在程式碼檢查期間實際執行這些規則。使用 --rulesdir
指定規則目錄不會自動啟用該目錄中的規則。
--rulesdir
範例
npm
npx eslint --rulesdir my-rules/ file.js
yarn
yarn dlx eslint --rulesdir my-rules/ file.js
pnpm
pnpm dlx eslint --rulesdir my-rules/ file.js
bun
bunx eslint --rulesdir my-rules/ file.js
npm
npx eslint --rulesdir my-rules/ --rulesdir my-other-rules/ file.js
yarn
yarn dlx eslint --rulesdir my-rules/ --rulesdir my-other-rules/ file.js
pnpm
pnpm dlx eslint --rulesdir my-rules/ --rulesdir my-other-rules/ file.js
bun
bunx eslint --rulesdir my-rules/ --rulesdir my-other-rules/ file.js
修正問題
--fix
此選項指示 ESLint 嘗試修正盡可能多的問題。修正會套用至實際檔案本身,並且僅輸出剩餘的未修正問題。
- 引數類型:無引數。
並非所有問題都可以使用此選項修正,而且此選項在以下情況下不起作用
- 當程式碼透過管道傳送到 ESLint 時,此選項會擲回錯誤。
- 除非處理器選擇允許自動修正,否則此選項對使用處理器的程式碼沒有任何影響。
如果您想修正來自 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
時指定要套用的修正類型。
- 引數類型:字串。以下修正類型之一
problem
- 修正程式碼中潛在的錯誤suggestion
- 將修正套用至可改善程式碼的程式碼layout
- 套用不會變更程式結構 (AST) 的修正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 .
忽略檔案
--ignore-path
eslintrc 模式專用。 此選項可讓您指定要用作 .eslintignore
的檔案。
- 引數類型:字串。檔案路徑。
- 多個引數:否
- 預設值:預設情況下,ESLint 會在目前工作目錄中尋找
.eslintignore
。
注意: 僅在使用已棄用的設定時才支援 --ignore-path
。如果您想在 eslint.config.js
檔案中包含來自 .gitignore
檔案的模式,請參閱包含 .gitignore
檔案。
--ignore-path
範例
npm
npx eslint --ignore-path tmp/.eslintignore file.js
yarn
yarn dlx eslint --ignore-path tmp/.eslintignore file.js
pnpm
pnpm dlx eslint --ignore-path tmp/.eslintignore file.js
bun
bunx eslint --ignore-path tmp/.eslintignore file.js
npm
npx eslint --ignore-path .gitignore file.js
yarn
yarn dlx eslint --ignore-path .gitignore file.js
pnpm
pnpm dlx eslint --ignore-path .gitignore file.js
bun
bunx eslint --ignore-path .gitignore file.js
--no-ignore
停用從 .eslintignore
檔案、--ignore-path
標誌、--ignore-pattern
標誌和設定檔中的 ignorePatterns
屬性排除檔案。
- 引數類型:無引數。
--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
此選項可讓您指定要忽略的檔案模式。在 eslintrc 模式下,這些是 .eslintignore
的補充。
- 引數類型:字串。支援的語法與
.eslintignore
檔案的語法相同,後者使用與.gitignore
規格相同的模式。您應該引用您的模式,以避免 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
此選項指定主控台的輸出格式。
如果您使用的是在本機檔案中定義的自訂格式器,則可以指定自訂格式器檔案的路徑。
npm 安裝的格式器會解析,無論是否有 eslint-formatter-
字首。
指定後,給定的格式會輸出到主控台。如果您想將該輸出儲存到檔案中,您可以像這樣在命令列上執行此操作
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,例如當透過 cat
或 less
管道傳輸 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-env*/
// 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-disable
和 eslint-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
相同,但可讓您指定報告錯誤的嚴重性層級(error
、warn
、off
)。這兩個選項一次只能使用一個。
- 引數類型:字串。以下值之一
off
(或0
)warn
(或1
)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" */
,這些註解的規則嚴重性和任何選項與已設定的內容相符。
- 引數類型:字串。以下值之一
off
(或0
)warn
(或1
)error
(或2
)
- 多個引數:否
- 預設值:預設情況下,使用
linterOptions.reportUnusedInlineConfigs
設定選項(預設為"off"
)。
這對於保持檔案乾淨且沒有誤導性的雜亂非常有用。內聯設定註解旨在以某種方式變更 ESLint 的行為:如果它們沒有變更任何內容,則沒有理由將它們保留在其中。
--report-unused-inline-configs
範例
npx eslint --report-unused-inline-configs error file.js
快取
--cache
儲存有關已處理檔案的資訊,以便僅對已變更的檔案進行操作。啟用此選項可以顯著提高 ESLint 的執行時間效能,方法是確保僅檢查已變更的檔案。快取預設儲存在 .eslintcache
中。
- 引數類型:無引數。
如果您使用 --cache
執行 ESLint,然後在不使用 --cache
的情況下執行 ESLint,則 .eslintcache
檔案將被刪除。這是必要的,因為程式碼檢查的結果可能會變更,並使 .eslintcache
無效。如果您想控制何時刪除快取檔案,請使用 --cache-location
來指定快取檔案的替代位置。
自動修正的檔案不會放入快取中。後續不會觸發自動修正的程式碼檢查會將其放入快取中。
--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
快取用於偵測已變更檔案的策略。
- 引數類型:字串。以下值之一
metadata
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
其他
--init
此選項執行 npm init @eslint/config
以啟動設定初始化精靈。它旨在透過回答幾個問題來協助新使用者快速建立 .eslintrc
檔案。當您使用此標誌時,CLI 不會執行程式碼檢查。
- 引數類型:無引數。
產生的設定檔會在目前目錄中建立。
--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 不會執行程式碼檢查。
--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 模式或 --ext
不符時,此選項可防止錯誤。當您的 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 default
和 File 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 會假設您想使用 .
作為模式。(在舊版 eslintrc 模式下執行時,ESLint 將以代碼 1 退出。)
- 引數類型:無引數。
--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 不會執行程式碼檢查。
- 引數類型:無引數。
-h
, --help
範例
npm
npx eslint --help
yarn
yarn dlx eslint --help
pnpm
pnpm dlx eslint --help
bun
bunx eslint --help
-v
, --version
此選項會將目前的 ESLint 版本輸出到主控台。當此選項存在時,所有其他選項都會被忽略。當您使用此標誌時,CLI 不會執行程式碼檢查。
- 引數類型:無引數。
-v
, --version
範例
npm
npx eslint --version
yarn
yarn dlx eslint --version
pnpm
pnpm dlx eslint --version
bun
bunx eslint --version
--print-config
此選項會輸出要用於傳遞檔案的設定。存在時,不會執行程式碼檢查,並且只有與設定相關的選項有效。當您使用此標誌時,CLI 不會執行程式碼檢查。
- 引數類型:字串。檔案路徑。
- 多個引數:否
--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
此選項會將一系列詳細的效能統計資訊(請參閱統計資訊類型),例如剖析、修正和檢查時間(每個規則的時間)新增到傳遞至格式器的 result
物件(請參閱統計資訊 CLI 用法)。
- 引數類型:無引數。
此選項適用於使用顯示統計資訊的自訂格式器。它也可以與內建的 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
退出代碼
在檢查檔案時,ESLint 會以以下退出代碼之一退出
0
:程式碼檢查成功,並且沒有程式碼檢查錯誤。如果--max-warnings
標誌設定為n
,則程式碼檢查警告的數量最多為n
。1
:程式碼檢查成功,並且至少有一個程式碼檢查錯誤,或者程式碼檢查警告的數量超過--max-warnings
選項允許的數量。2
:由於設定問題或內部錯誤,程式碼檢查失敗。