Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/iguntur/prop-env

Getter and Setter 'process.env' variable from '.env' file
https://github.com/iguntur/prop-env

env library node npm process utility

Last synced: about 9 hours ago
JSON representation

Getter and Setter 'process.env' variable from '.env' file

Awesome Lists containing this project

README

        

# prop-env [![Build Status](https://travis-ci.org/iguntur/prop-env.svg?branch=master)](https://travis-ci.org/iguntur/prop-env)

> Getter and Setter 'process.env' variable from '.env' file

## Install

```
$ npm install --save prop-env
```

## Usage

```
├── .env
├── index.js
└── package.json
```

```
// '.env' file
TWITTER_CONSUMER_KEY=gNiRPbQNG
TWITTER_CONSUMER_SECRET=95tpBUhR
```

```js
const env = require('prop-env');

console.log(env('TWITTER_CONSUMER_KEY'));
//=> 'gNiRPbQNG'

console.log(env('TWITTER_CONSUMER_SECRET'));
//=> '95tpBUhR'

// set default value
console.log(env('TWITTER_TOKEN_KEY'));
//=> undefined

console.log(env('TWITTER_TOKEN_KEY', 'Umst0AZIiwex36'))
//=> 'Umst0AZIiwex36'

console.log(env('TWITTER_TOKEN_KEY'));
//=> 'Umst0AZIiwex36'

console.log(env('TWITTER_TOKEN_SECRET'));
//=> undefined

console.log(env('TWITTER_TOKEN_SECRET', 'LxU9qi9hAe'));
//=> 'LxU9qi9hAe'

// Can not override the value
console.log(env('TWITTER_TOKEN_SECRET', 'fooBar'));
//=> 'LxU9qi9hAe'

console.log(env('TWITTER_TOKEN_SECRET'));
//=> 'LxU9qi9hAe'

console.log(process.env);
/*
{
// ...
"TWITTER_CONSUMER_KEY": "gNiRPbQNG",
"TWITTER_CONSUMER_SECRET": "95tpBUhR",
"TWITTER_TOKEN_KEY": "Umst0AZIiwex36",
"TWITTER_TOKEN_SECRET": "LxU9qi9hAe",
// ...
}
*/
```

## API

### env(key, value, envFile)

Returns `string` value of process.env `key`

#### key

Type: `string`

process.env key

#### value

Set env value

#### envFile

Type: `string`

Default: `process.cwd() + '/.env'`

Set env path

## License

MIT © [Guntur Poetra](http://iguntur.starmediateknik.com)