firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

Project-specific .env files aren't loaded when running functions emulator

Open larrifax opened this issue 2 years ago • 18 comments

[REQUIRED] Environment info

firebase-tools: 10.2.1

Platform: Ubuntu

[REQUIRED] Test case

The documentation around environment variables using .env-files while using the local functions emulator states the following:

Contents of .env.local take precedence over .env and the project-specific .env file Source: https://firebase.google.com/docs/functions/config-env#emulator_support

However, when running locally the project-specific .env file isn't even loaded, only the .env and .env.local is. And indeed, the source code doesn't even attempt to load project-specific files when running in an emulator setting:

https://github.com/firebase/firebase-tools/blob/5a8bd4e4337536821a258fa7d14cdc11d431c478/src/functions/env.ts#L199-L206

According to my interpretation of the documentation linked above, there's a mismatch between the documentation and the implementation, which should be resolved one way or another.

[REQUIRED] Steps to reproduce

  1. Add a .env, .env.development and .env.local file to your project, filling them with different environment variables
  2. firebase use development
  3. firebase emulators:start --only functions
  4. Trigger a function

[REQUIRED] Expected behavior

Functions emulator is started, and logs should show that all relevant .env* files are loaded: i functions: Loaded environment variables from .env, .env.development, .env.local.

[REQUIRED] Actual behavior

Functions emulator is started, but logs shows that the project specific .env file isn't loaded (.env.development is missing from output): i functions: Loaded environment variables from .env, .env.local.

larrifax avatar Mar 02 '22 11:03 larrifax

I'm seeing this same behavior. I interpreted the documentation the same as you did. Looking at the docs again, it could be interpreted in a way that matches the implementation where they don't intend the project-specific configuration to ever be used in the emulator. That fact isn't super clear, but I think the wording of the section offers a clue: "Deploying multiple sets of environment variables". "Deploying" in this case would suggest they are not talking about the emulator.

I'm thinking that now that I understand this, I can make the current implementation work for my purposes. But, I agree that at a minimum the documentation should probably be updated to clarify this fact.

jaytavares avatar Mar 02 '22 18:03 jaytavares

Thanks for a thorough writeup. I think the documentation is right and the implementation is wrong. We will work on a fix to be shipped in the next release.

taeold avatar Mar 02 '22 23:03 taeold

Thanks @taeold. This is maybe another bug/feature request, but I also noticed that .env variables aren't available when the "startup code" is ran, so it's currently not possible to e.g. store the region in an environment variable and use it like so:

functions/index.js:

import * as functions from "firebase-functions";
import * as callable from "./callables";

export const someFunction = functions.region(process.env.REGION).https.onCall(callable.someFunction);

The environment variables aren't available to use at the time when .region() or .runWith() are evaluated. They're only available "within onCall".

Is this something you're aware of/decided not to support? Or should I create a new bug/feature request for it?

larrifax avatar Mar 10 '22 07:03 larrifax

Thanks @taeold. This is maybe another bug/feature request, but I also noticed that .env variables aren't available when the "startup code" is ran, so it's currently not possible to e.g. store the region in an environment variable and use it like so:

functions/index.js:


import * as functions from "firebase-functions";

import * as callable from "./callables";



export const someFunction = functions.region(process.env.REGION).https.onCall(callable.someFunction);

The environment variables aren't available to use at the time when .region() or .runWith() are evaluated. They're only available "within onCall".

Is this something you're aware of/decided not to support? Or should I create a new bug/feature request for it?

I've had to remove my dynamic exports because of this. It would be nice to have this functionality back

jaytavares avatar Mar 10 '22 14:03 jaytavares

In my case, when I start the emulator the message below is not even showing:

i functions: Loaded environment variables from .env, .env.local.

pradella avatar Mar 11 '22 19:03 pradella

https://github.com/firebase/firebase-tools/issues/4239#issuecomment-1063763689

Hi @taeold, could you comment on the above point? I’m also hoping .env variables are available on .region() or .runWith() are evaluated

jtfrom9 avatar Mar 17 '22 11:03 jtfrom9

@taeold Hi. This is still not working for me locally. The variables are loaded from .env if I remove the .env.local file but the variables are not loaded at all if I add a .env.local file. I'm also importing variables from Secrets Manager which is working fine. Is this suppose to be working now?

michaelalhilly avatar May 25 '22 20:05 michaelalhilly

Not quite the described issue, but may help those who landed here searching for a related issue.

If any key in .env contains any lowercase characters, the entire .env is ignored by emulator, and firebase deploy --only functions will produce this error:

Key X_lowercase_Key must start with an uppercase ASCII letter or underscore, and then consist of uppercase ASCII letters, digits, and underscores.

Please hide/delete if irrelevant.

sartoshi-foot-dao avatar Jun 06 '22 12:06 sartoshi-foot-dao

.env variables are only available during application runtime, and not when you are deploying. You have to use Firebase's remote config if you want to use env variables when initializing functions, e.g. functions.config().environment.isDev.

erikkjernlie avatar Aug 05 '22 10:08 erikkjernlie

.env variables are only available during application runtime, and not when you are deploying. You have to use Firebase's remote config if you want to use env variables when initializing functions, e.g. functions.config().environment.isDev.

This is the old method for environment variables and what I used in previous projects. However, though not directly said in the docs, their language about migrating to the new .env method and how "config" is removed from V2 cloud functions makes me worry that it may be deprecated in the not too distant future.

My hope is that this is an oversight as @larrifax mentioned.

EDIT: The docs also suggest that if you have V2 cloud functions alongside V1 functions then deploys will fail if using "config()";

juane1000 avatar Aug 22 '22 21:08 juane1000

Why is this closed ?

You cannot deploy to v2 of functions when using functions.config(), and that seems to be the only solution for accessing any deployment variables. This is a big issue

Wian-TMC avatar Aug 24 '22 15:08 Wian-TMC

Same here, my .env or .env.local files are not loaded when I start the emulator

NickVanzo avatar Sep 16 '22 07:09 NickVanzo

.env variables are loaded into the application runtime (after the configuration in the beginning), so you need to use remoteConfig for using env variables during deploy :)

erikkjernlie avatar Sep 16 '22 22:09 erikkjernlie

.env variables are loaded into the application runtime (after the configuration in the beginning), so you need to use remoteConfig for using env variables during deploy :)

Please refer here for a rationale of the previous, current and future solution. Please note, Remote Config is deprecated and cannot be used when deploying v2 functions.

https://github.com/firebase/firebase-functions/issues/1084

Wian-TMC avatar Sep 19 '22 07:09 Wian-TMC

Thanks for reopening this. It would be great if this could work as expected.

michaelalhilly avatar Sep 19 '22 20:09 michaelalhilly

IMPORTANT NOTE

Please verify that you did not set any variable using a reserved name.

If you do that, the emulator will just fail silently upon loading the functions and not load your .env[.*] files.

Some of those reserved names could be "popular", such as "All keys starting with FIREBASE_".

edonosotti avatar Sep 29 '22 10:09 edonosotti

firebase/firebase-functions#1084

You're missing the point of the issue at hand. Please refer to https://github.com/firebase/firebase-functions/issues/1084

Wian-TMC avatar Sep 30 '22 09:09 Wian-TMC

any news on this ?

irux avatar Oct 15 '22 10:10 irux

Same issue here - how are we supposed to migrate to functions V2 if we can't use .env files in the root?

Joebayld avatar Nov 25 '22 10:11 Joebayld

Just ran into this. Very painful having to deploy my functions just to see if they work, despite having a perfectly good local emulator.

You can kind of hack around it by bringing in dotenv and loading the files if NODE_ENV === development. I actually was doing this first, then read the docs so I removed it, only to find it was broken.

EDIT Turns out I am an idiot and had a FIREBASE_ setting in my .env file. Removing it fixed the issue, though I still don't get the console message informing me that the env files have been loaded.

bruceharrison1984 avatar Dec 03 '22 21:12 bruceharrison1984

EDIT Turns out I am an idiot and had a FIREBASE_ setting in my .env file. Removing it fixed the issue, though I still don't get the console message informing me that the env files have been loaded.

If you're having problem with process.env in firebase functions this should help

luicfrr avatar Dec 19 '22 14:12 luicfrr

I had this same problem - removed a FIREBASE_xxx variable from my .env file and the variables loaded. Unfortunate an exception isn't thrown when the documented policy is unknowingly violated. Variables from the .env file are loaded at runtime, so if you attempt to call your function from the shell for example, you should see the following in the console:

firebase > myFunction({ test: '123' })
Sent request to function.
firebase > i  functions: Loaded environment variables from .env.

myktra avatar Dec 19 '22 15:12 myktra

Unsure if related, but firebase functions:shell doesn't load .env.local file. It says it loads .env, and the values that are present on process.env confirm it.

knownasilya avatar Mar 05 '23 04:03 knownasilya

Hi all, thanks for helping us and others diagnose issues with loading environment variables in the functions emulator. Two things:

  1. Parameterized configuration is now the recommended way to configure your environment variables. Please refer to the docs here for instructions on how to use params. Check out the thread here for context about the params feature (thanks to @Wian-TMC for sharing).
  2. I will be closing this issue, as the original error has been resolved in #4273. If you are still encountering issues with configuring your environment, please open a new issue and provide instructions on how to reproduce your error.

blidd-google avatar Mar 10 '23 21:03 blidd-google

This is still broken @blidd-google , please consider re-opening. The .env.local method for overriding environment variables on a development workstation is broken.

Details: https://github.com/firebase/firebase-tools/issues/6499

ekalosak avatar Sep 28 '23 21:09 ekalosak

Currently facing this issue as well and I believe that this started failing after the creation of several aliases. None of my .env files are being read in the emulator, not even in the body of the function. I worked around the issue by installing the dotenv package. Not ideal but couldn't waste anymore time looking into this.

Stf-F avatar Oct 10 '23 22:10 Stf-F

At the moment my project alias named env files are not being used, I have .env.dev & .env.prod, but at the moment I need to copy the contents into a .env file before deploying to dev or prod

conor909 avatar Dec 15 '23 10:12 conor909

Same issue here, the deployment is failing due to the code requiring an environment variable to exists. The variable is already defined in the .env and no reserved name is being used in the file. Still the deployment does not print the below line (which is documented in Firebase)

$ firebase deploy --only functions
# ...
# i functions: Loaded environment variables from .env.
# ...

Using "firebase-functions": "^4.6.0" & "firebase-tools": "^13.0.2"

a7md0 avatar Jan 06 '24 08:01 a7md0

Hi, I need to setup a custom environmental variable so that a private npm package that we include into the firebase/functions can configure itself accordingly however it seems like there is no single way for configuring environmental variables in gen2?

mustafaekim avatar Feb 06 '24 16:02 mustafaekim

Hi, here Not sure if this can help. I don't run emulator, I run deploy directly, and what I found is working for me.

Move the .env* files out of functions directly to the root of your directory, and everything works.

In my case, I have 3 .env files: .env, .env.beta, .env.prod with the command firebase use beta && ... && firebase deploy ..., the log will show the line:

i  functions: Loaded environment variables from .env, .env.beta.

instead of

i  functions: Loaded environment variables from .env.

rzfang avatar May 02 '24 09:05 rzfang