dotenv-dot
Dotenv-dot adds dot-notation variable transformation on top of dotenv. If you find yourself needing to condense environment variables into JSON variables, then dotenv-dot is your tool.
Install
npm install dotenv --savenpm install dotenv-dot --save
Usage
Dot-notation variables are those which contain a dot (.) in their name. Add dot-notation variables to the .env
file like any other variable.
MAIL_CONFIG.service=gmailMAIL_CONFIG.auth.user=noreply@domain.comMAIL_CONFIG.auth.pass=pass1234
Once the dotenv-dot transformer is executed, these variables will be condensed into a new variable named MAIL_CONFIG
.
MAIL_CONFIG='{"service":"gmail","auth":{"user":"noreply@domain.com","pass":"pass1234"}}'
process.env
variables
Transform As early as possible in your application, require dotenv and dotenv-dot. Then, execute dotenv-dot after dotenv.
const dotenv = ;const dotenvDot = transform; const myEnv = dotenv;const results = ; // only updates `process.env`
process.env
and the results of dotenv.config()
Transform If you want to update process.env
and the output of the dotenv.config()
, include the output as a parameter on the dotenv-dot transformer.
const myEnv = dotenv;const results = ; // updates `process.env` and `myEnv`
process.env
Transform variables without affecting It is possible to use dotenv without adding variables to process.env
. Dotenv-dot can also do the same.
const parsedOutput = dotenv; const results = ;
If you already have parsed output you may transform it without using dotenv.
const results = ;
Options
debug
Default: false
You may turn on logging to help debug why certain keys or values are not being set as expected.
const myEnv = dotenv; ;
ignoreProcessEnv
Default: false
You may want to ignore process.env
when transforming the output of the dotenv.config()
. When this option is turned on process.env
will not be consulted or altered.
const myEnv = dotenv; ;
import
?
How do I use dotenv-dot with ;;
Or, if only intending to access process.env
, the two modules should be loading in this order using their auto-imports:
;;
More information about the import
syntax is available on the dotenv repository.
Note: You may set the debug
option using the dotenv debug command line or environment variable. The ignoreProcessEnv
option is irrelevant when using the auto-imports.
.env
file?
How do I add arrays to the Arrays are added by using numbers to indicate the value's index in the resulting array.
THINGS.0='Was eaten by his others'THINGS.1='Thing One'THINGS.3='Wayward Thing Two'