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

自定义ESLint配置包遇两类问题求助:resolve/sync缺失与lodash依赖提示

两个ESLint配置问题的解决方案

问题1:Cannot find module 'resolve/sync'

原因

当前安装的resolve版本为^1.1.7,该版本过于陈旧,不存在resolve/sync子模块,而eslint-import-resolver-node等依赖需要调用此模块,导致报错。

解决步骤

  1. 卸载旧版本resolve:
npm uninstall resolve
  1. 安装最新稳定版resolve:
npm install resolve --save-dev

也可以直接修改package.json中resolve的版本为^1.22.8(或更高稳定版本),再执行npm install。

执行完成后重新运行ESLint,该模块找不到的报错即可消除。


问题2:'lodash' should be listed in the project's dependencies

原因

尽管在webpack的externals中声明了lodash为外部依赖,但eslint-plugin-import的import/no-extraneous-dependencies规则默认不会读取webpack的externals配置,仅检查package.json中的依赖列表;而配置的devDependencies: true仅允许使用devDependencies内的包,不影响外部依赖的识别逻辑。

解决方法(任选其一)

方法一:添加外部依赖白名单

修改ESLint配置的rules,为import/no-extraneous-dependencies添加allowlist选项,将lodash纳入白名单:

rules: {
  'import/no-extraneous-dependencies': ['error', {
    devDependencies: true,
    allowlist: ['lodash']
  }]
}

方法二:让ESLint读取webpack的externals配置

确保eslint-import-resolver-webpack能正确加载webpack配置并识别externals:

  • 确认webpack配置文件的相对路径正确(当前配置为./webpack/webpack.config.js,需保证路径指向正确)
  • 若仍不生效,可在ESLint的settings中直接为webpack resolver补充externals配置:
settings: {
  'import/resolver': {
    alias: [
      ['@src', './src'],
    ],
    node: {
      extensions: ['.js', '.jsx', '.ts', '.tsx'],
    },
    webpack: {
      config: './webpack/webpack.config.js',
      externals: {
        'lodash': 'Lodash'
      }
    },
  },
}

方法三:单独忽略lodash的该规则检查

若上述方法均不适用,可直接禁用该规则对lodash的检查:

rules: {
  'import/no-extraneous-dependencies': ['error', {
    devDependencies: true,
    excludeImport: ['lodash']
  }]
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 08:22:40