How to Use Firebase CLI: Commands and Examples
Use the
firebase command-line tool to manage Firebase projects by running commands like firebase init to set up, firebase deploy to publish, and firebase serve to test locally. Install it with npm install -g firebase-tools and log in using firebase login.Syntax
The basic syntax for Firebase CLI commands is firebase [command] [options]. Common commands include:
firebase login: Sign in to your Firebase account.firebase init: Set up Firebase features in your project folder.firebase deploy: Upload your project to Firebase hosting or functions.firebase serve: Run your project locally for testing.
Options can modify commands, like --only hosting to deploy only hosting.
bash
firebase login firebase init firebase deploy firebase serve
Example
This example shows how to initialize a Firebase project, log in, and deploy hosting files.
bash
npm install -g firebase-tools firebase login firebase init hosting firebase deploy --only hosting
Output
ā Success! Project initialized.
ā Success! Hosting deployed to https://your-project-id.web.app
Common Pitfalls
Common mistakes include:
- Not logging in before running commands, causing authentication errors.
- Running
firebase initin the wrong folder, which can misconfigure your project. - Forgetting to specify
--onlyoption when deploying specific features. - Not installing
firebase-toolsglobally, leading to command not found errors.
bash
Wrong: firebase deploy Right: npm install -g firebase-tools firebase login firebase deploy
Quick Reference
| Command | Description |
|---|---|
| firebase login | Sign in to Firebase account |
| firebase init | Set up Firebase project features |
| firebase deploy | Deploy project to Firebase |
| firebase serve | Run project locally |
| firebase logout | Sign out from Firebase account |
Key Takeaways
Install Firebase CLI globally with npm before use.
Always run firebase login to authenticate your account.
Use firebase init to configure your project folder.
Deploy your project with firebase deploy command.
Use --only option to deploy specific Firebase features.