如何在CommonJS语法的ESLint9中使用eslint:recommended与Google规则集
ESLint9 CommonJS 配置集成 eslint:recommended 和 Google 规则集
ESLint9 完全支持 CommonJS 格式的配置文件,按以下步骤调整你的 eslint.config.js 即可实现需求:
安装依赖包
确保已安装 ESLint 核心包和 Google 规则集:npm install eslint eslint-config-google --save-dev编写 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' } } ];验证配置生效
在项目根目录执行 ESLint 命令检查代码:npx eslint .
关键说明
- ESLint9 采用数组形式的配置结构,数组内每个元素可以是预定义规则集对象,或是自定义配置对象。
eslint/configs/recommended是 ESLint9 内置推荐规则集的 CommonJS 入口,替代了旧版配置中"extends": "eslint:recommended"的写法。eslint-config-google原生支持 CommonJS 引入,直接require后加入配置数组即可生效。
内容的提问来源于stack exchange,提问作者HOK
相关产品推荐
相关产品推荐

