简体   繁体   English

警告,缺少 FIREBASE_CONFIG 环境变量。 初始化 firebase-admin 将失败

[英]Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail

When I follow the document of firebase, but I can not connect to firebase.当我按照firebase的文档进行操作时,但无法连接到firebase。

Does anybody face this issue?有人面临这个问题吗?

My code :我的代码:

  • index.js

     var admin = require("firebase-admin"); var serviceAccount = require("./key/serviceAccountKey.json"); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: "https://sampleLink.firebaseio.com", }); const functions = require("firebase-functions"); var db = admin.firestore(); var data = { name: "Los Angeles", state: "CA", country: "USA", }; // Add a new document in collection "cities" with ID 'LA' var setDoc = db.collection("cities").doc("LA").set(data); setDoc;
  • package.json

     { "name": "functions", "description": "Cloud Functions for Firebase", "scripts": { "serve": "firebase serve --only functions", "shell": "firebase functions:shell", "start": "npm run shell", "deploy": "firebase deploy --only functions", "logs": "firebase functions:log" }, "dependencies": { "firebase": "^5.7.3", "firebase-admin": "~6.0.0", "firebase-functions": "^2.1.0" }, "private": true }
  • console

     tktktk:functions dev$ node index.js Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail The behavior for Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK. To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods: const firestore = new Firestore(); const settings = {/* your settings... */ timestampsInSnapshots: true}; firestore.settings(settings); With this change, timestamps stored in Cloud Firestore will be read back as Firebase Timestamp objects instead of as system Date objects. So you will also need to update code expecting a Date to instead expect a Timestamp. For example: // Old: const date = snapshot.get('created_at'); // New: const timestamp = snapshot.get('created_at'); const date = timestamp.toDate(); Please audit all existing usages of Date when you enable the new behavior. In a future release, the behavior will change to the new behavior, so if you do not follow these steps, YOUR APP MAY BREAK.

My env is below:我的环境如下:

$ firebase --version
6.3.0
$ node -v
v8.12.0
$ npm -v
6.4.1

I had the exact same issue.我有完全相同的问题。 Turns out it has to do with Node version 10 I removed Node 10 and went back to Node 8 and everything worked like a charm...原来它与 Node 10 版本有关,我删除了 Node 10 并返回到 Node 8,一切都像魅力一样......

 const functions = require('firebase-functions');
 const admin = require('firebase-admin');
 admin.initializeApp();

Just hit刚打

 firebase deploy --only functions

in Node v8.在 Node v8 中。

That FIREBASE_CONFIG warning hints that the path to the JSON is missing (or wrongful). FIREBASE_CONFIG警告提示JSON的路径丢失(或错误)。

Either setup FIREBASE_CONFIG , as it demands - or setup GOOGLE_APPLICATION_CREDENTIALS as environment variable and then run gcloud auth application-default login ;根据需要设置FIREBASE_CONFIG - 或将GOOGLE_APPLICATION_CREDENTIALS设置为环境变量,然后运行gcloud auth application-default login then you could use admin.credential.applicationDefault() instead of admin.credential.cert(serviceAccount) .那么你可以使用admin.credential.applicationDefault()而不是admin.credential.cert(serviceAccount)

The proper way to run a cloud function locally is via the Functions shell (see https://firebase.google.com/docs/functions/local-emulator ).在本地运行云函数的正确方法是通过 Functions shell(请参阅https://firebase.google.com/docs/functions/local-emulator )。 If you want to run it directly like $ node index.js , then you have to set the required environment variables yourself, or otherwise firebase-functions is going to complain.如果你想像$ node index.js一样直接运行它,那么你必须自己设置所需的环境变量,否则firebase-functions会抱怨。

However, note that the above message is just a warning.但是,请注意,上述消息只是一个警告。 Your code is running despite that.尽管如此,您的代码仍在运行。 If you're not seeing any data written to Firestore, that's probably because you're not handling the promise returned by the Firestore set() method.如果您没有看到任何写入 Firestore 的数据,那可能是因为您没有处理 Firestore set()方法返回的承诺。

For me, the problem was that line:对我来说,问题是那一行:

const functions = require('firebase-functions');

I just deleted it from index.js .我刚刚从index.js 中删除了它。 I don't use Firebase Cloud Function anymore but I forgot to remove that line.我不再使用 Firebase Cloud Function,但我忘记删除该行。 This is where my problem was coming from.这就是我的问题的来源。

So maybe you too imported this package in a file that don't need it.所以也许你也把这个包导入到一个不需要它的文件中。

About you problem, just now Thanks to your code i was able to fix my problem.关于你的问题,刚才多亏了你的代码,我才能解决我的问题。

What i do:我所做的:

const admin = require('firebase-admin');

var serviceAccount = require("./serviceAccount.json");

admin.initializeApp({ credential: admin.credential.cert(serviceAccount) });

const db = admin.firestore();

Im using a Cloud Firestore db, not a real time db, so i dont send the second argument in the initializeApp , i just put the config of the db in the environment file.我使用的是 Cloud Firestore 数据库,而不是实时数据库,所以我没有在initializeApp 中发送第二个参数,我只是将数据库的配置放在环境文件中。

export const environment = {
  production: false,
  firebaseConfig : {
    apiKey: 'XXXXXXXXXXX',
    authDomain: 'XXXXXXXXXX',
    databaseURL: 'XXXXXXXXXX',
    projectId: 'XXXXXXXXX',
    storageBucket: 'XXXXXXXXXXX',
    messagingSenderId: 'XXXXXXXXXXX',
    appId: 'XXXXXXXXXXXX',
    measurementId: 'XXXXXXX'
  }
};

Try using this version dependencies (in package.json ) with node v8.尝试在 node v8 中使用此版本依赖项(在package.json )。

"dependencies": {
    "firebase-admin": "~7.1.1",
    "firebase-functions": "^2.2.1"
}

It worked for me.它对我有用。

This worked for me :这对我有用:

 admin.initializeApp({ credential: admin.credential.cert({ "type": "service_account", "project_id": "took from Firebase Generated Private Key", "private_key_id": "took from Firebase Generated Private Key", "private_key": "took from Firebase Generated Private Key", "client_email": "took from Firebase Generated Private Key", "client_id": "took from Firebase Generated Private Key", "auth_uri": "took from Firebase Generated Private Key", "token_uri": "took from Firebase Generated Private Key", "auth_provider_x509_cert_url": "took from Firebase Generated Private Key", "client_x509_cert_url": "took from Firebase Generated Private Key" }), databaseURL: "https://test-dia.firebaseio.com" });

The correct way to remove the warning as of 2021 is to add firebase-functions-test to your dev-dependencies, and call it in your jest config like so:自 2021 年起删除警告的正确方法是将firebase-functions-test添加到您的开发依赖项,并在您的 jest 配置中调用它,如下所示:

// jest.preset.js
const test = require('firebase-functions-test')();
// ...

Read more about testing in "offline mode" here: https://firebase.google.com/docs/functions/unit-testing#offline-mode在此处阅读有关“离线模式”测试的更多信息: https : //firebase.google.com/docs/functions/unit-testing#offline-mode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告,在 firebase 中设置环境配置时缺少 FIREBASE_CONFIG 和 GCLOUD_PROJECT 环境变量 - Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing when set environment configuration in firebase 在 Nodejs 中为 firebase-admin NPM 初始化到 Firebase 的多个连接失败 - Initializing multiple connections to firebase are failing in Nodejs for firebase-admin NPM 使用 Nuxt 的 Firebase 管理员 - Firebase-Admin with Nuxt firebase-admin WebSocket错误 - firebase-admin WebSocket Error 使用空参数、admin.credential.applicationDefault() 和 serviceAccount 凭据使用 firebase-admin 初始化应用程序时的差异 - Differences when initializing app with firebase-admin using empty params, admin.credential.applicationDefault() and serviceAccount credentials firebase JS SDK 包是否与云函数的 Node.js 环境兼容? 为什么要使用 firebase-admin? - Is the firebase JS SDK package compatible with the Node.js environment of a cloud function? Why use firebase-admin? 在 netlify 函数中使用 firebase-admin - using firebase-admin in netlify function 创建多个 firebase-admin 实例 - Create multiple instances of firebase-admin 使用 firebase-admin 发送主题通知 - Sending topic notifications using firebase-admin 无法使用 npm 安装 firebase-admin - Can't install firebase-admin with npm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM