Have you ever used the same environment variables in different projects located on the same machine?
Most of the times we use the same databases or API keys across different projects and we redefine them on .env
for each project.
Problem
We usually use the following structure:
π Main folder/
βββ π Project1/
β βββ π .env.dev
β β {
β β MONGO_URI: mongodb://dev:27017
β β API_KEY: devKey
β β SPECIFIC_PROJECT_KEY: 1111
β β }
β βββ π .env
β {
β MONGO_URI: mongodb://prod:27017
β API_KEY: prodKey
β SPECIFIC_PROJECT_KEY: 2222
β }
βββ π Project2/
βββ π .env.dev
β {
β MONGO_URI: mongodb://dev:27017
β API_KEY: devKey
β SPECIFIC_PROJECT_KEY: 8888
β }
βββ π .env
{
MONGO_URI: mongodb://prod:27017
API_KEY: prodKey
SPECIFIC_PROJECT_KEY: 9999
}
We can see there are two duplicated variables: MONGO_URI
and API_KEY
, appearing a total of 4 times each one.
In case we edit the MONGO_URI
value in a future, we have to edit 4 files, 2 per project.
Solution
π Main folder/
βββ π entor.dev.json
β {
β "MONGO_URI": "mongodb://dev:27017",
β "API_KEY": "devKey"
β }
βββ π entor.prod.json
β {
β "MONGO_URI": "mongodb://prod:27017",
β "API_KEY": "prodKey"
β }
βββ π Project1/
β βββ π entor.dev.json
β β {
β β "SPECIFIC_PROJECT_KEY": "1111"
β β }
β βββ π entor.prod.json
β {
β "SPECIFIC_PROJECT_KEY": "2222"
β }
βββ π Project2/
βββ π entor.dev.json
β {
β "SPECIFIC_PROJECT_KEY": "8888"
β }
βββ π entor.prod.json
{
"SPECIFIC_PROJECT_KEY": "9999"
}
There are no duplicated variables.
If we need to change the MONGO_URI
value in a future, we just need to change it in the file entor.dev.json
and entor.prod.json
located at Main folder/
.
All the projects will get the shared variables just restarting their processes.
How
Entor is a tool for managing environment variables like dotenv, but with a more modern approach.
One of the main features of entor is that it allows you to share environment variables between multiple projects.
Thatβs useful when you are using the same environment variables like database credentials, secrets, API keys, etc.
Main features
- π Shared environment variables
- βοΈ Zero configuration
- βͺοΈ Zero dependencies
- π It uses JSON files
- π¨ Generates environment examples automatically
- πͺΆ Only 17 kB
Using entor
Itβs pretty simple to use. First, install the package withnpm install entor
Next create two files in the root of your project:
// entor.dev.json
{
"SPECIFIC_PROJECT_KEY": "1111"
}
and
// entor.prod.json
{
"SPECIFIC_PROJECT_KEY": "8888"
}
Then add the following line to your index.js
file, just after the imports/requires:
// requires...require("entor")();// more code...
Now you can launch your file with:node index.js --env=dev
Your process.env.SPECIFIC_PROJECT_KEY
will be 1111
.
If you change dev for prod:node index.js --env=prod
Your process.env.SPECIFIC_PROJECT_KEY
will be 8888
.
Thatβs all you need to do to configure entor to use basic environment variables.
By default entor will set the environment variables from the file entor.<env>.json
using the --env
flag value.
The --env
flag can be customizable on the config.
But what about sharing environment variables?
Shared environment variables
You just need to add some config on the index.js
:
require("entor")({
sharedEnvPath: "C:/Work/Main folder",
});
And create the shared files:
// entor.dev.json
{
"MONGO_URL": "mongodb://dev:27017"
}
and
// entor.prod.json
{
"MONGO_URL": "mongodb://prod:27017"
}
Relaunch your file withnode index.js --env=local
And youβll see that:
process.env.SPECIFIC_PROJECT_KEY
will be1111
.process.env.MONGO_URI
will bemongodb://dev:27017
.
Or with:node index.js --env=prod
process.env.SPECIFIC_PROJECT_KEY
will be8888
.process.env.MONGO_URI
will bemongodb://prod:27017
.
Notice that entor will override shared values with local ones.
This library can be a valid dotenv alternative that can save time to certain users.
If you want to know more about itβs config you can view itβs full readme at NPM.