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

如何在CommonJS语法的ESLint9中使用eslint:recommended与Google规则集

ESLint9 完全支持 CommonJS 格式的配置文件,按以下步骤调整你的 eslint.config.js 即可实现需求:

  1. 安装依赖包
    确保已安装 ESLint 核心包和 Google 规则集:

    npm install eslint eslint-config-google --save-dev
    
  2. 编写 CommonJS 格式配置
    修改 eslint.config.js 为如下内容,用 require 引入规则集,通过 module.exports 导出配置数组:

    // eslint.config.js (CommonJS 格式)
    const eslintRecommended = require('eslint/configs/recommended');
    const googleConfig = require('eslint-config-google');
    
    module.exports = [
      // 启用 eslint:recommended 内置推荐规则
      eslintRecommended,
      // 启用 Google 代码风格规则集
      googleConfig,
      // 自定义项目配置(可选)
      {
        languageOptions: {
          ecmaVersion: 'latest',
          sourceType: 'commonjs' // 匹配项目的 CommonJS 模块类型
        },
        rules: {
          // 此处可覆盖或添加自定义规则,例如:
          // 'no-console': 'warn'
        }
      }
    ];
    
  3. 验证配置生效
    在项目根目录执行 ESLint 命令检查代码:

    npx eslint .
    

关键说明

  • ESLint9 采用数组形式的配置结构,数组内每个元素可以是预定义规则集对象,或是自定义配置对象。
  • eslint/configs/recommended 是 ESLint9 内置推荐规则集的 CommonJS 入口,替代了旧版配置中 "extends": "eslint:recommended" 的写法。
  • eslint-config-google 原生支持 CommonJS 引入,直接 require 后加入配置数组即可生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 16:53:09