Home > other >  TypeError: firestoreService.initializeApp is not a function
TypeError: firestoreService.initializeApp is not a function

Time:05-03

I am trying to write a TS script that would take a JSON file and import it into Firebase collection as a document. I am getting inspired by this tutorial that uses firestore-import-export package. However, even though I have installed the package, I cannot initialize it. My scripts/test_import_data.ts script looks like this:

const firestoreService = require('firestore-export-import');
const serviceAccount = require(__dirname   '/../serviceAccountStaging.json');
const firebaseConfig = require(__dirname   '/../config.js');

async function main() {
  await firestoreService.initializeApp(serviceAccount, firebaseConfig.databaseURL);
}

main()
    .then(() => {
        process.exit(0)
    })
    .catch((error) => {
        console.error(error)
        process.exit(1)
    })

My package.json looks like this:

{
  ...
  "engines": {
    "node": "~v16.14.2",
    "npm": "~8.5.0"
  },
  "scripts": {
    "script": "ts-node"
  },
  "devDependencies": {
    "@types/yargs": "^17.0.10",
    "dotenv": "^16.0.0",
    "firestore-export-import": "^1.1.0",
    "ts-node": "^10.7.0",
    "typescript": "^4.6.4",
    "yargs": "^17.4.1"
  }
}

In terminal, I am running the script like this with the following error:

my@mac % npm run script scripts/test_import_data.ts
TypeError: firestoreService.initializeApp is not a function
        at main (/Users/mymac/projects/my-project/scripts/test_import_data.ts:6:26)
        at Object.<anonymous> (/Users/mymac/projects/my-project/scripts/test_import_data.ts:9:1)
        at Module._compile (node:internal/modules/cjs/loader:1105:14)
        at Module.m._compile (/Users/mymac/projects/my-project/node_modules/ts-node/src/index.ts:1455:23)
        at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
        at Object.require.extensions.<computed> [as .ts] (/Users/mymac/projects/my-project/node_modules/ts-node/src/index.ts:1458:12)
        at Module.load (node:internal/modules/cjs/loader:981:32)
        at Function.Module._load (node:internal/modules/cjs/loader:822:12)
        at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
        at phase4 (/Users/mymac/projects/my-project/node_modules/ts-node/src/bin.ts:567:12)

After Google/StackOverflow research, I am lost, now. Have you ever faced similar issue? Do you know what I am doing wrong, please? Any help is appreciated! ♥️

CodePudding user response:

It looks like you might be using an old API for that npm library that was in use before the Firebase SDKs went modular. Take a look at the documentation on npm very carefully that you linked to. The example code looks like this:

const { initializeFirebaseApp } = require('firestore-export-import')
initializeFirebaseApp(serviceAccount, appName, options)

Notice that the initializeFirebaseApp function is a direct import. There is no "service" object export to call methods on.

  • Related