Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jacobwgillespie/the-environment
🌱 Simple Node.js package for environment variables
https://github.com/jacobwgillespie/the-environment
Last synced: 2 months ago
JSON representation
🌱 Simple Node.js package for environment variables
- Host: GitHub
- URL: https://github.com/jacobwgillespie/the-environment
- Owner: jacobwgillespie
- License: mit
- Created: 2022-01-10T02:56:31.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-07-09T18:17:34.000Z (6 months ago)
- Last Synced: 2024-09-14T13:28:23.503Z (3 months ago)
- Language: TypeScript
- Homepage:
- Size: 98.6 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🌱 the-environment
[![CI](https://github.com/jacobwgillespie/the-environment/actions/workflows/ci.yml/badge.svg)](https://github.com/jacobwgillespie/the-environment/actions/workflows/ci.yml)
[![npm](https://img.shields.io/npm/dm/the-environment.svg)](https://www.npmjs.com/package/the-environment)
[![npm](https://img.shields.io/npm/v/the-environment.svg)](https://www.npmjs.com/package/the-environment)
![Powered by TypeScript](https://img.shields.io/badge/powered%20by-typescript-blue.svg)Simple Node.js package for reading environment variables, TypeScript support included.
## Installation
```
$ pnpm add the-environment
$ yarn add the-environment
$ npm install the-environment
```## Usage
The `optional` import mirrors the behavior of `process.env`, returning either the string value or `undefined` if the variable is unset:
```typescript
import {optional} from 'the-environment'// Fetch an optional environment variable:
console.log(optional.NODE_ENV)
```The `required` import either returns the string value or throws a `MissingEnvironmentVariableError` if the variable is unset:
```typescript
import {required, MissingEnvironmentVariableError} from 'the-environment'// Fetch a required environment variable, throw an error if not present:
console.log(required.NODE_ENV)try {
console.log(required.DOES_NOT_EXIST)
} catch (e) {
if (e instanceof MissingEnvironmentVariableError) {
//-> Missing required environment variable: DOES_NOT_EXIST
console.log(e.message)//-> DOES_NOT_EXIST
console.log(e.variable)
}
}
```If you'd like to import both `optional` and `required`, you can also use `import * as` to name it `env`:
```typescript
import * as env from 'the-environment'console.log(env.optional.NODE_ENV)
console.log(env.required.NODE_ENV)
```## License
MIT License, see `LICENSE`.