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

如何在Eslint v8.50的eslintrc.js配置中引入本地插件或规则?

ESLint v8.x(非Flat Config)本地插件简化引入方案

以下是几种无需迁移到Flat Config的简化方法,解决本地ESLint插件的引入问题:

方法1:利用NODE_PATH环境变量指定加载目录

通过设置NODE_PATH让Node.js识别本地插件目录,无需修改插件结构:

  • 在项目根目录创建存放本地插件的文件夹,比如local-plugins,将你的eslint-plugin-example目录放入其中
  • 在启动ESLint时指定NODE_PATH,或者把命令写入package.json的scripts:
    # 直接执行
    NODE_PATH=./local-plugins eslint .
    
    # 或在package.json中配置
    {
      "scripts": {
        "lint": "NODE_PATH=./local-plugins eslint ."
      }
    }
    
  • 在eslintrc.js中正常配置插件:
    module.exports = {
      plugins: ["example"], // 对应插件目录名eslint-plugin-example
      rules: {
        "example/your-custom-rule": "error"
      }
    };
    

方法2:在配置文件中直接加载插件对象

如果之前传入插件对象失败,大概率是插件格式不符合要求,正确做法:

  • 确保本地插件的index.js导出包含name属性的对象:
    // ./eslint-plugin-example/index.js
    module.exports = {
      name: "example", // ESLint依赖此属性识别规则前缀
      rules: {
        "your-custom-rule": {
          create(context) {
            // 自定义规则逻辑
          }
        }
      }
    };
    
  • 在eslintrc.js中直接引入并注册插件:
    const examplePlugin = require("./eslint-plugin-example");
    
    module.exports = {
      plugins: [examplePlugin],
      rules: {
        "example/your-custom-rule": "error"
      }
    };
    

方法3:使用npm link快速链接本地插件

比本地install软链接更灵活,修改插件后无需重新安装:

  1. 进入本地插件目录./eslint-plugin-example,执行:
    npm link
    
  2. 回到项目根目录,执行:
    npm link eslint-plugin-example
    
  3. 在eslintrc.js中正常配置插件即可:
    module.exports = {
      plugins: ["example"],
      rules: {
        "example/your-custom-rule": "error"
      }
    };
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 11:21:09