196

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?

1
  • 5
    if having issues listing projects use firebase projects:list Commented May 5, 2021 at 12:59

9 Answers 9

385

Found some useful information here Firebase CLI Reference.

The following code works for me.

firebase use <project_id>
Sign up to request clarification or add additional context in comments.

2 Comments

firebase.googleblog.com/2016/07/… - for a quick reference
If you're running on a CLI don't forget the --token otherwise it will not work.
48

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

2 Comments

Welcome, my approach is intended to be sharable with the rest of the team :)
I tried this out, and it works. Using this method prevents me from making silly mistakes of interchanging the environment wrongly.
42

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

1 Comment

You may need to logout of the CLI and log back in to see reference a new project id
41

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.

Comments

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.

5 Comments

Is there a change now? Because I cannot find the app name in firebase.json but can find it in .firebaserc.... Appreciate the clarification...
The project name is indeed in .firebaserc now.
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. :(
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?
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`
8

you can just use a command line switch

--project=my-firebase-project

Comments

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.

Comments

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}"
  }
}

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.