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

Viewed 20031

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??

5 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';

For react-native-firebase version 5.x.x and 6.x.x

import firebase from '@react-native-firebase/app'
import '@react-native-firebase/messaging'

for example get token

const fcmToken = await firebase.messaging().getToken();
console.log(fcmToken)

In my case was that I was importing firebase only as

import '@firebase/app'

instead of

import * as firebase from '@firebase/app'

after formatting it, I was able to access messaging from firebase.firebase.messaging()

Related