firebase.firestore() is not a function when trying to initialize Cloud Firestore

Eyk Rehbein picture Eyk Rehbein · Oct 8, 2017 · Viewed 65.1k times · Source

When I try to initialize Firebase Cloud Firestore, I ran into the following error:

Uncaught TypeError: WEBPACK_IMPORTED_MODULE_0_firebase.firestore is not a function

I installed firebase with npm install firebase --save previously.

import * as firebase from 'firebase';
import router from '../router';

const config = {
        apiKey: "a",
        authDomain: "a",
        databaseURL: "a",
        projectId: "a",
        storageBucket: "a",
        messagingSenderId: "a"
};
if(!firebase.apps.length){
  firebase.initializeApp(config);
  let firestore = firebase.firestore();
}

Answer

Eyk Rehbein picture Eyk Rehbein · Jun 4, 2018

I fixed it by importing multiple libraries: firebase and firebase/firestore. That's because the firebase core library does not include the firestore library.

import * as firebase from 'firebase';
import 'firebase/firestore';