How do I switch apps from the firebase cli?

FirebaseFirebase Tools

Firebase Problem Overview


This seems like something which should be pretty easy to do, but for whatever reason, I'm being defeated.

I'm trying to use the firebase-tools CLI to interact with my database. I'm able to login without any trouble, and when I type firebase list, I get a list of all my current apps. It also tells me which app I'm currently connected to.

My problem is, I want to connect to one of the other apps. I'm running queries on my staging app, and I need to run them on my production app. I can see the production app in the list, but I'm not finding any way to switch to that app.

Thoughts?

Firebase Solutions


Solution 1 - Firebase

Found some useful information here Firebase CLI Reference.

The following code works for me.

firebase use <project_id>

Solution 2 - Firebase

I rather use scripts. Consider a project structure like this:

your-project
├── .firebaserc
└── functions
   ├── package.json
   └── index.js

Go to .firebaserc and follow the next example

{
  "projects": {
    "default": "project-name",
    "prod": "other-name"
  }
}

Then go to package.json and add the following scripts (changeToProd, and changeToDev).

{
  ...
  "scripts": {
    ...
    "changeToProd": "firebase use prod",
    "changeToDev": "firebase use default"
  },
  "dependencies": {
    ...
  },
  ...
}

If your IDE support npm scripts you can run them using the IDE UI, otherwise it can be run using the command console. Make sure you are inside the functions folder.

npm run-script changeToProd

You can verify your current project by running the following command from the terminal or added to the scripts as we just did

firebase use

Solution 3 - Firebase

If you are using Node.js on windows, your answer should be

firebase use <project_id>

but without the <> for example

firebase use chat-app-2a150

You can use the following code to view all your projects so as to pick the correct project ID to use

firebase projects:list

Solution 4 - Firebase

2020:

The officially recommended way is to use "alias":

In your .firebaserc, set different project IDs like this:

{
  "projects": {
    "production": "my-project-id",
    "testing": "my-testing-project-id"
  }
}
// you can also add them interactively with `firebase use --add`

Then switch projects in CLI with firebase use testing , firebase use production.

Note: switching projects won't create any git diff, it's simply remembered in your local machine. Use firebase use to see which project is currently being used.

Uncommon cases:

  • If you want to use your own ID without committing changes to the project owner's .firebaserc, do firebase use my-own-id locally as mentioned in the accepted answer.
  • If you want people to fork your code then use their own IDs, add .firebaserc into .gitignore.

Solution 5 - Firebase

In the directory where you run firebase list, there will be a file called firebase.json. If you open that in a text editor, you will see the app name in there. You can change it there or delete firebase.json to change the app.

Or save yourself the hassle of editing a text file and do as Jason says: use firebase init.

Solution 6 - Firebase

you can just use a command line switch

--project=my-firebase-project

Solution 7 - Firebase

Addition @cutiko's answer

In package.json

"scripts": {
...
"prod": "echo \"Switch to Production environment\" && firebase use prod && npm run runtimeconfig",
"dev": "echo \"Switch to Development environment\" && firebase use default && npm run runtimeconfig"
...

npm run runtimeconfig to get custom config environment

In .firebaserc

{
  "projects": {
    "default":"{project-dev-id}",
    "prod": "{project-prod-id}"
  }
}

Solution 8 - Firebase

to change firebase app destination project you can type "firebase use myProjectName" . i also used the above answeres "firebase list" to check what project i have ( work for me in firebase cli 7.4 with angular 7 app)

Solution 9 - Firebase

I may be wrong but it seems that the original question was about changing apps within a given project, rather than simply changing projects.

This answer is about changing apps and site_IDs within a project.

In my case I have a project (CoolProject) with 2 web apps:

  • an assessment form: form
  • a main website: website

Both apps are in separate repos both locally and in GitHub.

Each app has its own specific site_ID:

  • form: coolproject-form[.web.app]
  • website: coolproject-website[.web.app]

I first setup the form app and deployed without any issue to coolproject-form. But when I created the web app (and associated coolproject-website site_ID) and tried to deploy it using firebase deploy --only hosting or firebase deploy --only hosting:website it incorrectly deployed it to coolproject-form overwriting the form app.

This is how I eventually solved the issue (based on this Firebase documentation):

  1. Check that both apps and corresponding site_IDs are correctly setup:
firebase apps:list
firebase hosting:sites:list
  1. Setup up the website deploy target for hosting (via .firebaserc)
firebase target:apply hosting website coolproject-website
  1. Update firebase.json (for the website app):
...
"hosting": [{
    "target": "website",
    "public": "build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }],
...
  1. Deploy
firebase deploy --only hosting

With this the website app is now correctly deployed to coolproject-website.web.app.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionwvm2008View Question on Stackoverflow
Solution 1 - FirebaseJ100View Answer on Stackoverflow
Solution 2 - FirebasecutikoView Answer on Stackoverflow
Solution 3 - FirebaseEngr. Meshach KoechView Answer on Stackoverflow
Solution 4 - FirebaseZYinMDView Answer on Stackoverflow
Solution 5 - FirebaseFrank van PuffelenView Answer on Stackoverflow
Solution 6 - FirebaseRon ChanView Answer on Stackoverflow
Solution 7 - FirebaseGiangView Answer on Stackoverflow
Solution 8 - Firebaseyehonatan yehezkelView Answer on Stackoverflow
Solution 9 - FirebaseSamHeymanView Answer on Stackoverflow