Firebase Function Environment Variables

April 3, 2024 ยท View on GitHub

Overview

Firebase functions can make use of environment variables in their runtime.

The Nx-firebase application generator will automatically create default function environment files for you to use:

  • environment/.env
  • environment/.env.local
  • environment/.secret.local

If you run the getconfig target, it will place .runtimeconfig.json file in the environment folder also.

These environment files are considered common to all of your functions by nx-firebase, and are copied from your firebase app's environment folder to your function's dist folder when the function is built.

This ensures they are available for deployment and emulation.

All functions share the same environment variable files.

Environment file types

FileDescriptionGit IgnoredDeployed
.envGeneral environment variables for functions-Yes
.env.localEnvironment variable overrides for function emulation--
.env.<project-alias>Environment variable overrides for specific deployment targets (eg. dev/prod)-Yes
.secret.localSecrets only for function emulationYes-
.runtimeconfig.jsonFunction configurationsYes-

Note that the Firebase team appear to be deprecating the use of .runtimeconfig.json function configs and recommending migration to dotenv environment variables.

Deployed Files

The firebase CLI will deploy .env and/or .env.<project-id> files along with function code, and they can be version controlled.

Non-deployed files

.env.local and .secret.local files are excluded from deployment by using an functions.ignore rule in firebase.json.

Local Files

.secret.local and .env.local are only used by the Firebase emulator suite.

.secret.local and .runtimeconfig.json should not be version controlled, and are both git ignored, but included in Nx dependency graph using a .nxignore override.