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

如何配置ESLint以识别package.json中的Conditional Exports?

解决ESLint无法识别库的Conditional Exports问题

当你的项目依赖带有Conditional Exports配置的库,代码能正常运行但ESLint抛出import/no-unresolved错误时,可以通过以下配置让ESLint正确识别库的导出路径:

1. 安装必要依赖

确保项目已安装eslint-import-resolver-node(未安装则执行):

npm install eslint-import-resolver-node --save-dev
# 或使用yarn
yarn add eslint-import-resolver-node --dev

2. 修改ESLint配置文件

在项目的.eslintrc.js(或.eslintrc.json等配置文件)中,添加import/resolver配置,开启对package.json中exports字段的解析:

module.exports = {
  // 保留原有其他配置(如rules、parser等)
  settings: {
    'import/resolver': {
      node: {
        // 指定解析package.json中的exports字段
        exportsFields: ['exports'],
        // 根据项目文件类型添加扩展名,避免解析非目标文件
        extensions: ['.js', '.jsx', '.ts', '.tsx']
      }
    }
  }
};

如果库的exports配置包含多条件导出(如区分import和require),上述配置也能自动适配解析逻辑。

3. 验证配置

重启ESLint服务(或重新执行lint命令),检查Unable to resolve path to module 'myLib/ComponentA'错误是否消失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 14:45:55