Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: firebase.messaging is not a function in node.js

While trying to fetch the FCM messages in node.js using firebase module, following error "TypeError: firebase.messaging is not a function" is occurring.

var firebase = require("firebase");

firebase.initializeApp({
    apiKey: "xxxxxxx",
    authDomain: "xxxxxxx",
    databaseURL: "xxxxxxx",
    projectId: "xxxxxxx",
    storageBucket: "xxxxxxx",
    messagingSenderId: "xxxxxxx"
});

const messaging = firebase.messaging();

How to rectify this error and how to use firebase module to pull messages??

like image 365
viz_tm Avatar asked Jun 01 '17 13:06

viz_tm


3 Answers

You must included import '@firebase/messaging' for it to work. So it's supposed to look like this:

import * as firebase from 'firebase/app';
import '@firebase/messaging';
like image 122
user11411137 Avatar answered Oct 10 '22 17:10

user11411137


The criteria you are trying to use only works on the browser:

You have to require firebase-messaging, check this full sample it will guide you https://github.com/firebase/quickstart-js/tree/master/messaging

For nodeJS implementation, you have to use admin.messaging

https://firebase.google.com/docs/reference/admin/node/admin.messaging

// Get the Messaging service for the default app
var defaultMessaging = admin.messaging();
like image 28
Bamieh Avatar answered Oct 10 '22 18:10

Bamieh


The documentation for Firebase does not make it clear that there is a difference of features available based on the current environment. firebase.messaging is not available to a Node.js client, but is available from the firebase-admin package. However, this package alone comes with a different set of features specfically for firebase.messaging.

You can see what's available to you based on your environment in the Firebase Reference docs. Specifically for your case the Node.js (Client) section.

like image 36
Francisco Mateo Avatar answered Oct 10 '22 17:10

Francisco Mateo