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

如何修复Vercel部署Next.js时exports字段无法解析目录的问题

Vercel部署Next.js失败(本地构建正常)的解决方案

报错信息

ModuleNotFoundError: Module not found: Error: Resolving to directories is not possible with the exports field (request was ./)

解决步骤

  • 检查项目package.json的exports字段,禁止使用./这类指向目录的导出配置。例如若存在以下错误配置:

    "exports": {
      "./": "./src/"
    }
    

    需修改为具体文件导出:

    "exports": {
      "./utils": "./src/utils/index.js",
      "./components": "./src/components/index.js"
    }
    
  • 排查代码中的导入语句,避免直接导入目录(如import xxx from './')。本地环境可能自动识别目录下的index文件,但Vercel构建规则更严格,需改为具体文件路径,比如import xxx from './index.js'。

  • 同步依赖版本:删除本地node_modules、package-lock.json或yarn.lock,重新安装依赖后再部署,避免Vercel构建时依赖版本不一致导致解析失败。

  • 检查next.config.js中的webpack配置,确认别名等模块解析规则是否存在冲突,确保配置符合Vercel的构建要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 23:46:59