μConfig: A Lightweight alternative to .env

Lev Gelfenbuim
4 min readJun 8, 2021

Here’s a short story about a new package made public some time ago. You might find it extremely useful if you work with configuration files more often than not. You will also get an idea of what the μ in μConfig stands for :)

It is 00:19 now 😴

One Saturday, I pushed the first commit to https://github.com/levz0r/uconfig. The package was published to NPM 30 minutes later, and the project is now complete. This is my shortest project to date. I think μConfig will still save you valuable time in your future (or current) project where static configuration files are used. Let’s see why.

The art of wrapping code with more code

To be honest, μConfig does nothing:

  1. Reading and parsing a JSON file.
  2. Accessing the nested fields of the parsed JSON file.

In order to achieve the first milestone, NodeJs’ built-in fs module was used to read a file and JSON.parse() to serialize it to a JavaScript object.

The second milestone was a quick-win by using a single external dependency on lodash.get.

See? All μConfig does is read, parse, and access a JSON file.

Connect these features together and you get a multi-environment JSON configuration reader 🤯

A real-world scenario

Imagine there is a JS module running on the back-end. This module accesses the database layer (in jargon we call it DAL). Of course, there are no connection strings, credentials, or sensitive information stored in the module itself. Instead, there is some kind of external “warehouse” where this kind of stuff is stored. The warehouse can be a 3rd party service, a set of environmental variables (DB_HOST, DB_USER, DB_PASSWORD, etc.), or a local file. Don’t judge! Every company/team has its needs and constraints. While there are design patterns and best practices, sometimes the development team’s velocity matters — especially in the start-up world. Now, it’s critical to mention that if a development team chooses a file system as their means of sensitive information storage, precautions must be taken: for example, it must never hit the SCM! So the best thing for the team is to add all their sensitive configuration files to .gitignore — no excuses! Then there’s the dilemma of how to share these configuration files between team members?! This is something I will save for another story.

Back to our JS module. To make matters worse, the module is used in multiple environments:

  • development: Runs locally on the developer’s computer
  • staging: Runs on staging VM
  • production: Runs on production VM

(feel free to replace VM with Docker or Kubernetes)

Of course, the environment itself is stored in a designated NODE_ENV environment variable.

Therefore, to work in all these environments, there might be 3 configuration files (development.config.json, staging.config.json, and production.config.json) looking like:

or a single configuration file with 3 sections:

I had the opportunity to work with both approaches and I believe the second variation where a single configuration file is used is easier to maintain. For simplicity, I’ll refer to this option for the rest of this story.

Get to the point already 😄

In the case of a single configuration file with multiple environments, this is how μConfig can read the configuration file:

The “magic” lies on line 7, where we instruct μConfig to read the DB field, based on the current environment (development, staging, production).

In case we prefer to store 3 configuration files, we could either use 3 instances of μConfig and work with the correct instance, as in the attached example (not recommended for use in production!), or we could load the correct configuration file using the following approach:

Yes, μConfig can fall back (thanks to Lodash) to values when a configuration key is not found.

Summary

If you or your team prefer to store configurations on a file system, μConfig is the right candidate for you. You are welcome to fork the repository and extend it to your specific needs.

The story of μConfig demonstrates that solutions don’t always have to be complicated.

Oh…! Almost forgot! μ is for micro. The package weighs just under 4KB 🤏

What about you? How are you using configurations in your projects? Share your thoughts!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Written by Lev Gelfenbuim

Tech enthusiast, founding member of Tastewise and Apeiros, author of the book Web Testing with Cypress (https://webtestingwithcypress.com/).

No responses yet

Write a response