hello! This is my first post (so please go easy on me in the comments 🤣). I’m excited to share a little project I’ve been working on that grew out of a personal need. If you’ve ever struggled to manage configuration in a Node.js application, you might find this little zero-dependency suite interesting. it’s known json to env converter.
What is json to env converter?
json-to-env-converter is an npm package that converts JSON objects into environment variables. It is a lightweight tool designed to help you work with JSON-based configuration by converting it to environment variables and injecting them into process.env; it works well when the configuration may be dynamic, nested, or sourced Scenarios from API or external systems.
Here’s the idea: Instead of manually setting complex or dynamically configured environment variables, you can programmatically load them from JSON objects and access them like any other environment variable.
Why should I build it?
I recently set up a Secrets api for each project I’m personally working on; I’m calling my Secrets api to get the configuration provided in json format at runtime. Not to mention that in modern and large applications, configurations can change based on user location or other dynamic factors. While .env files are great for static settings, they are not easy to work with:
- Nested Structure: Flattening nested JSON into environment variables can get tedious.
- Dynamic sources: Loading configuration at runtime without restarting the application is not trivial.
I built json-to-env-converter to explore solutions to these problems again, it’s more of a project for personal use, it’s definitely not meant to replace tools like dotenv, but to handle slightly different use cases; I figured what’s the harm in open sourcing it and releasing it publicly on npm, I’d be happy if anyone found out about using it.
how it works
Install the package from npm:
npm i json-to-env-converter
Here’s a simple example to show it in action:
basic example
import { jsonToEnv } from 'json-to-env-converter';
const config = {
database: {
host: 'localhost',
port: 5432,
},
};
jsonToEnv(config);
console.log(process.env.DATABASE_HOST); //Output: 'localhost'
console.log(process.env.DATABASE_PORT); //Output: '5432'
This takes a JSON object and converts it into an environment variable. It also flattens the nested key so database.host becomes DATABASE_HOST.
add prefix
To avoid conflicts you can prefix:
jsonToEnv(config, { prefix: 'MYAPP_' });
console.log(process.env.MYAPP_DATABASE_HOST); //Output: 'localhost'
Use case: dynamic configuration
One potential use case for this suite is handling dynamic configuration. For example, suppose you have a global application that can obtain locale-specific settings at runtime. Instead of manually managing .env files for each zone, you can dynamically load the correct settings based on the user’s location:
const regionConfig = {
us: {
dbHost: 'us-db.example.com',
apiUrl: 'https://api.us.example.com',
},
eu: {
dbHost: 'eu-db.example.com',
apiUrl: 'https://api.eu.example.com',
},
};
const region = 'us'; // This could come from a runtime check
jsonToEnv(regionConfig[region], { prefix: 'APP_' });
console.log(process.env.APP_DBHOST); // Output: 'us-db.example.com'
This allows your application to adjust its configuration without restarting or hardcoding values.
Should you use it?
To be honest, I’m still deciding how versatile this bag will be. If you are already familiar with .env files and static configuration, you may not need this tool. But if you are using:
- Configure a dynamic environment that changes at execution time
- Nested JSON objects that need to be converted to flat environment variables
- Program configuration settings from API or external services
If you’re not happy with your current setup, json-to-env-converter might save you some time and is worth a try.
Installation and feedback
If you want to try it out, you can install it from npm:
npm i json-to-env-converter
I’d love to hear your thoughts, feedback or most importantly suggestions for improvements, so feel free to mention it in the comments here or submit a pull request on my github repository https://github.com/neenus/json-to-env. This has been a learning experience for me and I’m excited to see where it goes.
Oh, and one more thing…thank you for reading my first post!