Back to blog
Mar 09, 2025
3 min read

Integrate Firebase with Node.js: Step-by-Step Guide

Learn to integrate Firebase with Node.js using this detailed guide. Setup, authentication, real-time database, and best practices covered.

Firebase and Node.js form a powerful duo for building scalable web applications. Firebase offers backend-as-a-service (BaaS) features like real-time databases, authentication, and cloud functions, while Node.js provides a robust JavaScript runtime for server-side logic. In this tutorial, you’ll learn how to seamlessly integrate Firebase with Node.js, even if you’re new to backend development.

Firebase and Node.js Integration

Why Use Firebase with Node.js?

  • Real-Time Data Sync: Firebase’s Realtime Database updates clients instantly.
  • Authentication Made Easy: Support for Google, Facebook, and email/password logins.
  • Serverless Architecture: Combine Firebase Cloud Functions with Node.js for cost-effective scaling.
  • High Compatibility: Firebase’s JavaScript SDK works natively with Node.js.

Prerequisites

  1. Node.js v14+ installed on your machine.
  2. A Firebase account (free tier available).
  3. Basic knowledge of JavaScript and npm.

Step 1: Set Up a Firebase Project

  1. Go to the Firebase Console.
  2. Click “Add Project” and follow the setup wizard.
  3. Register a web app in your project to get the configuration object:
    const firebaseConfig = {
      apiKey: "YOUR_API_KEY",
      authDomain: "your-project.firebaseapp.com",
      databaseURL: "https://your-project.firebaseio.com",
      projectId: "your-project",
      storageBucket: "your-project.appspot.com",
      messagingSenderId: "1234567890",
      appId: "1:1234567890:web:abc123def456"
    };
    

Step 2: Install Firebase Admin SDK

Use npm to install the Firebase Admin SDK:

npm install firebase-admin --save

Step 3: Initialize Firebase in Node.js

Create a firebase-admin.js file:

const admin = require('firebase-admin');
const serviceAccount = require('./path/to/serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: 'https://your-project.firebaseio.com'
});

const db = admin.database();
module.exports = { admin, db };

To get serviceAccountKey.json:

  1. In Firebase Console, go to Project SettingsService Accounts.
  2. Click “Generate New Private Key” and save the file.

Step 4: Implement Authentication

Create a User:

const { admin } = require('./firebase-admin');

async function createUser(email, password) {
  try {
    const userRecord = await admin.auth().createUser({
      email,
      password,
    });
    console.log('User created:', userRecord.uid);
  } catch (error) {
    console.error('Error creating user:', error);
  }
}

Step 5: Use Firebase Realtime Database

Write Data:

const { db } = require('./firebase-admin');

function writeData(userId, data) {
  db.ref('users/' + userId).set(data)
    .then(() => console.log('Data saved!'))
    .catch((error) => console.error('Error:', error));
}

Read Data:

db.ref('users').once('value')
  .then((snapshot) => console.log(snapshot.val()))
  .catch((error) => console.error('Error:', error));

Best Practices for Firebase + Node.js

  1. Secure Your Data: Use Firebase Security Rules to restrict unauthorized access.
  2. Environment Variables: Store API keys in .env files (use dotenv npm package).
  3. Error Handling: Wrap Firebase operations in try/catch blocks.
  4. Use Async/Await: Improves readability for asynchronous operations.

Troubleshooting Common Issues

  • Authentication Errors: Ensure your service account key is valid and not expired.
  • Database Permission Denied: Update Firebase Security Rules to allow read/write.
  • Connection Timeouts: Check your Node.js server’s internet connectivity.

Conclusion

By integrating Firebase with Node.js, you leverage Firebase’s ready-to-use services while maintaining control via Node.js’s backend flexibility. This setup is ideal for startups and developers aiming to build scalable apps without heavy infrastructure management.