'How do I switch apps from the firebase cli?

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?



Solution 1:[1]

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 2:[2]

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 3:[3]

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 4:[4]

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 5:[5]

you can just use a command line switch

--project=my-firebase-project

Solution 6:[6]

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 7:[7]

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.

Solution 8:[8]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cutiko
Solution 2 Engr. Meshach Koech
Solution 3
Solution 4
Solution 5 Ron Chan
Solution 6 Giang
Solution 7
Solution 8 yehonatan yehezkel