Import .env files per APP_ENV
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
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"