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)

Most upvoted comments

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:

  1. Install dotenv. npm install dotenv --save
  2. Use dotenv package in your function(for instance in index.ts) to load environment variables:
import { api } from './http';
import dotenv from 'dotenv';

dotenv.config();

export {
  api,
}

Now you can access your .env using process.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 removed FIREBASE_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, then process.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 expected process.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:

console.log('NODE_ENV', process.env.NODE_ENV )
console.log('TEST', process.env.TEST )
console.log('STAGE', process.env.STAGE )

this is the output:

NODE_ENV production
TEST undefined
STAGE undefined

and if I only start it without any exports, like so: firebase emulators:start --only functions

then the output changes to this:

NODE_ENV undefined
TEST undefined
STAGE undefined

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:

NODE_ENV foobar
TEST undefined
STAGE undefined

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 variables

Confirmed this is still an issue.

I’m running firebase-tools 11.24.0.

The Functions Emulator would not load the .env files into process.env before the function bootstraps even when I see this logged to the console:

functions: Loaded environment variables from .env, .env.<projectId>, .env.local. 

In some places it worked if I added this to my ./index.js root/entry file:

import dotenv from 'dotenv';

dotenv.config();

But this doesn’t work:

export const api = functions
  .region(process.env.region)
  .https.onRequest(app);

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.

deployment is failing due to the code requiring an environment variable to exis

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:

export const myfunction = onDocumentCreated(
	`users/{uid}/${process.env.TEST_KEY}/{id}`,
	(event) => {
		console.log(process.env.TEST_KEY);
	}
);

will produce the following trigger details in Google Cloud Console

image

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:

functions.region(process.env.REGION).storage.bucket().object(process.env.CUSTOM_BUCKET).onDelete((obj)=>{
    console.log(process.env.CUSTOM_BUCKET) // This will print the value of CUSTOM_BUCKET, But the trigger is not set on the function
})

.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.