0

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.

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
  • 2
    Can you share the content of the package.json file? Commented Sep 20, 2021 at 12:25

1 Answer 1

2

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(); 
Sign up to request clarification or add additional context in comments.

2 Comments

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.
I have updated the answer. Pls try to use from the firebase documentation the instructions for the SDK version 9.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.