Lazy Universal DotEnv - A Robust Environment Configuration for Universal Applications.
- Supports loading
.envfiles with overriding between differentNODE_ENVsettings andBUILD_TARGETconfigurations. - Supports variable expansion between different settings.
- Allows local overrides using files which use a ".local" postfix.
It is important to remember that all environment variables are always stored as strings. Even numbers and booleans. The casting to other types must therefore take place in the application code. See also: motdotla/dotenv#51
NODE_ENV: Typically eitherproduction,developmentortestBUILD_TARGET: Eitherclientorserver
Files are being loaded in this order. Values which are already set are never overwritten. Command line environment settings e.g. via cross-env always win.
.env.${BUILD_TARGET}.${NODE_ENV}.local.env.${BUILD_TARGET}.${NODE_ENV}.env.${BUILD_TARGET}.local.env.${BUILD_TARGET}.env.${NODE_ENV}.local.env.${NODE_ENV}.env.local.env
Note: local files without NODE_ENV are not respected when running in NODE_ENV=test.
import { getEnvironment } from "lazy-dotenv-universal";
const environment = getEnvironment({ nodeEnv, buildTarget });
const { raw, stringified, webpack } = environment;After this you can access all environment settings you have defined in one of your .env files.
A .env file:
MY_END=awesome
Webpack config:
import { getEnvironment } from "lazy-dotenv-universal";
export default {
// ...
plugins: [
new webpack.DefinePlugin(getEnvironment().webpack),
],
// ...
}Code being bundled by webpack:
console.log(process.env.MY_ENV); // -> "awesome"- raw: Just a plain JS object containing all app settings
- stringified: Plain object but with JSON stringified values
- webpack: For usage with Webpacks Define Plugin
Apache License Version 2.0, January 2004
Copyright 2018
Sebastian Software GmbH