I have installed firebase sdk with npm install firebase
like on documentation
package.json
{
"main": "firebase.js",
"type": "module",
"dependencies": {
"firebase": "^9.15.0"
}
}
firebase.js
import { initializeApp } from 'firebase/app';
const firebaseConfig = {
//... My apiKey ... etc
};
const app = initializeApp(firebaseConfig);
console.log('DEBUG APP',app);
//until here everything ok I can see the app object
The next step on documentation is Configure Web credentials:
import { getMessaging, getToken } from "firebase/messaging";
//here got error:
//SyntaxError: The requested module 'firebase/messaging' does not provide an export named 'getToken'
//I tried only import { getMessaging } ... but also not provided
const messaging = getMessaging();
// Add the public key generated from the console here.
getToken(messaging, {vapidKey: "my_wap_public_key_here"});
I have tried also from webpage client side (like documentation )
Do you use ESM and want to use browser modules? Replace all your import lines to use the following pattern: import { } from 'https://www.gstatic.com/firebasejs/9.15.0/firebase-SERVICE.js' (where SERVICE is an SDK name such as firebase-firestore).
But if i try
import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js';
I got error Uncaught SyntaxError: Cannot use import statement outside a module
CodePudding user response:
The Firebase Web SDK is only for use in front-end / browser environments whereas Node.js is considered a server environment.
From the FCM documentation...
Choosing a server option
You'll need to decide on a way to interact with FCM servers: either using the Firebase Admin SDK or the raw protocols. Because of its support across popular programming languages and its convenience methods for handling authentication and authorization, the Firebase Admin SDK is the recommended method.
You can find the Node.JS Admin SDK documentation here ~ https://firebase.google.com/docs/admin/setup#node.js