Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/4catalyzer/env
https://github.com/4catalyzer/env
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/4catalyzer/env
- Owner: 4Catalyzer
- License: mit
- Created: 2018-01-24T16:19:30.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-10-21T18:17:02.000Z (2 months ago)
- Last Synced: 2024-10-22T09:37:25.357Z (2 months ago)
- Language: TypeScript
- Size: 561 KB
- Stars: 0
- Watchers: 6
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @4c/env
A small set of utils for working with environment variables in node and webpack projects. Wraps the excellent [`getEnv`](https://github.com/ctavan/node-getenv/blob/master/README.md) as well
## Usage
```js
import * as Env from '@4c/env';
```### Getting and parsing variables
`env` wraps and reexports [`getEnv`](https://github.com/ctavan/node-getenv/blob/master/README.md) as `get()`.
```js
Env.get('NODE_ENV'); // 'production'// Fails hard on missing variables
Env.get('NOT_SET_VAR'); // Error// Fall back to a default if needed
Env.get('NOT_SET_VAR', 'default_value'); // 'default_value'// Parsing
Env.get.int('MAX_SUBSCRIPTIONS', 4);process.env.BOOLISH = 1;
Env.get.boolish('BOOLISH'); // true
```### Loading `.env` files
`env` will load and parse `.env` files, by environment. We use the naming convention `variables-{ENV}.env` to split out variables by different environment. The default is `dev`.
```js
Env.load(); // adds variables-dev.env to process.env
Env.load('test'); // adds variables-test.env to process.env
```There is also a utility for passing enviroment variables to webpack's `DefinePlugin` allowing instances of `process.env.FOO` in front-end code to be replaced with a specific compile-time value.
```js
// returns variables-test.env mapped to an object keyed by process.env.[foo] for use in webpack
new webpack.DefinePlugin(Env.define('test'));
```