How to Use Firebase Emulator: Setup and Example
To use the
Firebase Emulator, install the Firebase CLI and run firebase emulators:start to launch local emulators for services like Firestore and Authentication. Configure your app to connect to these local emulators instead of live Firebase services for safe, offline testing.Syntax
The basic command to start Firebase emulators is firebase emulators:start. You can specify which emulators to run using flags like --only firestore,auth. The firebase.json file configures emulator settings such as ports and rules.
firebase emulators:start: Starts all configured emulators.--only: Runs only specified emulators.firebase.json: Defines emulator ports and rules.
bash
firebase emulators:start --only firestore,auth
Example
This example shows how to start Firestore and Authentication emulators and connect a Node.js app to them for local testing.
javascript
/* Start emulators in terminal */ // Run this command in your project folder firebase emulators:start --only firestore,auth /* Node.js app connecting to emulators */ const { initializeApp } = require('firebase/app'); const { getFirestore, connectFirestoreEmulator } = require('firebase/firestore'); const { getAuth, connectAuthEmulator } = require('firebase/auth'); const firebaseConfig = { apiKey: 'fake-api-key', authDomain: 'localhost', projectId: 'demo-project' }; const app = initializeApp(firebaseConfig); const db = getFirestore(app); const auth = getAuth(app); // Connect to local emulators connectFirestoreEmulator(db, 'localhost', 8080); connectAuthEmulator(auth, 'http://localhost:9099'); console.log('Connected to Firebase emulators');
Output
Connected to Firebase emulators
Common Pitfalls
Common mistakes when using Firebase emulators include:
- Not running
firebase emulators:startbefore testing, so the app cannot connect. - Forgetting to configure the app to connect to the emulator ports.
- Using production Firebase credentials instead of emulator settings.
- Not updating security rules in
firebase.jsonfor the emulator environment.
Always verify emulator ports and update your app code accordingly.
javascript
/* Wrong: Not connecting to emulator */ const db = getFirestore(app); // No emulator connection /* Right: Connect to emulator */ connectFirestoreEmulator(db, 'localhost', 8080);
Quick Reference
| Command / Setting | Description |
|---|---|
| firebase emulators:start | Start all configured emulators |
| firebase emulators:start --only firestore,auth | Start only Firestore and Auth emulators |
| connectFirestoreEmulator(db, 'localhost', 8080) | Connect Firestore SDK to local emulator |
| connectAuthEmulator(auth, 'http://localhost:9099') | Connect Auth SDK to local emulator |
| firebase.json | Configure emulator ports and rules |
Key Takeaways
Always run 'firebase emulators:start' before testing locally.
Configure your app to connect to emulator ports to avoid using live services.
Use 'firebase.json' to set emulator ports and security rules.
Test safely offline without affecting production data.
Check emulator logs for errors during startup and connection.