'Firebase functions dotenv variable undefined
I'm running an update of a Firebase functions project.
Environment variables was managed with runtime environment configuration (functions.config). Regarding the recommended way in Firebase documentation, i choosed to used file-based configuration of environment variables with dotenv file format.
1 - I ran firebase functions:config:export
and
4 dotenv files was created in the functions/
folder.
.env.default
.env.
.env.dev
.env.local
.env file:
# Exported firebase functions:config:export command on 24/02/2022`
MAILJET_KEY="XXXX"
MAILJET_SECRET="XXXX"
STRAVA_SECRET="XXXX"
STRAVA_ID="XXXX"
2 - I updated the source code to manage process.env.MY_VARIABLE
const mailjet = require('node-mailjet').connect(process.env.MAILJET_KEY, process.env.MAILJET_SECRET);
3 - I ran npm run serve
or deploy the function but process.env.MY_VARIABLE
return undefined
Error: Mailjet API_KEY is required
**Do i run a something special to migrate from functions.config to process.env ??? **
Note: I've recently managed process.env
iin a new project and it's works fine !
Thx
Solution 1:[1]
My issue after the migration was that the process.env
was not ready on initial emulators startup.
Ref: https://github.com/firebase/firebase-tools/issues/4239#issuecomment-1063763689
functions/index.js:
import * as functions from "firebase-functions";
import * as callable from "./callables";
// Not possible
export const someFunction = someFunctionInit(process.env.APP_ENV);
So I needed to move the process.env usage within the function itself,
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Hugo Gresse |