Accepted answer

You haven't passed your config object to admin.initializeApp

Since you use dotenv to load env files, you can create a config object with env details like

const config = {
    type: "service_account",
    project_id: "xxx",
    credential: 'your credentials'

  db = admin.firestore();

If you are not passing any config to initializeApp then you need to define it FIREBASE_CONFIG env object.

According to the documentation:

The SDK can also be initialized with no parameters. In this case, the SDK uses Google Application Default Credentials and reads options from the FIREBASE_CONFIG environment variable. If the content of the FIREBASE_CONFIG variable begins with a { it will be parsed as a JSON object. Otherwise the SDK assumes that the string is the name of a JSON file containing the options.


It looks like you might be missing process.env.FIREBASE_SECRET (unless you've purposefully not included it since it's a private key). I should also point out that .env* files are not suitable for storing private keys since they are bundled in your application code and are publicly accessible.

There are a couple of ways that you can initialise the admin SDK. They are outlined on the Add the Firebase Admin SDK to your server reference guide. It looks like you are attempting to initialise the SDK without parameters, which is covered here. Basically, you need to set an environment variable (via export in your terminal, not .env files) called FIREBASE_CONFIG which is either a path to a JSON file containing your config information or is a JSON object containing your config information.

Good luck!

Related Query

More Query from same tag