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

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();

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