FirebaseError: Failed to load environment variables from .env
Asked Answered
I

6

8

I am using Firebase to run the backend for my application. When I try to run the backend using firebase emulators:start --inspect-functions it says

Node v: 18.13.0
Firebase-tools v: ^11.18.0
Firebase-functions v: ^4.2.0

i  emulators: Starting emulators: auth, functions, storage
⚠  functions: You are running the Functions emulator in debug mode (port=9229). This means that functions will execute in sequence rather than in parallel.
⚠  functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: firestore, database, hosting, pubsub
⚠  functions: Unable to fetch project Admin SDK configuration, Admin SDK behavior in Cloud Functions emulator may be incorrect.
i  ui: Emulator UI logging to ui-debug.log
i  functions: Watching "/home/ktk/code/js/sol/functions/functions" for Cloud Functions...
⚠  functions: package.json indicates an outdated version of firebase-functions. Please upgrade using npm install --save firebase-functions@latest in your functions directory.
✔  functions: Using node@18 from host.
⬢  functions: Failed to load function definition from source: FirebaseError: Failed to load environment variables from .env.

Things I've checked:

  • the .env does exist
  • the .env syntax is correct
  • executed chmod 777 .env to change the permissions
Imprimatur answered 15/4, 2023 at 9:52 Comment(3)
there may be an issue with permissions - ensure the .env file is writable.Euridice
I did chmod 777 .env and still the error persistsImprimatur
Based on #76038479 it looks like you resolved this issue. What was the fix?Cobby
S
16

The problem here could be that you are using reserved firebase names for your environment variable keys.You can take a look to the Firebase Config Env documentation.

Any key that starts with the word FIREBASE is reserved.

I had the same problem, and I could solve it by changing the name of my env variables.

I hope that helps. Good luck! :)

Sabba answered 12/5, 2023 at 11:19 Comment(1)
Ivan is correct. My error said Error: Failed to load environment variables from .env.: but what I failed to do was look below it where it also said Error Key FIREBASE_DATABASE_URL starts with a reserved prefix (X_GOOGLE_ FIREBASE_ EXT_) along with several other keys that I used. I simply changed the prefix to MYFIRE_DATABASE_URL and made the same update in the my index.js file as in admin.initializeApp({ ... databaseURL: process.env.MYFIRE_DATABASE_URL,... and everything worked. Thanks for the answer :-)Cruciform
J
4

I was using the "PORT" key in my .env file. This key is in the list of reserved Firebase keys.

Really wish the error message was more helpful.

Jinn answered 30/8, 2023 at 18:6 Comment(0)
F
2

I did some testing here, as I was getting the same error. Another cause of this terrible unreadable error message is having any env keys that are not in UPPERCASE="".

For example, a key like this: APP_projectId_API_KEY="foo" will cause this error.

Dotenv files aren't meant to have this restriction. By convention, app/environment constants are written in uppercase, but this should be a choice - see: Why are Node js environment variables written in all caps and underscores?

Follicle answered 17/6, 2023 at 13:37 Comment(0)
T
1

Check the variables . env, do not use reserved environment variables that use Firebase. List of reserved environment: https://firebase.google.com/docs/functions/config-env?gen=2nd#reserved-names

Tommyetommyrot answered 2/2 at 0:53 Comment(0)
N
0

I got this too. I resolved it by taking the quotes around the variable names

it went from

"MY_SPECIAL_ID"="myidvalue"

to

MY_SPECIAL_ID="myidvalue"

looks like there are a lot of tiny specificities in the .env files that are not given well enough in the errors.

Neurilemma answered 15/9 at 14:5 Comment(1)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewHemi
E
-1

Any keys starting with FIREBASE_ will cause an error.

FIREBASE_API_KEY=XYZ123
FIREBASE_AUTH_DOMAIN=mydomain.firebaseapp.com 
FIREBASE_DATABASE_URL=https://mydatabase.firebaseio.com 
etc...
Euridice answered 15/4, 2023 at 10:25 Comment(3)
I have checked everything you said, i do not have a .npmignore. and I removed the env form the gitignore as well. It also doesnt have spaces but still the error persistsImprimatur
The error message indicates that there's a problem with loading environment variables from the .env file. you have to be sure .env file exists at the root get console.log for this, or if this is possible as your error log shows update the package or even restart simuloatorEuridice
Pleases revise this answer - any keys starting with FIREBASE_ will cause an error.Follicle

© 2022 - 2024 — McMap. All rights reserved.