0

I tried to connect my javascript app with firebase, I created an account in firebase, I pasted my configuration, but it gives me 3 errors:

Unexpected token 'export';

Cannot use import statement outside a module;

firebase is not defined

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <script src="https://www.gstatic.com/firebasejs/9.8.3/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/9.8.3/firebase-firestore.js"></script>

    <script type="module">
        // Import the functions you need from the SDKs you need
        import { initializeApp } from "https://www.gstatic.com/firebasejs/9.8.3/firebase-app.js";
        // TODO: Add SDKs for Firebase products that you want to use
        // https://firebase.google.com/docs/web/setup#available-libraries
      
        // Your web app's Firebase configuration
        const firebaseConfig = {
          apiKey: "<key>",
          authDomain: "javascript-3bbaa.firebaseapp.com",
          projectId: "javascript-3bbaa",
          storageBucket: "javascript-3bbaa.appspot.com",
          messagingSenderId: "<id>",
          appId: "<appId>"
        };
      
        // Initialize Firebase
        const firebase = initializeApp(firebaseConfig);
        const db = firebase.firestore();
      </script>
    
</body>
</html>
kimkim
  • 29
  • 3
  • Does this answer your question? [ES6: import module from URL](https://stackoverflow.com/questions/34607252/es6-import-module-from-url) – jabaa Jun 23 '22 at 19:59
  • @jabaa thank you for your answer but it remains `firebase.firestore is not a function` – kimkim Jun 23 '22 at 20:07
  • That's a different problem. The problem in your question is `Unexpected token 'export';` and the duplicate contains solutions. – jabaa Jun 23 '22 at 21:23

1 Answers1

0

In firebase v9, you have to use getFirestore method. Try this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

<script type="module">

    import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.8.3/firebase-app.js';
    import { getFirestore, collection, getDocs } from 'https://www.gstatic.com/firebasejs/9.8.3/firebase-firestore-lite.js';
    
    const firebaseConfig = {
        apiKey: "<key>",
        authDomain: "javascript-3bbaa.firebaseapp.com",
        projectId: "javascript-3bbaa",
        storageBucket: "javascript-3bbaa.appspot.com",
        messagingSenderId: "<id>",
        appId: "<appId>"
    };

    // Initialize Firebase
    const app = initializeApp(firebaseConfig);
    const db = getFirestore(app);
</script>

</body>
</html>
  • Code-only are usually not really helpful. In this case I first had to search for your modifications. – jabaa Jun 23 '22 at 21:21
  • 1
    You did much more in the code than you described in your answer. You removed script tags, added imports, ... What else did you do and why? – jabaa Jun 23 '22 at 22:27
  • 1
    You are using firebase v9, and writing code according to firebase v8. In v9, you dont need script tags. – Saqib Rasheed Jun 24 '22 at 10:11
  • You need to import initializeApp from firebase-app.js, which, as the name says, initilizes the app. And getFirestore from firebase-firestore-lite.js, takes the app created by initializeApp as an input parameter and returns firestore db. – Saqib Rasheed Jun 24 '22 at 10:13
  • You can find more in firebase documentation here: https://www.npmjs.com/package/firebase – Saqib Rasheed Jun 24 '22 at 10:14
  • Don't write it as comments. Edit you answer and add it there. (I didn't ask the question) – jabaa Jun 24 '22 at 10:24