env-get
Version:
simple tool to wrap fetching of env variables in node.js
39 lines (29 loc) • 1.31 kB
Markdown
Simple utility to get environment variables with an optional default.
Throws an error if the variable is not found and no default is supplied.
This utility was designed to support twelve-factor applications and the strict separation of code and config.
[][12-factor-config-url]
[![NPM Version][npm-image]][npm-url]
[![NPM Monthly][downloads-month-image]][npm-url]
[![Test Status][test-image]][test-url]
[![Coverage Status][coverage-image]][coverage-url]
```javascript
const env = require('env-get');
// PORT not set
const port = env.get('PORT', 4000);
console.log(port); // 4000
// TOKEN not set
const token = env.get('TOKEN'); // throws Error
// HOSTNAME set
const hostname = env.get('HOSTNAME');
console.log(hostname); // blaing.io
```
[]:https://12factor.net/config
[]: https://npmjs.org/package/env-get
[]: https://circleci.com/gh/billylaing/env-get/tree/master
[]: https://coveralls.io/github/billylaing/env-get
[]: https://img.shields.io/npm/v/env-get.svg
[]: https://img.shields.io/npm/dm/env-get.svg
[]: https://img.shields.io/circleci/project/billylaing/env-get.svg?label=tests
[]: https://coveralls.io/repos/github/billylaing/env-get/badge.svg