npx eslint --init生成的配置常失效,因其仅覆盖基础js环境,未自动安装typescript/vue等解析器和插件,导致.ts/.vue文件被跳过;必须手动补全parser、plugins、extends并确保本地依赖正确安装。

直接用 npx eslint --init 就能生成配置文件,但默认不支持 TypeScript 或 Vue 等框架的深度校验——必须手动补全解析器和插件,否则 ESLint 会跳过 .ts 或 .vue 文件。
为什么 npx eslint --init 生成的配置常失效
交互式命令生成的 .eslintrc.js 或 .eslintrc.cjs 通常只覆盖基础 JS 环境,比如选了 “JavaScript modules” 和 “browser”,它不会自动加 @typescript-eslint/parser,也不会装 eslint-plugin-vue。结果就是:你在 index.ts 里写 any 类型,ESLint 完全没反应。
- 选了 TypeScript 却没装
@typescript-eslint/parser→ 报错Cannot find module '@typescript-eslint/parser' - 选了 React 但没加
eslint-plugin-react→jsx-a11y规则不生效 - 生成的是
.eslintrc.json,但项目用了 ESM(package.json里有"type": "module")→ 必须改用.eslintrc.cjs或eslint.config.js
npx eslint --init 后必须手动补的三件事
命令跑完只是起点。以下操作缺一不可,否则 VSCode 里看不到波浪线、保存也不修复:
- 检查
node_modules是否真装了对应插件:比如 Vue 项目要确认有eslint-plugin-vue和@vue/eslint-config-prettier - 打开生成的配置文件,在
parser字段明确写'@typescript-eslint/parser'(不是路径,不是相对引用) - 在
extends数组末尾追加'plugin:vue/vue3-recommended'(Vue 3)或'plugin:react/recommended'(React),并确保plugins数组包含对应字符串
VSCode 不识别新配置?先看输出通道
改完配置后没反应,别急着重装插件。打开命令面板(Ctrl+Shift+P),运行 ESLint: Show Output Channel,重点看两行:
-
Using configuration from /path/to/your/.eslintrc.cjs—— 如果路径不对,说明 VSCode 加载了父目录或全局配置 -
Failed to load plugin 'vue'—— 表示插件没装对,或plugins里拼错了名字(比如写了'vue'却漏了eslint-plugin-前缀)
此时该删 node_modules 重装,而不是改 settings.json。
想跳过交互式命令?用脚本一键生成
如果你反复配同类项目(比如全是 TS + React),不如写个最小化初始化脚本,避免每次选错选项:
npm install eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react eslint-plugin-react-hooks --save-dev npx eslint --init --config ./eslint-init-template.js
其中 eslint-init-template.js 是你预存的配置模板,内容导出一个对象,含 parser、plugins、extends 全字段。这样生成的配置从第一行就对齐真实需求,不用再人工修。
最常被忽略的是:ESLint 插件只读取项目本地的 eslint 包,哪怕你全局装了 eslint,只要 node_modules/.bin/eslint 不存在,VSCode 就会静默失败——连报错都不给。











