# @ifaxity/env

> Node 6 or above is required as ES6 is needed.

Latest version **1.2.0** (published 2019-12-22) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @ifaxity/env
pnpm add @ifaxity/env
yarn add @ifaxity/env
bun add @ifaxity/env
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2019-12-22 |
| First published | 2018-11-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6 |
| Dependencies | 0 |
| Unpacked size | 7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Christian Norrman |
| Maintainers | ifaxity |

## Links

- npm: https://www.npmjs.com/package/@ifaxity/env
- npm.io page: https://npm.io/package/@ifaxity/env

## Recent versions

- 1.2.0 (latest) — 2019-12-22
- 1.1.1 — 2019-10-30
- 1.1.0 — 2019-10-24
- 1.0.4 — 2019-08-31
- 1.0.3 — 2019-01-23
- 1.0.1 — 2018-12-08
- 1.0.0 — 2018-11-26

## README

@ifaxity/env
============

## The minimal enviroment variable package for modern Node.js environments. No dependencies.

Node 6 or above is required as ES6 is needed.

Code heavily based on [dotenv package](https://github.com/motdotla/dotenv) on npm

---------------
## Installation

`npm install @ifaxity/env --save`

or if you use yarn

`yarn add @ifaxity/env`

--------
## Usage

To use the module just require it like this

`const env = require('@ifaxity/env');`

------
## API

Example .env file:
```sh
# Node environment
NODE_ENV=production

# Example database connect options
DB_HOST='localhost'
DB_PORT=27017

# Decimals can also be used
PI=3.14

# Booleans too
DEBUG=true

# Multiline strings (must be double quoted strings)
MULTILINE="Hello\nWorld!"
```

### [env](#env)

The module is a proxy object so you can use this to get an environment variable and also convert it to the correct type. As node.js doesn't support `process.env` to have any other values other than strings for now.

Types parsed are limited to `boolean, number, string` for now.
If the variable doesnt exist then `null` is returned.

Like this:

Consider the variables in the example file above

```js
const env = require('@ifaxity/env');
env.NODE_ENV // same as process.env.NODE_ENV.

env.PORT // returns 3000 (as a number)
env.BOOL // returns true (as a boolean)

// Can also use the object deconstruction syntax
const { NODE_ENV, PORT, TRUE } = require('@ifaxity/env');
```


However `parse` & `config` is reserved as functions in this module.

### [env.config([, opts])](#config)

Configures and loads the environment variables from a file.

Returns the parsed variables in a form of an object.
However unlike the module these variables are not proxied.

#### Parameters
* opts {Object} - Optional options. If any of the optional options is not of a valid type or if its value is not valid then a `TokenError` will be thrown.

  * `encoding {String}` - Encoding to use when reading the env files. Encoding types are defined in the [nodejs documentation](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings)

  * `path {String|Array}` - Full path to the directory to look for files. Files are loaded in order of .env, .env.${NODE_ENV} and .env.local. If a overwriting the earlier files if there are duplicate keys. Default value is the current working directory.

  * `env {Object}` - Object to set the config options to. Default value is process.env.

  * `defaults {Object}` - Object to set default values if a key doesn't exist in the .env file. Default value is null.

#### Basic Usage

```js
const env = require('@ifaxity/env');

// Basic usage
env.config();

// Custom path & variable
const envVars = env.config({
  path: __dirname,
  env: {},
});
```

### [env.parse(data)](#parse)
Parses a key=value pair string to an object of variables

Returns an object of the `key=value` pairs of the data

#### Parameters
* data {String} - Data to parse in a `key=value` pair format.

#### Basic Usage

```js
const env = require('@ifaxity/env');

const envVars = env.parse(fs.readFileSync(process.cwd() + '/.env', 'utf8'));
```

---
_Source: https://npm.io/package/@ifaxity/env · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
