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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay