TypeError: firestoreService.initializeApp is not a function

TypeError: firestoreService.initializeApp is not a function

我正在尝试编写一个 TS 脚本,该脚本将采用 JSON 文件并将其作为文档导入到 Firebase 集合中。我受到 this tutorial that uses firestore-import-export 包的启发。但是,即使我已经安装了软件包,我也无法对其进行初始化。 我的 scripts/test_import_data.ts 脚本如下所示:

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)
    })

我的 package.json 看起来像这样:

{
  ...
  "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"
  }
}

在终端中,我运行这样的脚本有以下错误:

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)

经过Google/Whosebug研究,我迷路了,现在。你有没有遇到过类似的问题?你知道我做错了什么吗?任何帮助表示赞赏! ♥️

看起来您可能正在为 Firebase SDK modular 之前使用的那个 npm 库使用旧的 API。仔细查看您链接到的关于 npm 的文档。示例代码如下所示:

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

请注意,initializeFirebaseApp 函数是直接导入的。没有“服务”对象导出来调用方法。