0

Problem

As far I know, Firebase Cloud Functions natively support .env files, without installing any third-party package like dotenv.

I have created a .env file, inside my functions directory:

/app

/functions
   .env
   .eslintrc.js
   package.json
   /src
     ...

Inside my .env file, I have the following:

FIREBASE_PROJECT_ID=aaaaa
FIREBASE_KEY_ID=bbbbb
FIREBASE_PRIVATE_KEY=-----BEGIN PRIVATE KEY-----\nxxxxxxxx\n-----END PRIVATE KEY-----\n
FIREBASE_CLIENT_EMAIL=zzzzz
FIREBASE_CLIENT_ID=ddddd
FIREBASE_AUTH_URI=qqqqqq
FIREBASE_TOKEN_URI=jjjjjj
FIREBASE_AUTH_PROVIDER_X509_CERT_URI=lllllll
FIREBASE_CLIENT_X509_CERT_URI=nnnnnnnn
FIREBASE_STORAGE_BUCKET_URI=mmmmmmm

For some reason, when I try to deploy my functions, I get the error:

Error: Error occurred while parsing your function triggers. The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.


Admin SDK initialization

This is how I am initializing the Firebase Admin SDK:

try {
  const googleCloudServiceAccount = require("./credentials");

  admin.initializeApp({
    credential: admin.credential.cert(googleCloudServiceAccount),
    storageBucket: process.env.FIREBASE_STORAGE_BUCKET_URI,
  });
} catch (err) {
  functions.logger.error(`Error initializing firebase: ${err}`);
}

// Initialize Firebase Services
const auth = admin.auth();
const firestore = admin.firestore();
const storage = admin.storage();

// Storage Bucket
const storageBucket = storage.bucket(process.env.FIREBASE_STORAGE_BUCKET_URI);

credentials.js

module.exports = {
  type: "service_account",
  project_id: process.env.FIREBASE_PROJECT_ID,
  private_key_id: process.env.FIREBASE_KEY_ID,
  private_key: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, "\n"),
  client_email: process.env.FIREBASE_CLIENT_EMAIL,
  client_id: process.env.FIREBASE_CLIENT_ID,
  auth_uri: process.env.FIREBASE_AUTH_URI,
  token_uri: process.env.FIREBASE_TOKEN_URI,
  auth_provider_x509_cert_url: process.env.FIREBASE_AUTH_PROVIDER_X509_CERT_URI,
  client_x509_cert_url: process.env.FIREBASE_CLIENT_X509_CERT_URI,
};

What am I doing wrong?

-Note: if instead of using .env and a credentials.js file, I just hardcode all my secrets and relative stuff inside a credentials.json file, everything works fine.

1 Answer 1

1

After globally upgrading my firebase-tools and avoid using reserved prefix everything is working as expected!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.