How to tell if cloud functions are running in the emulator and not deployed?

1,842 views
Skip to first unread message

Jonathon Broughton

unread,
Sep 15, 2017, 8:29:12 PM9/15/17
to Firebase Google Group
What environment setting, configuration variable or otherwise can I use to conditionally test if I am running a CF locally in emulator or deployed to firebase cloud?

TIA

Doug Stevenson

unread,
Sep 16, 2017, 6:02:29 PM9/16/17
to Firebase Google Group
Jonathon,

If I invoke an HTTP trigger with the statement "console.log(process.env)", I get this in the log:

{ WORKER_PORT: '8091',
  GCLOUD_PROJECT: 'the-id-of-my-project',
  FUNCTION_NAME: 'api',
  SUPERVISOR_HOSTNAME: '192.168.1.1',
  PATH: '/usr/local/bin:/usr/bin:/bin',
  PWD: '/user_code',
  FUNCTION_TRIGGER_TYPE: 'HTTP_TRIGGER',
  NODE_ENV: 'production',
  SHLVL: '1',
  CODE_LOCATION: '/user_code',
  FUNCTION_MEMORY_MB: '256',
  GCP_PROJECT: 'the-id-of-my-project',
  PORT: '8080',
  SUPERVISOR_INTERNAL_PORT: '8081',
  ENTRY_POINT: 'api',
  OLDPWD: '/var/tmp/worker/',
  _: '/usr/bin/env',
  HOME: '/tmp' }

I don't know which one is the most appropriate, but I'd go with process.env.GCLOUD_PROJECT or process.env.GCP_PROJECT to see where it's running.  Neither of those are probably in your shell environment.  Alternately, you can put something special in your shell env and check for it in process.env.

Doug

Vlad O

unread,
May 21, 2019, 9:40:55 AM5/21/19
to Firebase Google Group
Another option is to setup Environment configuration as described here https://firebase.google.com/docs/functions/config-env

Sam Stern

unread,
May 21, 2019, 2:39:57 PM5/21/19
to Firebase Google Group
Hi Vlad,

This sounds like a good feature request for the emulator.  We could set something like "process.env.FIREBASE_EMULATOR" to "true".  Would that work for your use case?  If so please file a feature request on the firebase-tools repository!

- Sam

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/2a2483e3-0bf5-49ac-ad9f-89cfb75e9978%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages