JSPM

jsonforenv

1.2.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q24553F
  • License MIT

Transform JSON into Environment Variables: jsonforenv - Simplifying Environment Variable Configuration

Package Exports

  • jsonforenv
  • jsonforenv/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jsonforenv) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

jsonforenv

jsonforenv

jsonforenv is a simple tool that simplifies the configuration of environment variables. With it, you can easily load environment variables from a JSON configuration file, making your application configuration more organized and flexible.

GitHub stars GitHub fork GitHub license

Installation

You can install it via npm or yarn:

npm install jsonforenv
# Or
yarn add jsonforenv

Usage

First create a .json file, in the location you want.

{
  "DATABASE_URL": "mongodb://localhost/mydb",
  "SECRET_KEY": "mysecretkey",
  "DEBUG_MODE": true
}

import the library into your code:

import { loadEnvFromFile, loadEnvFromFolder } from "jsonforenv";

If you want to load just one configuration file, call the loadEnvFromFile function passing the path to the JSON file containing the environment variables:

loadEnvFromFile("path/to/your/config.json");

Now if you want to load several configuration files that are inside a folder, call the loadEnvFromFolder function passing the path to the folder containing the .json files:

loadEnvFromFolder("path/to/your/folder");

If the JSON file found is valid, the environment variables will be loaded based on the settings provided in the file, as in the example below:

import { loadEnvFromFile } from "jsonforenv";
loadEnvFromFile();

console.log(process.env.DATABASE_URL); // mongodb://localhost/mydb
console.log(process.env.SECRET_KEY); // mysecretkey

FAQ

Does it throw errors?

jsonforenv handles errors during the JSON file loading process. If errors occur when reading the file, parsing the JSON, or if the file is not found, the library will throw appropriate errors to inform the user of the problem.

Here is an example of how to catch errors when using jsonforenv:

try {
  loadEnvFromFile("path/to/your/config.json");
} catch (error) {
  console.error(error.message);
  // Do something about the error if necessary
}

Contributing

Contributions are welcome! Feel free to open issues and submit pull requests to improve this package.

License

This project is licensed under the MIT License.

Assignments