You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ESLint CLI中传递简单的extends配置?

问题:如何在ESLint CLI中传递extends配置?

我有一个纯Node+TypeScript+Prettier项目,原本的ESLint配置文件.eslintrc.yml内容如下:

env:
  node: true
  es2022: true

extends:
  - eslint:recommended
  - plugin:@typescript-eslint/recommended

parser: '@typescript-eslint/parser'

parserOptions:
  ecmaVersion: latest
  sourceType: module

plugins:
  - '@typescript-eslint'

出于需求,我想移除所有.config.*.yml类配置文件,完全通过CLI命令完成ESLint配置。目前已经转换了大部分配置项,唯独extends部分找不到对应的CLI参数(官方文档也无相关说明),当前使用的CLI命令如下:

npx eslint
    --no-eslintrc
    --env node,es2022
    --parser @typescript-eslint/parser
    --parser-options ecmaVersion:latest
    --parser-options sourceType:module
    --plugin @typescript-eslint
    --fix
    .

请问是否有办法在CLI中传递extends配置?


解决方案

ESLint CLI本身没有直接对应extends的参数,但可以通过--config参数间接实现,有两种可行方式:

方式一:使用进程传递临时配置(无文件残留)

利用Shell的进程替换功能,将包含extends的极简配置以JSON格式传递给--config参数,无需生成物理文件:

npx eslint \
  --no-eslintrc \
  --env node,es2022 \
  --parser @typescript-eslint/parser \
  --parser-options ecmaVersion:latest \
  --parser-options sourceType:module \
  --plugin @typescript-eslint \
  --config <(echo '{
    "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"]
  }') \
  --fix \
  .

这个命令通过<(echo ...)生成临时的配置输入流,让ESLint加载包含extends规则的配置。

方式二:使用临时配置文件

如果你更倾向于文件形式,可以创建一个极简的临时配置文件(比如.eslintrc.tmp.json),内容仅包含extends:

{
  "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"]
}

然后在CLI中指定这个文件:

npx eslint \
  --no-eslintrc \
  --env node,es2022 \
  --parser @typescript-eslint/parser \
  --parser-options ecmaVersion:latest \
  --parser-options sourceType:module \
  --plugin @typescript-eslint \
  --config .eslintrc.tmp.json \
  --fix \
  .

使用完成后可手动删除临时文件。


内容的提问来源于stack exchange,提问作者charnould

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 08:02:20