question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Firebase deploy trigger functions with dotenv not working

See original GitHub issue

[REQUIRED] Environment info

firebase-tools: 11.0.1

Platform: macOS

[REQUIRED] Test case

I have added a .env file providing some variables:

STORAGE_UPLOAD_BUCKET=mycustombucket

Then i want to use this variable to create a trigger:

functions.storage
  .bucket(process.env.STORAGE_UPLOAD_BUCKET)
  .object()
  .onFinalize(async (object) => {
  console.log(process.env.STORAGE_UPLOAD_BUCKET);
  //DO STUFF...
});

The problem is that STORAGE_UPLOAD_BUCKET is not defined during the time of deployment, so the trigger gets deployed for the default bucket, not matter the value of STORAGE_UPLOAD_BUCKET. But during runtime of the function it is then set. The same problem exists for other triggers: You cannot use environment variables in firestore trigger paths.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
inlinedcommented, Jun 21, 2022

We didn’t remove Runtime Config from the discovery phase for reverse compatibility reasons, though it is going away in general (no ETA yet). We don’t populate dotenv files during the discovery phase because our reconciliation between Functions and Extensions requires us to be able to determine the shape of the deployment generically outside the context of the project/environment variables. We’ll do this by capturing dependencies on, for example, the storage bucket as a “parameter”. This parameter will be enforced at deploy time (e.g. while interactive you’ll be prompted for any missing values and in non-interactive mode a deployment will fail). It will be backed by dotenv files in Cloud Functions for Firebase and by the backend server in the case of Firebase Extensions.

1reaction
inlinedcommented, Jun 26, 2022

Yes, I would recommend checking the status of an environment variable before doing global initialization. So instead of:

const dbCon = new Database(process.env.DB_ADDRESS);

change to

let dbConn;
if (process.env.DB_ADDRESS) {
  dbConn = new Database(process.env.DB_ADDRESS);
}

We are considering giving a startup callback as well so you can have code that runs only when a new instance is launched in production.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting up dotenv in firebase functions - Stack Overflow
Set your variables in the corresponding environment, and then run firebase use dev or firebase use prod before you deploy.
Read more >
Configure your environment | Cloud Functions for Firebase
When deploying a function with parameterized configuration variables, the Firebase CLI first attempts to load their values from local .env files.
Read more >
Fixing the Firebase Functions Configuration Loading Issues
A quick tutorial to debug and fix issues with environment variables not being loaded in Firebase Functions. Also applies to the local ...
Read more >
Using Environment Variables | Cloud Functions Documentation
Runtime environment variables are key/value pairs deployed alongside a function. These variables are scoped to the function and are not visible to other ......
Read more >
Firebase functions cannot find module express
Heroku deploy fails with react and next js; Node. 18. ... Issues with node - Error: Cannot find module 'firebase-functions' Ask Question Asked...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found