firebase-tools: Firebase Emulator simply does not initialize .env || .env.local || .env.default
Related issues
https://github.com/firebase/firebase-functions/issues/1084
[REQUIRED] Version info
**node:**v14.17.6
**firebase-functions:**3.20.1 **firebase-tools:**10.6.0
**firebase-admin:**10.1.0
[REQUIRED] Test case
- Create local firebase project
- insert and populate .env, .env.local, .env.default (for good measure)
- start emulator suite
- log process.env
[REQUIRED] Steps to reproduce
[REQUIRED] Expected behavior
For process.env to have been populated with local .env values.
[REQUIRED] Actual behavior
.env values are nowhere to be found.
Were you able to successfully deploy your functions?
Technically, yes.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 5
- Comments: 37 (6 by maintainers)
Thanks, everyone for your research. I faced the same issue when locally “firebase-functions” did not get the environment variables. Maybe this will help somebody while we are waiting to fix this issue:
npm install dotenv --save
Now you can access your
.env
usingprocess.env.AWESOME_VAR_NAME
UPDATE: In my case, I found out how to solve the issue without using the “dotenv” package. I have added an env variable named
FIREBASE_PROJECT_NAME
, and this was a mistake, I used reserved environment variables. One of the rules says, Do not use any of these keys in your .env files: “All keys starting with FIREBASE_”. When I removedFIREBASE_PROJECT_NAME
variable from the.env
file everything start working locally as expected.My testing so far has shown that all variables I define inside my .env files are available inside my function body, but only inside them.
So if I have an condition in my
index.ts
, where the function exports are defined, thenprocess.env
will only log the google firebase environment variables. But if I log the same process.env again inside one of my functions, then the correct and expectedprocess.env
content, which is defined in my.env
-file will be printed.This is somewhat understandable, as firebase does only include the .env file(s) in the package that gets uploaded and deployed. So the actual building step uses the host system variables which has started the build/deploy process. Be it a pipeline or the local shell on your computer.
But now the strange, and to me unexplainable, part starts.
If I run for example this command:
export NODE_ENV=production && export TEST=test && export STAGE=stage && firebase emulators:start --only functions
and put this logging near the top of my index.ts file:this is the output:
and if I only start it without any exports, like so:
firebase emulators:start --only functions
then the output changes to this:
I have no idea why only the NODE_ENV survives and everything else gets lost. Given, there are still a lot of firebase related variables if I print my whole
process.env
but I have not idea where to look further for some explanation why my host system environment variables are mostly ignored, apart from NODE_ENV.I also tested to start it with a random NODE_ENV value, to make sure its not from some build process and pure coincidence:
export NODE_ENV=foobar && export TEST=test && export STAGE=stage && firebase emulators:start --only functions
That resulted in this output:
If anyone has any clues on what is wrong here or how this can get fixed I would be very grateful.
It’s been two years guys, how can this not be fixed yet? It’s a major problem for testing with multiple environments.
I had to add
import "dotenv/config" // Load environment variables from .env file
in my index.js file (source of my functions) in order to actually load the env variablesConfirmed this is still an issue.
I’m running
firebase-tools 11.24.0
.The Functions Emulator would not load the
.env
files intoprocess.env
before the function bootstraps even when I see this logged to the console:In some places it worked if I added this to my
./index.js
root/entry file:But this doesn’t work:
Which seems to defeat the purpose of the autoloading mechanism that’s documented here:
https://firebase.google.com/docs/functions/config-env#emulator_support
and here:
https://firebase.google.com/docs/emulator-suite/connect_functions#configure_a_local_testing_environment
The .env is not loaded when using the --import option.
firebase emulators:start --export-on-exit --import <my-folder>
.When using only
firebase emulators:start
, it is working fine.My code will throw an exception if that environment variable in not defined, and that’s what is happening during the analysis.
Running into the same issue here.
@polisen Can you please re-open this issue? The bot closed it for inactivity but since then more people complained, including me.
I’m having the same problem. I opened a SOF issue there and then I found this GH issue so I thought I would share. The problem seems to be that environment variables are not being loaded during the deployment process. We should have a way to tell the deployment process to load the dotenv files.
(BTW @nerder and @jhonatanoliveira you guys are talking about a very different problem…)
Update: I confirm the following snippet:
will produce the following trigger details in Google Cloud Console
I’ve encountered the same issue when
--import
is used. Only.env
is loaded,.env.local
is ignored. Any workaround?I am facing the same issue where I need to load different environment variables to trigger my cloud function according to my projectId. However, I could access the env variables inside the function body.
eg:
.env variables are only accessible from INSIDE the function ! (not outside, meaning inside onRequest( ( request, response ) => {…} ). That’s it.
As the doc says : “Once your custom environment variables are deployed, your function code can access them…”
@polisen Can you leave a comment here so the bot will reopen the issue? I can confirm it is still happening with firebase-functions@4.2.1 and firebase-tools@11.25.2
I can confirm that the folder is located within functions. The .env format is also correct. I have rimraffed and re-installed everything, reconfigured emulators, removed .runtimeconfig.json but no dice.