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

Firebase部署NextJS应用遇文件复制错误:找不到指定index.html文件

解决Next.js App Router部署Firebase Hosting时的文件复制错误

问题根源

你用的是Next.js 13的App Router,它的构建产物目录结构和传统Pages Router不一样:App Router的静态页面文件存在.next/app里,但firebase-tools默认会按Pages Router的路径.next/server/pages去查找,结果就找不到文件,抛出了ENOENT错误。

解决方案

1. 用Firebase的Next.js框架集成自动生成配置

重新初始化Firebase项目,让工具自动生成适配App Router的配置:

firebase init hosting

初始化过程中按提示操作:

  • 选择你现有的Firebase项目
  • 输入public目录时直接回车(使用默认值)
  • 询问是否将目录设为单页应用时选No
  • 询问是否自动构建部署Next.js应用时选Yes
  • 选择你的Next.js项目根目录(一般就是当前目录)
  • 确认构建命令为npm run build

2. 手动修改firebase.json配置

不想重新初始化的话,直接修改firebase.json,确保适配App Router的结构:

{
  "hosting": {
    "source": ".",
    "frameworksBackend": {
      "region": "us-central1"
    },
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "function": "nextjsServer"
      }
    ]
  },
  "functions": {
    "source": ".next/server"
  }
}

这个配置会让Firebase正确识别Next.js App Router的服务器端产物,通过Cloud Functions处理所有路由请求。

3. 检查Next.js的构建配置

如果你的next.config.js里设置了output: 'standalone',得额外调整:

  • 把firebase.json里的functions.source改成.next/standalone
  • 部署前先执行next build,确保生成完整的构建产物

4. 清理缓存后重新部署

rm -rf .next .firebase
npm run build
firebase deploy --only hosting

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 16:48:14