javascriptfirebasegoogle-cloud-firestorefirebase-authenticationuncaught-reference-error

Firebase auth and firestore functionalities not working


I am using firebase for the backend part of my application. My code goes like this:

 <script type="module">
        // Import the functions you need from the SDKs you need
        import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.1/firebase-app.js";
        // TODO: Add SDKs for Firebase products that you want to use
        // https://firebase.google.com/docs/web/setup#available-libraries
        import {getFirestore,collection,getDocs} from 'firebase/firestore/lite'
        // Your web app's Firebase configuration
        const firebaseConfig = {
         //My app's Config object...
        };
      
        // Initialize Firebase
        const app = initializeApp(firebaseConfig);
        
      </script>
     <script src="https://www.gstatic.com/firebasejs/9.6.1/firebase-app.js"></script>
     <script src="https://www.gstatic.com/firebasejs/9.6.1/firebase-auth.js"></script>
     <script src="https://www.gstatic.com/firebasejs/9.6.1/firebase-firestore.js"></script>

auth script:

const auth=firebase.auth();
const db=firebase.firestore();

I am getting these errors in the browser console:

Uncaught SyntaxError: Unexpected token 'export'
Uncaught SyntaxError: Cannot use import statement outside a module
Uncaught SyntaxError: Cannot use import statement outside a module
Uncaught ReferenceError: firebase is not defined at auth.js:1

Can someone pls help me!


Solution

  • Since version 9 of its JavaScript SDK, Firebase has changed the way you call its modules. These two lines in your code are for the v8 and before SDKs:

    const auth=firebase.auth();
    const db=firebase.firestore();
    

    The equivalent for v9 and above is:

    const auth = getAuth();
    const db = getFirestore()
    

    I recommend checking out the Firebase documentation, which contains examples for both SDK variants, the upgrade guide for the v9 SDK, and this blog post on the modular SDK design