Dotenv that works with hot reloading.
# Using defaults:
npx dotenv-hot install
# Or, specifying directories:
npx dotenv-hot install --env-module-dir client/src --scripts-dir lib
# See `npx dotenv-hot --help` for full usage instructions.
After running this, commit the staged changes.
npx dotenv-hot watch
Or, install with yarn add -D dotenv-hot
and add to your package.json
:
{
"scripts": {
"watch-env": "dotenv-hot watch"
}
}
To remove the added files and the git fiter, run:
npx dotenv-hot uninstall
-
It creates several files:
-
.envFromFile.[ts|js]
— whenwatch
is run, the values from.env
are loaded into this file as JS variable declarations. This file is fully maintained bydotenv-hot
and should not be modified. By default this is added in<gitRepoRoot>/src
, but can be customized with the--env-module-dir
flag. -
env.[ts|js]
— this file handles importing the vars from.envFromFile.[ts|js]
, falling back on the values inprocess.env
, and exporting. This is where you'll import env vars from, and you'll need to add new lines in this file when you have new env vars that you want to use in this way. By default this is also added in<gitRepoRoot>/src
, but is also customizable with the--env-module-dir
flag. -
git-filter-dotenv-hot-env-module.sh
— this is a script for use bygit
as afilter
. This file also does not need to be modified. By default this is added in<gitRepoRoot>/scripts
, but can be customized with the--scripts-dir
flag.
-
-
It tells
git
to use the filter inscripts
for the.envFromFile.[ts|js]
file. This prevents the local changes that are made to this file from being seen by git.
- It monitors the contents of
.env
, and if they change it updates.envFromFile.[ts|js]
By default, any .env
files will be ignored by this package in prod
, since the dotenv-hot watch
process will not be running. However, if you need to load values from a .env
file in prod (no hot reloading here though), you can add dotenv
and uncomment the relevant lines in env.[ts|js]
.