LoginSignup
3
2

More than 1 year has passed since last update.

[Firebase] Functionsで本番・開発・Emulator環境を判別する

Last updated at Posted at 2022-10-05

はじめに

環境ごとに処理を変えたいんだけどFunctionsでどうやって判別できるの?
調べても直接的な回答記事が見つからなかったが、process.envに用意されてるんじゃないかと調べてみたら使えそうなものを見つけた。

エミュレータであるかを判別する方法

process.env["FUNCTIONS_EMULATOR"] //  "true" or "false で返ってくる

予約済みの環境変数として最初から設定されているもよう。

本番環境 / デバッグ環境を判別する方法

process.env["FIREBASE_DEBUG_MODE"] //  "true" or "false で返ってくる

こちらも予約済みの環境変数として最初から設定されているもよう。

尚、設定によってはFIREBASE_DEBUG_MODEで判別できない場合もあるらしく、その場合はprojectIdで判別する必要がありそうです。

process.env["GCLOUD_PROJECT"] === "projectId";

Sample: 本番・開発・Emulator環境を判別する

const isEmulator = process.env["FUNCTIONS_EMULATOR"] === "true";
const isDebug = process.env["FIREBASE_DEBUG_MODE"] === "true";

const environment = () => {
  switch (true) {
      case !isDebug:{
          return "本番環境";
      }
      case isEmulator: {
          return "Emulator";
      }  
      default: {
          return "開発環境";
      }  
  }
}

console.log(environment());

最後に

もっといい方法あれば教えてほしいですー!

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2