angrykoala
angrykoala

Reputation: 4064

Know if express app is running as firebase cloud function

I have an express app that I want to run locally as a standalone server in my machine however, when deploying it to firebase cloud functions I need to set it as a cloud function.

Is there a reliable way to know in which environment the app is running without manually setting env variables or what is the best practice?

eg:

if(isRunningInFirebase()){
  exports.myFun=functions.https.onRequest(app)
} else app.listen(3030)

Upvotes: 5

Views: 1334

Answers (4)

Brent Washburne
Brent Washburne

Reputation: 13158

Firebase now sets the FUNCTIONS_EMULATOR environment variable when running the emulator:

if (process.env.FUNCTIONS_EMULATOR === 'true') {
    functions are on localhost
} else {
    functions are on Firebase
}

Upvotes: 8

Dan Kolis
Dan Kolis

Reputation: 39

Not having to edit a program before running it locally or on googles computers remotely saves a lot of time.

The object process.env is asserted both locally and when run in the cloud. It differs hugely, but here's a property I think is reliable, easy to understand, and to use.

This stand alone code says where it is, and sets the variable port to a usual number I use for both circumstances.

// Dan K The program wants to know, where am I ?
'use strict'

console.log( "program ID: " + "zincoNoDogs13" );

// Make a message and set a port number for other uses depending on whether the
// program wakes up on a local computer or in google cloud

const functions = require( 'firebase-functions' );
const express = require('express');
const app = express();
exports.api = functions.https.onRequest( app );

var port;
var imaThis = "local";
let lookie = process.env.HOME;
if( lookie == "/tmp" ) { imaThis = "cloud"; }

if( imaThis == "local" ) {console.log( "I am on a local computer" ); port = 3000; }
if( imaThis == "cloud" ) {console.log( "I am in the clouds man" ); port = 80; }

console.log( "Port number is going to be: " + port );

You get one or the other, or well, I do anyway:

program ID: zincoNoDogs13 I am in the clouds man Port number is going to be: 80

program ID: zincoNoDogs13 I am on a local computer Port number is going to be: 3000

Upvotes: 1

Matthew Mullin
Matthew Mullin

Reputation: 7646

I did some exploring by logging the process.env

When running functions locally using firebase functions:shell or firebase serve --only functions there are a bunch of local machine type node variables.

When running the deployed function sitting in Firebase Cloud Functions. There is a new node environment variable that is not set when running locally:

NODE_ENV: 'production'

So to use it:

if (process.env.NODE_ENV === 'production') { 
    // running in production cloud environment 
} else { 
    // running locally (shell or serve) 
}

Upvotes: 2

LundinCast
LundinCast

Reputation: 9810

There are environment variables that are automatically populated in the functions runtime and in locally emulated functions, as documented here. One of them for example is the GCLOUD_PROJECT variable, which is set to your Firebase project ID. You can have your app checking for it like this:

if(process.env.GCLOUD_PROJECT) { 
    // running in Firebase environment 
}
else { 
    // running somewhere else 
}

Upvotes: 5

Related Questions