@slytrunk/app-env

Import .env files per APP_ENV

Usage no npm install needed!

<script type="module">
  import slytrunkAppEnv from 'https://cdn.skypack.dev/@slytrunk/app-env';
</script>

README

app-env

Import .env files per APP_ENV

Usage

module.exports = {
  plugins: [
    [
      "module:@slytrunk/app-env",
      {
        moduleName: "@slytrunk/app-env",
        cacheIdentifier: process.env.APP_ENV,
        envs: {
          stage: ".env.stage",
          production: ".env.production"
        }
      }
    ]
  ]
}

Then you can import env variables in your code:

import { API_URL } from "@slytrunk/app-env";

Be sure to specify APP_ENV up front.

$ APP_ENV=stage yarn dev

Debug Mode

If your APP_ENV is not production, we support hot-loading different environments so you can test multiple environments in one build.

import { setEnvironment } from "@slytrunk/app-env/debug";
setEnvironment("stage");

NOTE: It's a good idea to set cacheIdentifier as babel will cache the results of this module for subsequent builds and may get stuck in debug mode.

You can get a list of available environments so you can build ui to switch environments:

import { getEnvironmentOptions } from "@slytrunk/app-env/debug";

getEnvironmentOptions();
// ["stage", "production"]

Lastly, you can load variables directly from debug mode, however this is not recommended as the babel plugin does this for you automatically using the regular interface.

// !! Not Recommended
import { getEnv } from "@slytrunk/app-env/debug";

getEnv("API_URL");
// "http://api.example.com"

// Recommended
import { API_URL } from "@slytrunk/app-env";
// "http://api.example.com"