DEV Community

jacob30
jacob30

Posted on

Firebase local tips

If you're getting the following error error: Failed to parse private key: Error: Invalid PEM formatted message when using Firebase locally. Try replacing the following with the following:

export class Database {
    public database: admin.database.Database;

    constructor() {
        const app = admin.apps.find((it: any) => it?.name === "[DEFAULT]") ||
            admin.initializeApp({
                credential: admin.credential.cert({
                    projectId: config.FIREBASE_PROJECT_ID,
                    clientEmail: config.FIREBASE_CLIENT_EMAIL,
                    privateKey: config.FIREBASE_PRIVATE_KEY!.replace(/\\n/gm, "\n"),
                }),
                databaseURL: config.FIREBASE_DATABASE
            });

        this.database = getDatabase(app);
    }
Enter fullscreen mode Exit fullscreen mode
export class Database {
  public database: admin.database.Database;

  constructor() {
    const app = admin.initializeApp({
      credential: {
        getAccessToken: () =>
          Promise.resolve({ access_token: 'foo', expires_in: 3600 }),
      },
      databaseURL: `${config.FIREBASE_URL_WITH_PORT}/?ns=demo-yourprojectname`,
    });
 this.database = getDatabase(app);
  }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)