Firebase functions gen2: How to access the environmental variables in node_modules

1,617 views
Skip to first unread message

Mustafa Ekim

unread,
Feb 26, 2024, 12:05:51 AM2/26/24
to fireba...@googlegroups.com
Hi,

We're currently in the process of upgrading our Firebase functions to the second generation. While exploring the available options for defining environmental variables in gen2, I've found that none of them address a very common use case, leaving me at a standstill.

A significant portion of the code executed within our functions relies on npm modules that we've implemented. These npm modules, however, require environmental variables for proper setup. Unfortunately, in the new gen2 environment, there doesn't seem to be a straightforward way to configure `process.env` while loading our functions.

In essence, our functions depend on an npm module which in turn depends on an environmental variable. How can I ensure that the code within `index.js` in the `node_modules` directory functions correctly?


console.info(`process.env.TI_ENVIRONMENT_NAME: ${process.env.TI_ENVIRONMENT_NAME}`);

Thankks


--

Mustafa Ekim

--

Jeff Huleatt

unread,
Feb 26, 2024, 2:08:36 PM2/26/24
to Firebase Google Group
Hi Mustafa,

Just as in 1st gen, you can set environment variables with a `.env` file. The "Environment variables" subheading on the "Configure your environment" guide shows how to do this for 2nd gen functions: https://firebase.google.com/docs/functions/config-env?gen=2nd#env-variables.

For most use cases in 2nd gen, we recommend using parameterized configuration (https://firebase.google.com/docs/functions/config-env?gen=1st#params) instead, but for your specific use case, it sounds like setting environment variables directly is the way to go.

Best,
Jeff

Reply all
Reply to author
Forward
0 new messages