- Very simple to setup.
- Automatically assigns configured values on top of
process.env
. - Automatically supports overriding configured values via Environment Variables.
- Promotes best practices (FAQ):
- Have only one config file.
- Don't commit the file to version control.
- Supported by Create React App out of the box.
- Can't use dynamic values. e.g.:
- Referencing other variables (can use dotenv-expand for that).
- Importing constants from another file.
- Setting a value by running a JavaScript expression like
Date.now()
.
- All values are translated into strings.
- That's just how
process.env
behaves when assigning values on it. e.g.:process.env.ENABLED = true; process.env.NUM = 3; console.log(process.env.ENABLED, process.env.NUM); // 'true' '3'
- That's just how
- Very flexible.
- Can use any data structure.
- Supports overriding configured values via Environment Variables (although requires a mapping config).
- Supports multi deployment/instance configs out of the box.
- Doesn't automatically map config values on
process.env
.- That's because it takes a different approach to configs.
- Environment Variables overrides require a non trivial mapping config (Custom Environment Variables).
- Doesn't support ES Modules syntax (although TypeScript is supported).
Tips
Create React App
.env
out of the box.node-config
, can use react-app-rewired with aconfig-overrides.js
file:Usage in Frontend Projects
For both solutions, Webpack's EnvironmentPlugin can be used to map the config values on top of
process.env
to be available in the client side code.EnvironmentPlugin Notes
JSON.stringify
and thenJSON.parse
so non-string values are supported (if usingnode-config
).process.env
in the client and that Object behaves like regular JS and not limited to howprocess.env
works in Node.js.Jest Configuration
Requires additional configuration since Webpack doesn't run when running Jest.
Config Setup Before Startup
Both solutions can be executed before the target process runs.
.env
preloadnode-config
This will require
custom-environment-variables.js
to be configured.Running before Jest