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

如何为jQuery配置ESLint(Flat Config模式)?

解决ESLint Flat Config适配jQuery插件的问题

1. 优先改用局部安装(规避全局模块引用问题)

全局安装的ESLint插件在Flat Config中容易出现路径识别问题,建议直接在项目目录下局部安装依赖:

# 可选:卸载全局的ESLint和插件,避免版本冲突
npm uninstall -g eslint eslint-plugin-jquery
# 局部安装到项目开发依赖
npm install eslint eslint-plugin-jquery --save-dev

2. 修正eslint.config.mjs配置

Flat Config要求plugins必须是键值对对象(键为插件别名,值为导入的插件模块),不能用数组形式。正确配置示例如下:

import globals from "globals";
import pluginJs from "@eslint/js";
// 导入jquery插件模块
import jqueryPlugin from "eslint-plugin-jquery";

export default [
  // 匹配所有JS文件,设置脚本执行模式
  { files: ["**/*.js"], languageOptions: { sourceType: "script" } },
  // 启用浏览器环境全局变量
  { languageOptions: { globals: globals.browser } },
  // 启用ESLint官方推荐的JS规则
  pluginJs.configs.recommended,
  // 配置jQuery插件及规则集
  {
    plugins: {
      jquery: jqueryPlugin
    },
    // 启用jQuery的deprecated规则集,可按需替换为plugin:jquery/recommended等其他规则
    extends: ["plugin:jquery/deprecated"],
    // 可选:单独调整特定jQuery规则的报错级别
    rules: {
      "jquery/no-ajax": "warn",
      "jquery/no-deprecated-events": "error"
    }
  }
];

3. 验证配置有效性

运行ESLint命令测试配置:

npx eslint build/may08/js/merged.min.js

关于全局安装插件的引用(不推荐)

如果坚持使用全局安装的插件,需要先找到WSL2中全局npm包的路径(通常为~/.npm-global/lib/node_modules/),然后通过绝对路径导入:

// 替换为你的全局node_modules实际路径
import jqueryPlugin from "/home/你的用户名/.npm-global/lib/node_modules/eslint-plugin-jquery";

这种方式灵活性差,换环境需修改路径,优先推荐局部安装方案。

内容的提问来源于stack exchange,提问作者J-A-Brown

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 05:22:34