183

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?

wvm2008
  • 2,864
  • 4
  • 21
  • 25

9 Answers9

358

Found some useful information here Firebase CLI Reference.

The following code works for me.

firebase use <project_id>
J100
  • 3,896
  • 2
  • 11
  • 18
44

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
cutiko
  • 9,887
  • 3
  • 45
  • 59
38

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
35

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.
ZYinMD
  • 3,602
  • 1
  • 23
  • 32
17

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Is there a change now? Because I cannot find the app name in firebase.json but can find it in .firebaserc.... Appreciate the clarification... – Abhishek Nov 25 '17 at 12:15
  • 2
    The project name is indeed in `.firebaserc` now. – Frank van Puffelen Nov 25 '17 at 15:48
  • 2
    Might be wrong but this doesn't seem to switch the database. Deploying will deploy to "current" project but it seems to still pull data from original database. Pretty damn dangerous if you're going use this to separate production from staging environments. Hope no one loses their prod data. :( – Enric Ribas Feb 26 '18 at 11:43
  • 1
    I'm having the same issue that @Enric described about accessing the default database while being on a different project. Is there some way to detect what project is being used on the client side so that I can change the config to initialize the app appropriately? – Zectbumo Apr 20 '19 at 03:34
  • make sure you change firebaseConfig ` // Your web app's Firebase configuration var firebaseConfig = { apiKey: "api-key", authDomain: "new-project.firebaseapp.com", databaseURL: "https:/new-project.firebaseio.com", projectId: "new-project-id", storageBucket: "xxxxxxxxxxx.appspot.com", messagingSenderId: "xxxxxx", appId: "xxxxxxxxxx", measurementId: "xxxxxxxx" }; // Initialize Firebase` – sultanmyrza Sep 01 '20 at 14:14
8

you can just use a command line switch

--project=my-firebase-project
Ron Chan
  • 3,315
  • 5
  • 27
  • 31
3

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.

SamHeyman
  • 207
  • 3
  • 13
1

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}"
  }
}
Giang
  • 2,384
  • 2
  • 25
  • 26
0

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)

yehonatan yehezkel
  • 1,116
  • 18
  • 28