React+Next.js+Firebase Auth报错:Component auth未注册
解决Firebase "Component auth has not been registered yet" 部署错误
我用Docker Compose运行Next.js应用,近期部署模式下出现以下错误,此前开发和部署均正常:
Error: Component auth has not been registered yet at a.initialize (/app/.next/server/chunks/777.js:3638:2077) at rS (/app/.next/server/chunks/777.js:868:166) at nc (/app/.next/server/chunks/777.js:1243:1031) at 13842 (/app/.next/server/chunks/520.js:19:12286) at t (/app/.next/server/webpack-runtime.js:1:128) at 57427 (/app/.next/server/chunks/520.js:19:10854) at t (/app/.next/server/webpack-runtime.js:1:128) at 85068 (/app/.next/server/app/[locale]/page.js:1:10126) at Object.t [as require] (/app/.next/server/webpack-runtime.js:1:128) at require (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:16:18490) { digest: '4174768128' }
Firebase初始化代码:
import { initializeApp } from 'firebase/app'; import { getAuth } from 'firebase/auth'; import { getFirestore } from 'firebase/firestore'; const firebaseConfig = { apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY, authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN, projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID, storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET, messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID, appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID, }; const app = initializeApp(firebaseConfig); const db = getFirestore(app); const auth = getAuth(app); // 问题来源 export { auth, db };
package.json依赖片段:
"dependencies": { "@firebase/firestore": "^4.6.5", "firebase": "^10.12.4", // 其他依赖省略 }
npm ls @firebase/app输出显示存在两个版本的@firebase/app:0.10.13和0.10.15,说明依赖版本冲突。
问题原因
同时安装firebase核心包和单独的@firebase/firestore包,两者依赖的@firebase/app版本不一致,导致Firebase组件注册逻辑混乱,生产构建时触发auth组件未注册错误。
解决方案
移除冲突依赖
编辑package.json,删除@firebase/firestore依赖项——firebase核心包已包含firestore模块,无需单独安装。清理并重装依赖
执行以下命令:# 删除node_modules和锁文件 rm -rf node_modules package-lock.json # 重新安装依赖 npm install重新构建部署
更新Docker镜像并重启服务:# 构建生产版本 npm run build # 重启Docker Compose服务 docker-compose up --build -d
额外验证
如果问题仍存在,可将firebase版本锁定为最新稳定版(如^10.14.1),确保所有子依赖版本统一。
内容的提问来源于stack exchange,提问作者Maxime Debarbat
相关产品推荐
相关产品推荐

