node.js - 在本地运行 Node test.js 时访问本地 Firebase 环境变量

标签 node.js firebase google-cloud-functions

我有以下简单的test.js:

const functions = require('firebase-functions');
console.log(functions.config().supportgmail.email);

在运行 node test.js 之前我已完成以下操作:

  1. firebase 函数:config:get > .runtimeconfig.json
  2. firebase 模拟器:启动
  3. Node test.js

运行node test.js后,出现以下错误,并且环境变量未输出到控制台:

{"severity":"WARNING","message":"Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. 
Initializing firebase-admin will fail"}

为了使本地 Firebase 环境变量可访问,我还需要在 test.js 中执行哪些操作?请注意,当我在本地运行整个 Web 应用程序项目时,访问本地环境变量没有问题,除非我使用 node test.js 独立运行 JS - 我只想让我新创建的需要访问环境变量的函数在将其合并到我的应用程序项目之前可以正常工作。谢谢!

最佳答案

使用 Node v10 部署功能时会出现此错误(如果您没有使用 Node 10,请告诉我)。这是由 firebase-functions 中的错误引起的。 Node 10 环境不再包含 GCLOUD_PROJECT。作为解决方法,您可以将以下行添加到代码中:

process.env.GCLOUD_PROJECT = JSON.parse(process.env.FIREBASE_CONFIG).projectId

或者您可以降级到 Node 8,直到此问题得到解决。

另外请尝试重新部署云函数。

另请查看以下有关此问题的 GitHub 线程 1 , 2 .

************ 更新 **************

根据以下消息:

您请求的“Node ”版本“10”与您的全局版本“12”不匹配,如果不是,该消息不是错误,它只是让您知道您的 package.json 声明目标 Node 版本为 10,但您在计算机上使用 Node 12 进行模拟。要消除该消息,请在您的计算机上安装 Node 10 而不是 12。反之亦然。

请确定是 Node.js version are you using in your package.json file引擎领域。

根据 Firebase Official Documentation ,据说

Firebase SDK for Cloud Functions 2.0.0 and higher allows a selection of Node.js runtime

当您使用 Node.js 时,您的 Cloud Functions SDK 应该为 2.0.0 或更高版本,因此请按照后续步骤操作或查看 Official Documentation确保您安装了正确的 SDK:

  1. 如果您尚未将 SDK 更新到最新版本,请在函数文件夹中运行以下命令:

npm install firebase-functions@latest --save npm install firebase-admin@latest --save-exact

  • 还将 Firebase CLI 更新到最新版本:
  • npm install -g firebase-tools

    如果您希望使用 firebaseserve 对函数进行本地模拟来获取环境变量,则需要按照下一个 Documentation 进行操作。 .

    If you're using custom functions configuration variables, run the following command in the functions directory of your project before running firebase serve.

    firebase functions:config:get > .runtimeconfig.json

    However, if you're using Windows PowerShell, replace the above command with:

    firebase functions:config:get | ac .runtimeconfig.json

    最后,Cloud Functions Official Documentation 中缺少并陈述的要点。是当您在不带任何参数的情况下初始化 Firebase Admin SDK 时,会自动应用此配置。 请添加以下内容:

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp();
    
    let firebaseConfig = JSON.parse(process.env.FIREBASE_CONFIG);
    let project = firebaseConfig.projectId;
    console.log(project);
    

    希望对您有所帮助。

    关于node.js - 在本地运行 Node test.js 时访问本地 Firebase 环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64926615/

    相关文章:

    node.js - Stripe API 未处理的拒绝响应错误

    javascript - 如何让本地 eslint 插件与本地安装的 eslint 一起工作

    java - android 在设备上保存非常小的数据的最佳方法

    firebase - Google Firebase事件原始数据中关键 "previous_timestamp_micros"的含义是什么

    node.js - 函数正在循环

    python - 使用 Google Cloud Functions 时如何设置 Google Cloud Datastore 实体的日期属性?

    asp.net - 使用 Asp.Net Web Api 实现 Angular 2

    javascript - 如何在 POSTMAN 上获得与浏览器相同的响应行为?

    node.js - 如何将 "Auto Prefixer"合并到 "package.json"和 "grunt.js"中?

    AngularFire2 在 Typescript 中获取经过身份验证的用户 ID