I am trying to implement firebase on my server and I am coming with a lot of errors. I have been trying for a few hours now with a bunch of tweaks and different ways to try and get this working, but no such luck. This is where my code sits now:
<script type="module">
import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.6.3/firebase-app.js'
import { getAnalytics } from 'https://www.gstatic.com/firebasejs/9.6.3/firebase-analytics.js'
import { getMessaging } from 'https://www.gstatic.com/firebasejs/9.6.3/firebase-messaging.js'
</script>
<script src="https://www.gstatic.com/firebasejs/4.3.0/firebase.js"></script>
<script>
const app = firebase.initializeApp({
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
});
console.log(app);
const messaging = getMessaging();
getToken(messaging, { vapidKey: '' }).then((currentToken) => {
if (currentToken) {
console.log('got token');
} else {
// Show permission request UI
console.log('No registration token available. Request permission to generate one.');
// ...
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
// ...
});
</script>
and the error i am getting is
Uncaught ReferenceError: getMessaging is not defined
at index.html:20:21
I have tried different variations like
const messaging = firebase.getMessaging();
and
const messaging = app.getMessaging();
but it always come back undefined. Shouldn’t it be pulling from the import library?
Also, I know the documents say all you need is const app = initializeApp({})
but that was also returning undefined until I imported this script <script src="https://www.gstatic.com/firebasejs/4.3.0/firebase.js"></script>
and called to it using firebase.initializeApp()
.. I don’t even know if that is the most up-to-date resource. I have installed all of the node_modules with NPM but I don’t really know how node works and the "firebase/app"
paths weren’t working for me. Any help or guidance would be greatly appreciated, thank you!