Hi, I'm trying to do a react clone with firebase backend, but for some reason it can't compile.I get a 'can't resolve firebase' message instead. None of the solutions I've found and employed have worked thus far.
1 Answer
It looks like you are using the Firebase SDK 8 code but probably has installed the Firebase SDK 9 version. Your code should look like this with the new SDK:
import { getFirestore } from "firebase/firestore"; import { initializeApp } from "firebase/app"; import { getAuth} from "firebase/auth"; const firebaseConfig = { apiKey: "x", authDomain: "x", projectId: "x", storageBucket: "x", messagingSenderId: "x", appId: "x", }; const app = initializeApp(firebaseConfig); const db = getFirestore(); const auth = getAuth(); export { db, auth }; Here you can find more about the migration to the SDK 9 version.
Here is an example how to use the new SDK 9 with collections:
import { collection, onSnapshot } from "firebase/firestore"; const unsubscribe = onSnapshot(collection(db, "cities"), () => { // Respond to data // ... }); // Later ... // Stop listening to changes unsubscribe(); 2 Comments
ronan23
Thanks Tarik,That seems to have resolved that problem. But now when I use the Useeffect hook, it says db.collection is not a function. Any ideas whats causing that? Thanks.
Tarik Huber
I have updated the answer. Pls try to use from the firebase documentation the instructions for the SDK version 9.

package.jsonfile?