Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set env variable in local development for google cloud functions?

I'm trying to run cloud function on my local system for which I need to set some env variables. I'm following docs for env and for local development docs.

I'm trying to run my project via the following command:

node node_modules/@google-cloud/functions-framework --target=syncingredients --env-vars-file=.env.yaml

Where my .env.yaml looks like:

API_KEY: key
AUTH_DOMAIN: project.firebaseapp.com
like image 788
Black Mamba Avatar asked Oct 15 '25 04:10

Black Mamba


2 Answers

Seems like --env-vars-file aren't supported with functions framework (https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/38)

I would recommend the workaround suggested by relymd-djk:

pre-req:

npm install env-cmd 
npm install yaml2json

modifying the package.json scripts section with:

"scripts": {
    "start":"yaml2json .env.yaml >.env.json && env-cmd -r ./.env.json functions-framework --target=syncingredients",
    "deploy": "gcloud functions deploy myFunction --entry-point syncingredients  --trigger-http --runtime nodejs16  --env-vars-file ./.env.yaml"
}

to run the function:

npm start
like image 128
ClumsyPuffin Avatar answered Oct 17 '25 19:10

ClumsyPuffin


Thanks to ClumsyPuffin for highlighting that it isn't an available feature so I went for dotenv

Changed the file to .env:

API_KEY="key"
AUTH_DOMAIN="project.firebaseapp.com"

And used the following command to run the function locally

node -r dotenv/config node_modules/@google-cloud/functions-framework --target=syncingredients
like image 31
Black Mamba Avatar answered Oct 17 '25 20:10

Black Mamba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!