FirebaseError: Expected first argument to collection() to be a CollectionReference
FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestoreShort answer
In the modular Firebase SDK, collection() is a function whose first argument must be the Firestore instance from getFirestore(app). The error means you passed something else: undefined, a missing import, or an object from the older namespaced API. Pass the db created by getFirestore and keep one API style.
Fix it fast
1Find out which cause you have
Log the value you pass as the first argument. It should be a Firestore object, not undefined and not a plain config.
console.log(db); const ref = collection(db, "users");2Match what you see, then jump to its fix
What does the error look like?
FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore
Cause 1 of 2
db is undefined or is not the Firestore instance
1Check
Check how db is exported and imported. A wrong import path, or exporting app instead of db, gives undefined or the wrong object.
grep -rn "getFirestore\|import .*db" src/ lib/ 2>/dev/null2Fix
Create db once with getFirestore(app), export it, and import that same name wherever collection() is called. The Firebase quickstart passes db as the first argument every time.
// lib/firebase.js
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);
// elsewhere
import { db } from "@/lib/firebase";
import { collection, getDocs } from "firebase/firestore";
const snap = await getDocs(collection(db, "users"));3Verify
The call returns a snapshot and the error is gone.
console.log(snap.size);Confirmed by firebase.google.com
Cause 2 of 2
The namespaced and modular APIs are mixed up
1Check
The two styles differ: namespaced code calls db.collection("x"), modular code calls collection(db, "x"). Look for compat imports next to modular calls.
grep -rn "firebase/compat\|firebase.firestore()" src/2Fix
Move the file to one style. In the modular style use getFirestore and free functions. Compat objects only work with compat calls, per the upgrade guide.
// namespaced (compat)
const db = firebase.firestore();
db.collection("cities").get();
// modular
import { getFirestore, collection, getDocs } from "firebase/firestore";
const db = getFirestore(app);
await getDocs(collection(db, "cities"));3Verify
Run the page again; no FirebaseError is thrown.
npm run devConfirmed by firebase.google.com
How do I stop it coming back?
- Export db from one module and import it everywhere; never rebuild it inline.
- Do not mix firebase/compat imports with modular calls in the same file.
- In Next.js, keep the Firebase init file free of server-only values so it imports cleanly on both sides.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fixWeb apps
Google sign-in popup blocked by Cross-Origin-Opener-Policy
The page loses its reference to the sign-in popup window, because the opener policy severs it
If your site sends Cross-Origin-Opener-Policy: same-origin, a sign-in popup from another origin lands in a separate browsing context group, so your page cannot reach it.
3 causes, each with a fixWeb apps
AuthUserPoolException: Auth UserPool not configured in a Next.js app
Auth UserPool not configured
The error means an Amplify Auth call ran before Amplify.configure did.
3 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fix