在 VS Code 存檔時讓 ESLint 自動修正排版,需要三樣東西:專案裡的 ESLint 與設定檔、VS Code 的 ESLint 擴充套件,以及 settings.json 的存檔設定。下面先給可以直接複製的完整設定,再逐步說明。ESLint 本身是什麼、規則與嚴重度怎麼設定,可前往〈ESLint 是什麼?JavaScript 程式碼檢查工具的用途、規則與設定檔入門〉觀看,本文只講 VS Code 自動排版的設定。
完整設定(可直接複製)
以下以 ESLint v10 搭配 @stylistic/eslint-plugin 為例,存檔時會統一縮排、引號、分號與空白。
- 在專案目錄安裝套件:
npm install --save-dev eslint @eslint/js globals @stylistic/eslint-plugin
- 在專案根目錄建立
eslint.config.mjs:
import { defineConfig } from "eslint/config";
import js from "@eslint/js";
import globals from "globals";
import stylistic from "@stylistic/eslint-plugin";
export default defineConfig([
{
files: ["**/*.{js,mjs,cjs,jsx}"],
plugins: { js },
extends: ["js/recommended"],
languageOptions: { globals: globals.browser },
},
stylistic.configs.customize({
indent: 2,
quotes: "single",
semi: true,
}),
]);
- 在 VS Code 安裝 ESLint 擴充套件(識別碼
dbaeumer.vscode-eslint)。 - 在 VS Code 的
settings.json加入:
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}
為什麼舊教學的 .eslintrc 設定沒有作用
本文 2023 年的版本教的是建立 .eslintrc 並套用 eslint:recommended,現在照做會遇到兩個問題:
- 設定檔讀不到:ESLint v9.0.0 起預設設定檔改為
eslint.config.*(flat config),官方 v10 遷移文件寫明 v10.0.0 起不再支援舊的.eslintrc格式。現在執行npm install eslint --save-dev會裝到 v10,專案裡只有.eslintrc時,執行npx eslint會出現下面的錯誤:
ESLint couldn't find an eslint.config.* file.
- 推薦規則不管排版:ESLint 從 v8.53.0 起棄用核心的排版規則(空白、分號、引號這類只管外觀的規則),推薦規則組只檢查可能的錯誤。只套推薦規則時,縮排再亂也不會被修正,存檔看起來就像「沒有反應」。
新舊設定格式的差異與官方遷移工具,〈ESLint 是什麼〉的「.eslintrc 與 eslint.config.js」段落有說明。
步驟一:安裝 ESLint 與排版規則套件
ESLint 官方部落格建議排版交給格式化工具(Formatter),或改用社群維護的 @stylistic/eslint-plugin。想讓 ESLint 存檔時直接排版,用後者最直接:
npm install --save-dev eslint @eslint/js globals @stylistic/eslint-plugin
eslint:ESLint 本體。@eslint/js:ESLint 官方的推薦規則組(設定檔中的js/recommended)。globals:瀏覽器、Node.js 的全域變數清單,避免console、window被當成未定義的變數。@stylistic/eslint-plugin:從 ESLint 核心移出、改由社群維護的排版規則。
ESLint 官方文件列出的 Node.js 版本需求是 ^20.19.0、^22.13.0 或 >=24,而且專案要先有 package.json(沒有的話先執行 npm init)。
步驟二:建立 eslint.config.mjs
在專案根目錄建立 eslint.config.mjs:
import { defineConfig } from "eslint/config";
import js from "@eslint/js";
import globals from "globals";
import stylistic from "@stylistic/eslint-plugin";
export default defineConfig([
{
files: ["**/*.{js,mjs,cjs,jsx}"],
plugins: { js },
extends: ["js/recommended"],
languageOptions: { globals: globals.browser },
},
stylistic.configs.customize({
indent: 2,
quotes: "single",
semi: true,
}),
]);
files:要檢查的檔案類型。extends: ["js/recommended"]:開啟官方推薦規則,負責找出未使用的變數這類可能的錯誤。stylistic.configs.customize():排版規則。indent是縮排空白數,quotes是引號("single"或"double"),semi是否加分號,依團隊習慣調整。
檔名用 .mjs,是因為設定檔使用 import 語法;如果 package.json 已設定 "type": "module",也可以命名為 eslint.config.js。
先在終端機確認設定有效,例如有一個排版混亂的 app.js:
const name="Sean"
function hello( who ){
console.log("hi, "+who)
}
hello(name)
執行:
npx eslint --fix app.js
實測(ESLint 10.15.0、@stylistic/eslint-plugin 6.0.1)修正後的結果:
const name = 'Sean';
function hello(who) {
console.log('hi, ' + who);
}
hello(name);
步驟三:安裝 VS Code ESLint 擴充套件
在 VS Code 側邊欄點選擴充功能圖示,搜尋「ESLint」,安裝 Microsoft 發布的 ESLint 擴充套件(識別碼 dbaeumer.vscode-eslint)。擴充套件使用的是專案裡安裝的 ESLint,所以步驟一的安裝不能省略。
步驟四:設定存檔時自動修正
按 Ctrl + Shift + P(macOS 為 Cmd + Shift + P)開啟命令選擇區,執行 Preferences: Open User Settings (JSON),在 settings.json 加入:
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}
"explicit" 表示手動存檔(Ctrl + S)時執行。VS Code 官方文件另外列出 "always"(自動儲存時也執行)與 "never"(不執行)。舊教學寫成 true,現行的 VS Code 文件與 ESLint 擴充套件說明都改用 "explicit"。
完成後打開專案裡的 .js 檔,按 Ctrl + S 存檔,就會依 eslint.config.mjs 的規則自動修正排版。
已經在用 Prettier:排版交給 Prettier,ESLint 只檢查錯誤
如果團隊已經使用 Prettier,就不用再裝 @stylistic,改成以下設定:
- 安裝套件:
npm install --save-dev eslint @eslint/js globals prettier eslint-config-prettier
eslint.config.mjs在最後加上eslint-config-prettier,關掉會和 Prettier 衝突的規則(必須放在其他設定之後):
import { defineConfig } from "eslint/config";
import js from "@eslint/js";
import globals from "globals";
import eslintConfigPrettier from "eslint-config-prettier/flat";
export default defineConfig([
{
files: ["**/*.{js,mjs,cjs,jsx}"],
plugins: { js },
extends: ["js/recommended"],
languageOptions: { globals: globals.browser },
},
eslintConfigPrettier,
]);
- 在 VS Code 安裝 Prettier 擴充套件(識別碼
esbenp.prettier-vscode),settings.json改成:
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}
存檔時由 Prettier 排版,ESLint 修正能自動修的問題。同一段 app.js 實測(Prettier 3.10.1)執行 npx prettier --write app.js 後:
const name = "Sean";
function hello(who) {
console.log("hi, " + who);
}
hello(name);
接著執行 npx eslint app.js 沒有回報任何問題。Prettier 預設使用雙引號,要改成單引號等設定,可在專案加上 .prettierrc 設定檔。
實務踩坑與常見排錯
1. 存檔沒有自動排版
依序檢查:
- 專案根目錄有沒有
eslint.config.*:只有.eslintrc時,ESLint v10 會直接出現上面「couldn't find an eslint.config.* file」的錯誤。 - 終端機執行
npx eslint 檔名有沒有回報:終端機抓得到問題、VS Code 存檔卻沒反應,再檢查settings.json的editor.codeActionsOnSave。 - 設定檔是不是只開了推薦規則:推薦規則不含排版,要加上步驟二的
@stylistic設定,或改用 Prettier。
2. ESLint 與 Prettier 規則衝突
同時使用兩者時,ESLint 的排版規則和 Prettier 的輸出不一致,存檔後就會一直出現紅色底線,或兩邊修出不同的結果。解法是 eslint-config-prettier:它會關掉與 Prettier 重疊的規則(也包含 @stylistic 的規則),而且要放在設定陣列的最後。只設定 editor.defaultFormatter 並不能解決衝突,它只決定由哪個擴充套件格式化,不會關掉 ESLint 的排版規則。
結語
排版交給工具,在存檔時自動統一,團隊就不必在程式碼審查時討論縮排與引號,能把時間留給程式邏輯本身。
參考資料與延伸閱讀
- ESLint 官方文件:Getting Started with ESLint
- ESLint 官方文件:Migrate to v10.x
- ESLint 官方部落格:Deprecation of formatting rules
- ESLint Stylistic 官方文件:Shared Configurations
- VS Code 官方文件:Code Actions on save
- VS Code Marketplace:ESLint 擴充套件
- eslint-config-prettier(GitHub)
- VS Code Marketplace:Prettier – Code formatter
- 維基百科:Lint 靜態分析工具與歷史演進
- 延伸閱讀:ESLint 是什麼?JavaScript 程式碼檢查工具的用途、規則與設定檔入門
- 延伸閱讀:React Console Warning:Support for defaultProps will be removed from function components 原因與解決方法
- 延伸閱讀:什麼是 JavaScript IDE?5 款主流前端開發工具推薦與比較
發佈留言