How to share environment variables between projects

Icaruk
3 min readNov 28, 2021

--

Photo by Toa Heftiba on Unsplash

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_URIand 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_URIvalue 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 with
npm 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.

Photo by Xan Griffin on Unsplash

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 with
node index.js --env=local

And you’ll see that:

  • process.env.SPECIFIC_PROJECT_KEY will be 1111.
  • process.env.MONGO_URI will be mongodb://dev:27017.

Or with:
node index.js --env=prod

  • process.env.SPECIFIC_PROJECT_KEY will be 8888.
  • process.env.MONGO_URI will be mongodb://prod:27017.

Notice that entor will override shared values with local ones.

Photo by Lubo Minar on Unsplash

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.

--

--

Icaruk
Icaruk

Written by Icaruk

0 Followers

Fullstack developer

No responses yet