dotenv-placeholder v1.7.1
.env + .env.placeholder
This package is a wrapper for dotenv to make it work with Git more easily
It makes your life easier with your git repository if you decide to add .env to the .gitignore of your project (very good decision by the way, that's the way it should be)
What does it do ?
If no .env.placeholder file is found, it will create one next to your .env file, and will put the keys indicated in your .env file, without the values.
If no .env file is found, it will create one next to your .env.placeholder file, and will copy in it the content found in .env.placeholder
It warns you if you have empty fields in .env
You can add default values to the .env.placeholder and it will add this values to the .env file EXCEPTED if there is already a value given in .env for this key
You can add a <random:♥> value in .env.placeholder (where ♥ represents the length of the random string you want to generate), or just <random>, which is equivalent to <random:30>.
Example
# .env
KEY=secret_value
# .env.placeholder
ADDITIONAL_KEY=
KEY_WITH_DEFAULT=default_value
A_RANDOM_KEY=<random>Will transform into :
# .env
KEY=secret_value
ADDITIONAL_KEY=
KEY_WITH_DEFAULT=default_value
A_RANDOM_KEY=k7RDE_aBJxAE==qchk720DH-4Rzc
# .env.placeholder
ADDITIONAL_KEY=
KEY_WITH_DEFAULT=default_value
A_RANDOM_KEY=<random:28>
KEY=And it will produce the following messages in the console :
info .env.placeholder → .env : [ 'ADDITIONAL_KEY', 'KEY_WITH_DEFAULT', 'A_RANDOM_KEY' ]
info .env.placeholder ← .env : [ 'KEY' ]
warning empty keys found in .env : [ 'ADDITIONAL_KEY' ]How to use it
In your Node.js script
const dotenv = require("dotenv-placeholder");
dotenv.config(); // this will trigger the buildIn your package.json
This is my prefered way, because when someone will clone your project, and then run npm i, it will automatically create the .env file, allowing the person to customize the .env before running your project
{
"scripts": {
"postinstall": "dotenv-build --path=./config/.env --placeholderPath=./config/.env.placeholder"
/* all options are optionnal */
}
}