Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/harukayamamoto0/jsonforenv
a json reader for env
https://github.com/harukayamamoto0/jsonforenv
dotenv env jsonforenv
Last synced: about 1 month ago
JSON representation
a json reader for env
- Host: GitHub
- URL: https://github.com/harukayamamoto0/jsonforenv
- Owner: HarukaYamamoto0
- License: mit
- Created: 2023-06-18T06:11:48.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-05T02:04:19.000Z (about 1 year ago)
- Last Synced: 2023-11-05T21:03:45.900Z (about 1 year ago)
- Topics: dotenv, env, jsonforenv
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/jsonforenv
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://img.shields.io/github/stars/HarukaYamamoto0/jsonforenv?color=informational)
![GitHub fork](https://img.shields.io/github/forks/HarukaYamamoto0/jsonforenv?color=informational)
![GitHub license](https://img.shields.io/github/license/HarukaYamamoto0/jsonforenv?color=informational)## Installation
You can install it via npm or yarn:
```bash
npm install jsonforenv
# Or
yarn add jsonforenv
```## Usage
First create a `.json` file, in the location you want.
```json
{
"DATABASE_URL": "mongodb://localhost/mydb",
"SECRET_KEY": "mysecretkey",
"DEBUG_MODE": true
}
```import the library into your code:
```javascript
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:
```javascript
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:
```javascript
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:
```javascript
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`:
```javascript
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](LICENSE).
## Assignments
- **Logo:** Gear icons created by Freepik - Flaticon