Home Web Front-end JS Tutorial From JSON to Environment Variables: Introducing json-to-env-converter

From JSON to Environment Variables: Introducing json-to-env-converter

Jan 02, 2025 pm 05:54 PM

From JSON to Environment Variables: Introducing json-to-env-converter

Hi there! This is my first-ever article (so go easy on me in the comments ?). I’m excited to share a little project I’ve been working on which came out of a personal need. If you’ve ever wrestled with managing configurations in your Node.js application, you might find this small zero dependency package interesting. It’s called 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’s a lightweight tool designed to help you handle JSON-based configurations by converting them into environment variables and injecting it into process.env; it's intended for scenarios where configurations might be dynamic, nested, or sourced from APIs or external systems.

Here’s the idea: Instead of manually setting environment variables for complex or dynamic configurations, you can programmatically load them from a JSON object and access them just like any other environment variable.

Why Did I Build It?

I've recently built a secrets api and for every project that I'm personally working on; I'm calling my secrets api to get my config at runtime which is provided in json format. Also not to mention in modern and large apps config can change depending on the user location, or other dynamic factors. While .env files are great for static setups, they don’t easily handle:

  • Nested structures: Flattening nested JSON into environment variables can get tedious.
  • Dynamic sources: Loading configurations at runtime without restarting the app isn’t straightforward.

I built json-to-env-converter to explore a way to address these issues once again it was more of project for personal use and it’s definitely not meant to replace tools like dotenv, but rather to handle a slightly different use case; and I thought what's the harm in making it open source and publishing it publicly on npm and I would be so happy if anyone found use of it.

How It Works

Install the package from npm:

npm i json-to-env-converter
Copy after login
Copy after login

Here’s a simple example to show what it does:

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'
Copy after login
Copy after login

This takes a JSON object and converts it into environment variables. It also flattens nested keys, so database.host becomes DATABASE_HOST.

Adding a Prefix

To avoid collisions, you can add a prefix:

jsonToEnv(config, { prefix: 'MYAPP_' });

console.log(process.env.MYAPP_DATABASE_HOST); //Output: 'localhost'
Copy after login

A Use Case: Dynamic Configurations

One potential use case for this package is handling dynamic configurations. For example, imagine you have a global application that fetches region-specific settings at runtime. Instead of manually managing .env files for each region, you could dynamically load the right settings based on the user’s location:

npm i json-to-env-converter
Copy after login
Copy after login

This allows your app to adapt its configuration without requiring a restart or hardcoded values.

Should You Use It?

Honestly, I’m still deciding on how broadly useful this package might be. If you’re already comfortable with .env files and static configs, you might not need this tool. But if you’re working with:

  • Dynamic environments where configs change at runtime
  • Nested JSON objects that need to be converted into flat environment variables
  • Programmatic configuration setups sourced from APIs or external services

and not happy with your current setup then json-to-env-converter might save you some time and worth a try.

Installation and Feedback

If you’re curious to give it a try, you can install it from npm:

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'
Copy after login
Copy after login

I’d love to hear your thoughts, feedback, or most importantly suggestions for improvement so feel free to mention in the comments here or submit pull requests on my github repo https://github.com/neenus/json-to-env. This is a learning experience for me, and I’m excited to see where it goes.

Oh and one more thing... thanks for reading my first article!

The above is the detailed content of From JSON to Environment Variables: Introducing json-to-env-converter. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

See all articles