Let's say you want to store development environment variables in dev.env, production environment variables in
prod.env, and then somehow use those to launch your server. One common way to launch your server is with the env
command:
env $(< dev.env | xargs) runserver
This is a start, but it's pretty lackluster:
it overwrites any variables that are already set in your environment
it doesn't work on windows (and may not work in terminals besides bash...I have no idea)
it would be nice avoid wrapping dev.env in $(<| xargs)
if you want to source multiple files, you have to type even more punctuation
you can't dynamically construct any values with code
You may use a node .js script that exports an object where all property values are strings, or a function that will be
called with a small API. This is especially useful if you need to dynamically create default values for environment
variables with arbitrary code. If you need to refer to existing environment variables or defaults applied from prior files,
you are encouraged to use the function form.
var path = require('path')module.exports = function (env) { env.setDefaults({ BUILD_DIR: path.resolve(__dirname, '..', 'build'), REDIS_HOST: 'localhost', REDIS_PORT: '6379', }) if (env.get('TARGET')) { env.setDefault( 'BUILD_DIR', path.resolve(exports.BUILD_DIR, process.env.TARGET) ) }}
(or)
module.exports = { BUILD_DIR: path.resolve(__dirname, '..', 'build'), REDIS_HOST: 'localhost', REDIS_PORT: '6379',}// referring to process.env is discouraged because it will work differently when defaultenv// is run with the --noExport option.if (process.env.TARGET) { module.exports.BUILD_DIR = path.resolve( module.exports.BUILD_DIR, process.env.TARGET )}
JS Env File API
If your .js file exports a function, it will be called with an object with the following methods:
get(key: string): ?string
Gets the value of an environment variable.
setDefault(key: string, value: string)
Sets the value of an environment variable if it is not already set.
setDefaults(defaults: {[key: string]: string})
Calls setDefault for each key-value pair in defaults.
.json files
You may use a JSON file that contains an object where all property values are strings:
if FOO appears in more than one of the env files, the leftmost file in your arguments wins
if FOO is already defined in the environment defaultenv gets run with, defaultenv won't overwrite it
once an environment variable gets set to the empty string, defaultenv won't overwrite it
Exporting values
Sometimes it's more convenient to just export a bunch of variables to your terminal session so you can run a bunch of
commands with those variables instead of running each command via defaultenv.
The -p or --print option will make defaultenv output a bash script that will export the values it loaded from
the envfiles:
> defaultenv -p foo.envexport FOO=foo
So if you wrap this in $( ) it will export them to your terminal session:
> $(defaultenv -p foo.env)> echo $FOOfoo
Options
-f, --force
Overwrite existing values for environment variables (by default, existing values will be preserved)
This does not currently apply to variables loaded via --dotenv option.
If true, overwrite existing values for environment variables (by default, existing values will be preserved)
This does not currently apply to variables loaded via --dotenv option.