gpt4 book ai didi

node.js - 无法在 Node.js 中导入 firebase-admin

转载 作者:行者123 更新时间:2023-12-05 02:04:28 24 4
gpt4 key购买 nike

我有一个 Node (14.3.0) 服务器,我通过添加以下行在我的 package.json 中启用了 ES6 模块导入:

package.json:

"类型": "模块",

根据此处的 firebase-admin 文档:https://firebase.google.com/docs/admin/setup/#node.js

If you are using ES2015, you can import the module instead:

import * as admin from 'firebase-admin';

当我使用 import * as admin from 'firebase-admin'; 时,出现以下错误:

credential: admin.credential.applicationDefault(),
TypeError: Cannot read property 'applicationDefault' of undefined

firebase-admin 似乎没有正确导入 - 我尝试删除 package.json 中的 "type": "module" 行并导入 firebase-管理员要求:

const admin = require(firebase-admin)

它有效,所以我的问题是 - 是否可以使用 ES6 在 Node 中导入 firebase-admin,如果可以,怎么做?

下面是一个完整的、最小的复制品:

服务器.js

import express from 'express';
import * as admin from 'firebase-admin';

const app = express();
const PORT = process.env.PORT || 5000;

app.use(express.json());

admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: process.env.FIREBASE_DB_URL,
});

app.listen(PORT, () => console.log(`listening on ${PORT}`));

export default app;

包.json

{
"name": "server",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"express": "^4.17.1",
"firebase-admin": "^9.2.0",
},
"type": "module",
"scripts": {
"dev": "node server.js"
},
"engines": {
"node": "14.x"
}
}

注意:在运行服务器之前,请确保在您的 shell (Mac/Linux) 中执行以下操作:

export GOOGLE_APPLICATION_CREDENTIALS="/your/path/to/service-account-file.json"

最佳答案

我在 firebase-admin-node github 上问了这个问题。显然,他们还没有用 Node 14 测试导入。答案很简单:

import admin from 'firebase-admin'

你可以在这里看到一个解释:

https://github.com/firebase/firebase-admin-node/issues/1061#event-3868300300

关于node.js - 无法在 Node.js 中导入 firebase-admin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64296423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com