Skip to content

Instantly share code, notes, and snippets.

@devvspaces
Created August 4, 2026 21:02
Show Gist options
  • Select an option

  • Save devvspaces/fe283208d4dbd1ab97dfc0e6e0bdea4c to your computer and use it in GitHub Desktop.

Select an option

Save devvspaces/fe283208d4dbd1ab97dfc0e6e0bdea4c to your computer and use it in GitHub Desktop.
Make firebase admin work with nestjs
import { App, initializeApp, cert, getApps } from 'firebase-admin/app';
import { getMessaging, Message } from 'firebase-admin/messaging';
import { ServiceAccount } from 'firebase-admin';
// Ensure gaxios (used by firebase-admin & google-auth-library) uses Node native fetch
function patchGaxiosClass(Gaxios: any) {
if (Gaxios && !(Gaxios.prototype as any).__fetchPatched) {
(Gaxios.prototype as any).__fetchPatched = true;
const originalRequest = Gaxios.prototype.request;
Gaxios.prototype.request = function (opts: any) {
opts = opts || {};
opts.fetchImplementation = opts.fetchImplementation || this.defaults?.fetchImplementation || globalThis.fetch;
return originalRequest.call(this, opts);
};
}
}
try {
const fbPath = require.resolve('firebase-admin');
const gauthPath = require.resolve('google-auth-library', { paths: [fbPath] });
const gaxiosPath = require.resolve('gaxios', { paths: [gauthPath] });
patchGaxiosClass(require(gaxiosPath).Gaxios);
} catch {}
try {
patchGaxiosClass(require('gaxios').Gaxios);
} catch {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment