eslint --init命令的各个选项分别代表什么含义

解读

国内前端面试中,ESLint 几乎必问,而“--init 每一步到底在问什么”是高频追问。
很多候选人只背过“选 Airbnb、Standard、Google 三选一”,却说不清背后到底生成了哪些文件、装了哪些依赖、对 CI 有什么影响。
面试官想借此判断:

  1. 你是否真正落地过工程化,而不是“跑完命令就完事”;
  2. 能否根据团队现状(TypeScript、React、Monorepo、Legacy)灵活定制配置;
  3. 是否理解 parser、plugin、shareable config 三者的区别与协作。
    答得越深,越能体现“代码即规范”的落地能力。

知识点

  1. 交互式命令行工具:inquirer 驱动的多步问答,最终生成 .eslintrc.* 与 package.json 片段。
  2. 模块类型(module type):ES Module / CommonJS / None,决定 parserOptions.sourceType 与 env.node。
  3. 框架类型:React、Vue、Angular、Svelte、None,决定自动安装 eslint-plugin-react、eslint-plugin-vue 等并开启对应规则集。
  4. TypeScript:若选“Yes”,会安装 @typescript-eslint/parser 与 @typescript-eslint/eslint-plugin,并设置 parser、extends、rules。
  5. 运行环境:Browser + ES2022 / Node / Worker…,决定 env.* 与 globals.*。
  6. 代码风格来源:
    a. Airbnb:eslint-config-airbnb(-base),依赖 eslint-plugin-import、react-hooks、jsx-a11y,规则最严;
    b. Standard:eslint-config-standard,无需分号、2 空格,自带 eslint-plugin-import、n、promise;
    c. Google:eslint-config-google,2 空格、强制 JSDoc;
    d. XO:接近 Standard,但带更多个人风格;
    e. 自定义:仅 extends: ["eslint:recommended"],后续自行追加。
  7. 配置格式:JavaScript / YAML / JSON,影响后续可编程性与注释能力。
  8. 实时校验:是否安装 eslint-plugin-react-refresh、eslint-plugin-vue-scoped-css 等,用于 Vite/Webpack HMR。
  9. 依赖安装方式:npm / yarn / pnpm,命令尾部自动追加 --save-dev。
  10. 生成文件位置:根目录或 packages/* 下,决定 extends 中是否使用 root: true,避免 Monorepo 向上查找父配置。

答案

eslint --init 的每一步问答,本质是在帮你拼装一段“可共享的 ESLint 配置”。
下面按国内终端真实出现的顺序,逐条拆解含义与落地差异:

  1. How would you like to use ESLint?
    · To check syntax only
    · To check syntax and find problems
    · To check syntax, find problems, and enforce code style
    国内团队 90% 选第三项,因为“风格”是 Code Review 最耗时环节;选第一项常见于老项目迁移,避免一次性爆红。

  2. What type of modules does your project use?
    · JavaScript modules (import/export)
    · CommonJS (require/exports)
    · None of these
    选 ES Module 会在 parserOptions.sourceType: "module";若项目仍用 Webpack 4 + babel-loader,可能选 CommonJS 避免动态 import 报错。

  3. Which framework does your project use?
    · React
    · Vue.js
    · Angular
    · Svelte
    · None of these
    国内 Vue 项目居多,选 Vue 后会自动安装 eslint-plugin-vue,并 extends: ["plugin:vue/vue3-essential"];React 项目则追加 eslint-plugin-react、react-hooks、jsx-a11y。

  4. Does your project use TypeScript?
    Yes / No
    选 Yes 会安装 @typescript-eslint/parser 与 @typescript-eslint/eslint-plugin,并在 overrides 里对 .ts/.tsx 单独配置;国内大厂 TypeScript 覆盖率 > 80%,此步几乎必开。

  5. Where does your code run?
    · Browser
    · Node
    可多选。Browser 会开启 env: { browser: true, es2022: true };Node 会开启 env: { node: true },并关闭 no-console 等规则。全栈项目需同时勾选,避免 window / global 混淆。

  6. How would you like to define a style for your project?
    · Use a popular style guide
    · Answer questions about your style
    · Inspect your JavaScript file(s)
    国内主流选第一项,后续出现 Airbnb、Standard、Google 三选一;若团队已有祖传 .eslintrc,会选第三项,让 ESLint 反向推断规则。

  7. Which style guide do you want to follow?
    · Airbnb: 依赖包体积最大,规则 200+,面试常问“如何关闭 react/jsx-filename-extension”;
    · Standard: 无需分号,2 空格,B 站、字节内部脚手架默认;
    · Google: 2 空格+JSDoc,适合写库;
    · XO: 个人项目常用。
    选定后,init 会把对应 config 与插件写入 devDependencies,并 extends: ["airbnb-base"] 或 ["airbnb-typescript"]。

  8. What format do you want your config file to be in?
    · JavaScript
    · YAML
    · JSON
    国内推荐 JS 格式,可写注释与动态逻辑,例如:
    const isProd = process.env.NODE_ENV === 'production';
    module.exports = { rules: { 'no-debugger': isProd ? 'error' : 'warn' } };
    JSON 格式无法注释,YAML 在 Windows 下易因缩进出错。

  9. Would you like to install them now with npm?
    Yes / No
    选 Yes 后,init 会执行 npm install -D eslint xxx...;若公司内网使用 cnpm 或 pnpm,需手动回车 No,再自行替换包管理器命令,否则 CI 会卡在 registry。

执行完毕后,项目根目录生成 .eslintrc.js(或 yaml/json),并在 package.json 追加 script:
"lint": "eslint . --ext .js,.jsx,.ts,.tsx --max-warnings 0"
至此,规则集、解析器、插件、环境变量、全局变量一次性就位,可直接对接 VS Code 的 eslint 插件与 husky lint-staged。

拓展思考

  1. Monorepo 场景:
    在 pnpm-workspace 项目里,--init 默认把配置放在根目录。子包若用不同框架(React + Vue),需在 packages/*/ 下再建 .eslintrc.js,并设置 root: true,避免向上继承冲突。可结合 eslint-config-turbo 做规则分层。

  2. 渐进迁移老代码:
    先选“check syntax only”,在 .eslintrc.js 里将所有规则置为 warn,配合 eslint-filtered-fix 仅修复安全规则(如 no-var),通过 git cherry-pick 逐步清零 warn,实现“边需求边重构”。

  3. 与 Prettier 并存:
    --init 生成的 Airbnb 配置包含大量风格规则,与 Prettier 冲突。正确姿势:
    extends: ['airbnb-typescript', 'prettier'],
    并在 .prettierrc.js 里关闭 ESLint 的格式化职责,使 ESLint 专注逻辑、Prettier 专注格式,CI 阶段并行跑 eslint + prettier --check,平均节省 30% 流水线时间。

  4. 自定义共享配置发包:
    将公司规范封装成 @my-company/eslint-config,内部 extends: ['@my-company'],并在 init 最后一步选“Inspect your JavaScript file(s)”指向本地 index.js,即可把私有规则沉淀为 npm 包,统一 20+ 仓库。

  5. 性能优化:
    大型项目 init 后首次全量 lint 可能耗时 30s+,可在 .eslintrc.js 里配置 cache: true、cacheLocation: 'node_modules/.cache/eslint',CI 中把该目录加入 actions/cache,二次 lint 可降至 5s 内,满足“增量合并请求”时效要求。

把 --init 的每一步吃透,你不仅能回答“选项含义”,还能在面试官追问“如何落地 Monorepo、如何兼容 Legacy、如何提速 CI”时,给出工程级方案,直接拉开与其他候选人的差距。