0
0
Firebasecloud~5 mins

Environment configuration in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you build apps with Firebase, you often need to set different settings for development and production. Environment configuration helps you keep these settings organized and separate so your app behaves correctly in each place.
When you want to use different Firebase projects for testing and live app without mixing data.
When you need to store API keys or URLs that change between your local machine and the cloud.
When you want to switch easily between staging and production environments without changing code.
When you want to keep sensitive information out of your main codebase.
When you want to deploy your app with the right settings automatically.
Config File - .firebaserc
.firebaserc
{
  "projects": {
    "default": "my-app-dev",
    "production": "my-app-prod"
  }
}

This file tells Firebase CLI which projects to use for different environments. The default project is used when no environment is specified. The production project is for your live app.

Commands
This command sets the active Firebase project to the development project so your commands affect the dev environment.
Terminal
firebase use my-app-dev
Expected OutputExpected
Now using alias my-app-dev (my-app-dev).
Deploys your app's hosting files to the currently active Firebase project, which is the development environment here.
Terminal
firebase deploy --only hosting
Expected OutputExpected
=== Deploying to 'my-app-dev'... i deploying hosting ✔ hosting: my-app-dev deployed successfully ✔ Deploy complete!
--only - Limits deployment to specific services like hosting.
Switches the active Firebase project to the production environment to deploy live app changes.
Terminal
firebase use production
Expected OutputExpected
Now using alias production (my-app-prod).
Deploys your app's hosting files to the production Firebase project for live users.
Terminal
firebase deploy --only hosting
Expected OutputExpected
=== Deploying to 'my-app-prod'... i deploying hosting ✔ hosting: my-app-prod deployed successfully ✔ Deploy complete!
--only - Limits deployment to specific services like hosting.
Key Concept

If you remember nothing else, remember: use Firebase project aliases to switch environments easily and keep your development and production settings separate.

Common Mistakes
Deploying without switching to the correct Firebase project alias.
Your app will deploy to the wrong environment, causing confusion or data mix-up.
Always run 'firebase use <alias>' to select the right environment before deploying.
Hardcoding API keys or URLs directly in the app code.
This makes it hard to change settings per environment and risks exposing sensitive info.
Store environment-specific settings in Firebase config or environment files and access them dynamically.
Summary
Use the .firebaserc file to define project aliases for different environments.
Switch environments with 'firebase use <alias>' before deploying.
Deploy hosting or other Firebase services to the selected environment with 'firebase deploy'.