firebase.database is not a function

The question:

I am trying to upgrade from earlier firebase version to the latest in my ionic project. I followed this tutorial for upgrade. In step 4 from this page I am stuck on the last statement firebase.database().ref();.

Error message

TypeError: firebase.database is not a function

Below is my code. Kindly help.


// Initialize Firebase
this.config = {
    apiKey: "some-api-key",
    authDomain: "",
    databaseURL: "",
    storageBucket: "",


this.authWithOAuthPopup = function(type) {
    var deferred = $q.defer();
    console.log(service.config);    // ---> Object {apiKey: "some-api-key", authDomain: "", databaseURL: "", storageBucket: ""}
    console.log(firebase);  // ---> Object {SDK_VERSION: "3.0.5", INTERNAL: Object}
    service.rootRef = firebase.database().ref(); //new Firebase(""); ---> I am getting error on this line "TypeError: firebase.database is not a function"
    service.rootRef.authWithOAuthPopup(type, function(error, authData) {
        if (error) {
            service.authError = error;
            switch (error.code) {
                case "INVALID_EMAIL":
                    console.log("The specified user account email is invalid.");
                case "INVALID_PASSWORD":
                    console.log("The specified user account password is incorrect.");
                case "INVALID_USER":
                    console.log("The specified user account does not exist.");
                    console.log("Error logging user in:", error);
        } else {
            service.authData = authData;
            console.log("Authenticated successfully with payload:", authData);
        return deferred.promise;
    return deferred.promise;

var service = this;


After adding latest database library this questions problem is solved.

Updating my code here

this.authWithOAuthPopup = function(type) {
    var deferred = $q.defer();
    service.rootRef = firebase.database(); //.ref(); //new Firebase("");

    var provider = new firebase.auth.FacebookAuthProvider();
    firebase.auth().getRedirectResult().then(function(result) {
        if (result.credential) {
            // This gives you a Facebook Access Token. You can use it to access the Facebook API.
            var token = result.credential.accessToken;
            // ...
        // The signed-in user info.
        var user = result.user;
    }).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // The email of the user's account used.
        var email =;
        // The firebase.auth.AuthCredential type that was used.
        var credential = error.credential;
        // ...
    return deferred.promise;

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

I ran into this with Ionic and it turned out that I wasn’t including everything when using the latest Firebase Client. If you’ve included Firebase as firebase-app, then the Database and Auth pieces need to be required separately since they aren’t bundled when including Firebase in this way.

Add the following to your index.html after you include firebase-app.js

<script src=""></script>
<script src=""></script>

Obviously you don’t need to use the CDN, you could use bower (probably the preferred way with Ionic) or NPM with Browserify.

// Browserify Setup
var firebase = require('firebase/app');

Snippet below taken from the Firebase Web Setup Docs

You can reduce the amount of code your app uses by just including the features you need. The individually installable components are:

firebase-app – The core firebase client (required).
firebase-auth – Firebase Authentication (optional).
firebase-database – The Firebase Realtime Database (optional).
firebase-storage – Firebase Storage (optional).

From the CDN, include the individual components you need (include firebase-app first)

Method 2

A bit late to the party, but in case some one wanted to know the syntax in angular, (or Ionic 4) just add this to your .module.ts file (Note, as peterb mentioned, the /database import)

import { AuthService } from './auth.service';
import { AngularFireAuthModule } from 'angularfire2/auth';
import { AngularFireDatabaseModule } from 'angularfire2/database';

  imports: [
  providers: [

Method 3

i solved this issue by giving the url in the constructor

Method 4

First, make sure you are using

<script src=""></script>

Firebase authWithOAuthPopup has changed a little bit in the new version.

Now you don’t use the ref to call authentication methods. You should be using firebase.auth() insted.

var auth = firebase.auth();
var provider = new firebase.auth.TwitterAuthProvider();
auth.signInWithPopup(provider).then(function(result) {
  // User signed in!
  var uid = result.user.uid;
}).catch(function(error) {
  // An error occurred

Method 5

Also faced this problem on @angular/firebase 5.1.2, solved when updated @angular/cli and all dependencies to the latest version.

Method 6

For people facing similar error(this._database.native.on is not a function) in React-native –

  1. run the pod install – after adding firebase new service(database/auth …)
  2. terminate the metro bundler and restart it using “npx react-native start”
  3. run “npx react-native run-ios”

This will create a new build and the error should be gone.


Method 7


var firebase = require('firebase/app');

Method 8

You need to import newest version of firebase-database . U can find on cndjs

Method 9

npm install --save firebase



You need to add all of the firebase products you are using by way of require() as shown above.

Method 10

I have the same error -firebase.database is not a function- but with different situation you just need to add

above and the link of javascript that contain the Firebase configuration.

You may also try to use the defer Attribute in your script as it will not load the scripts until page elements are loaded.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment