1

I'm learning firebase, and I'm trying to setup a simple button to launch the auth popup.

However my app seems to be unable to run any code from any external user defined js file.

I've done the needful, having setup using firebase init hosting, and I run the server using firebase emulators:start

I just want it to open src=test.js and run the console log. But no matter what I do, I can't seem to get it to run the console log as long as it's on a separate document. When I put console log back in the index.html file, it works. but when I try to call another document I get nothing.

I've tried using defer with the script, but that does nothing either, as well as putting ./ in front of the file name.

the app seems to be loading the firebase files just fine, as I see some console log outputs to the browser console, but nothing from user defined ones. Any help would be appreciated.

I'm at a complete loss. The file is located in the public folder right next to index.html

I'm trying to run this on localhost:5000

<title>Welcome to Firebase Hosting</title>

<script type="text/javascript" src="test.js"></script>

    <!-- update the version number as needed -->
    <script defer src="/__/firebase/9.5.0/firebase-app-compat.js"></script>
    <!-- include only the Firebase features as you need -->
    <script defer src="/__/firebase/9.5.0/firebase-auth-compat.js"></script>
    <script defer src="/__/firebase/9.5.0/firebase-database-compat.js"></script>
    <script defer src="/__/firebase/9.5.0/firebase-firestore-compat.js"></script>
    <script defer src="/__/firebase/9.5.0/firebase-functions-compat.js"></script>
    <script defer src="/__/firebase/9.5.0/firebase-messaging-compat.js"></script>
    <script defer src="/__/firebase/9.5.0/firebase-storage-compat.js"></script>
    <script defer src="/__/firebase/9.5.0/firebase-analytics-compat.js"></script>
    <script defer src="/__/firebase/9.5.0/firebase-remote-config-compat.js"></script>
    <script defer src="/__/firebase/9.5.0/firebase-performance-compat.js"></script> 
<script defer src="/__/firebase/init.js?useEmulator=true"></script>

test.js

console.log("test");

So I was able to get it working with this code. Rather than using the built in initializers, I went thru the firebase documentation and a different tutorial. It seems something in Firebase was preventing the file from being served.

However now I have another issue (i'll make a new question for this), I suspect the items in the first <script> tag are not running as i'm now getting the error:

Uncaught SyntaxError: Cannot use import statement outside a module

which i'm not really sure how to handle. I guess I need to put them in a separate file, as a module and re-import it right?

<!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>
  <script>// Import the functions you need from the SDKs you need
    import { initializeApp } from "firebase/app";
    import { getAnalytics } from "firebase/analytics";
    // TODO: Add SDKs for Firebase products that you want to use
    // https://firebase.google.com/docs/web/setup#available-libraries
    import{getAuth, createUserWithEmailAndPassword} from 'firebase/auth';
    import { getFirestore } from "firebase/firestore";
    // Your web app's Firebase configuration
    // For Firebase JS SDK v7.20.0 and later, measurementId is optional
    const firebaseConfig = {
      apiKey: "AIzaSyA948R6SrFwe_Q_MA6ea58NxGXNHU_ilV8",
      authDomain: "md-data-2d027.firebaseapp.com",
      projectId: "md-data-2d027",
      storageBucket: "md-data-2d027.appspot.com",
      messagingSenderId: "531965376903",
      appId: "1:531965376903:web:8400361210fe42f6543b22",
      measurementId: "G-2QNMX6B4EY"
    };
    
    // Initialize Firebase
    const app = initializeApp(firebaseConfig);
    const analytics = getAnalytics(app);
    console.log("app:" + app);
    </script>
    <script src="app.js" defer></script>
</head>
<body>
  
</body>
</html>
koloml
  • 525
  • 2
  • 15
Sharan Balani
  • 143
  • 1
  • 9
  • 2
    You have a typo in your statement (missing quote after the last `t` in `test`). It should be `console.log("test");` – jsejcksn Nov 29 '21 at 07:09
  • fixed it, but still nothing, If the file was being read the console should have at least popped up an error right? but No error even appeared – Sharan Balani Nov 29 '21 at 07:32
  • If you comment all other scripts out, what happens? – connexo Nov 29 '21 at 07:36
  • trieed that, still can't see the console log. I wonder though if this has anythign to do with it: emulators: Starting emulators: hosting i hosting: Serving hosting files from: public – Sharan Balani Nov 29 '21 at 07:44
  • Check that `test.js` is loaded correctly (in your browser's "network" tab). I'm almost sure your backend is not serving this file to your browser. It's most likely a backend issue, it has nothing to do with Firebase. – Jeremy Thille Nov 29 '21 at 07:47
  • I think you were right @JeremyThille i was able to get it workign (see my edits), but now have a new error – Sharan Balani Nov 29 '21 at 08:20
  • Nice, but now that's a completely different problem. See https://stackoverflow.com/questions/58211880/uncaught-syntaxerror-cannot-use-import-statement-outside-a-module-when-import – Jeremy Thille Nov 29 '21 at 08:40

0 Answers0