# dotenv-parse-variables

> Parse dotenv files for Boolean, Array, and Number variable types, built for CrocodileJS

Latest version **2.0.0** (published 2021-02-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install dotenv-parse-variables
pnpm add dotenv-parse-variables
yarn add dotenv-parse-variables
bun add dotenv-parse-variables
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2021-02-12 |
| First published | 2016-07-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/dotenv-parse-variables) |
| Module format | CommonJS |
| Node | >= 8.3.0 |
| Dependencies | 2 |
| Unpacked size | 19.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Nick Baugh |
| Maintainers | niftylettuce |
| Keywords | array, boolean, check, convert, dot, dotenv, env, number, parse, variables |

## Links

- npm: https://www.npmjs.com/package/dotenv-parse-variables
- Repository: https://github.com/niftylettuce/dotenv-parse-variables
- Issues: https://github.com/niftylettuce/dotenv-parse-variables/issues
- npm.io page: https://npm.io/package/dotenv-parse-variables

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ^4.3.1
- [is-string-and-not-blank](https://npm.io/package/is-string-and-not-blank.md) ^0.0.2

## Alternatives

- [replicas-cli](https://npm.io/package/replicas-cli.md) — 3.0K weekly downloads
- [env-contract](https://npm.io/package/env-contract.md) — 133 weekly downloads
- [@openveo/api](https://npm.io/package/@openveo/api.md) — 61 weekly downloads
- [@ryniaubenpm2/cumque-error-reiciendis](https://npm.io/package/@ryniaubenpm2/cumque-error-reiciendis.md) — 54 weekly downloads
- [ts-global-type-extra](https://npm.io/package/ts-global-type-extra.md) — 11 weekly downloads

## Recent versions

- 2.0.0 (latest) — 2021-02-12
- 1.0.1 — 2020-09-15
- 1.0.0 — 2020-09-15
- 0.3.1 — 2020-07-10
- 0.3.0 — 2020-05-04
- 0.2.3 — 2019-09-09
- 0.2.2 — 2019-07-11
- 0.2.1 — 2019-07-02
- 0.2.0 — 2018-05-22
- 0.1.0 — 2017-08-23
- 0.0.2 — 2017-08-04
- 0.0.1 — 2016-07-30

## README

# dotenv-parse-variables

[![build status](https://travis-ci.com/niftylettuce/dotenv-parse-variables.svg)](https://travis-ci.com/niftylettuce/dotenv-parse-variables)
[![code coverage](https://img.shields.io/codecov/c/github/niftylettuce/dotenv-parse-variables.svg)](https://codecov.io/gh/niftylettuce/dotenv-parse-variables)
[![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![made with lass](https://img.shields.io/badge/made_with-lass-95CC28.svg)](https://lass.js.org)
[![license](https://img.shields.io/github/license/niftylettuce/dotenv-parse-variables.svg)](LICENSE)

> Parse dotenv files for `Boolean`, `Array`, and `Number` variable types, built for [Lad][] and [Forward Email][fe].


## Table of Contents

* [Install](#install)
* [Example](#example)
* [Usage](#usage)
* [Options](#options)
* [Contributors](#contributors)
* [License](#license)


## Install

[npm][]:

```sh
npm install dotenv-parse-variables
```

[yarn][]:

```sh
yarn add dotenv-parse-variables
```


## Example

Imagine you have a configuration file at `.env` with the following:

```bash
FOO=bar
BAZ=2
BEEP=false
BOOP=some,thing,that,goes,wow
# note how we use an asterisk here to turn off the parsing for this variable
BLEEP=false*
# note how we use an asterisk in the array to turn off parsing for an array key value
PING=ping,true*,2,100
# note a string between bacticks won't be parsed
PONG=`some,thing,that,goes,wow`
```

After using this plugin, the environment variables are parsed to their proper types.

To test it out, simply log the returned object in your console:

```js
console.log(env);
```

And you'll see that it outputs the properly parsed variable types:

```js
{
  // String
  FOO: 'bar',
  // Number
  BAZ: 2,
  // Boolean
  BEEP: false,
  // Array
  BOOP: [ 'some', 'thing', 'that', 'goes', 'wow' ],
  // NOTE: this was not parsed due to the * asterisk override above
  BLEEP: 'false',
  // NOTE: only the "true*" above was opted out through the use of an asterisk
  PING: [ 'ping', 'true', 2, 100 ],
  // NOTE: this was not parsed because the string was between bacticks
  PONG: 'some,thing,that,goes,wow'
}
```

If your configuration line ends in `*` it will not be parsed by this package, which allows you to keep values as the `String` variable type if needed. Also when you encapsulate a value between bacticks e.g. \`value\`, the value won't be parsed and it will return as a `String` variable. This can be used in situations where you for example have a `,` inside your string and it should not be parsed as an array.


## Usage

This package works well with [dotenv][dotenv], however we also recommend to use [dotenv-extended][dotenv-extended] and [dotenv-expand][dotenv-expand] as we do in [Lad][].  You could also simply just use [Lad][] or [@ladjs/env][] specifically.

> Example with `dotenv`:

```js
const dotenv = require('dotenv');
const dotenvParseVariables = require('dotenv-parse-variables');

let env = dotenv.config({})
if (env.error) throw env.error;
env = dotenvParseVariables(env.parsed);

console.log(env);
```

> Example with `dotenv-extended` (which supports a well-defined `.env` file) and `dotenv-expand` (which supports variable interpolation):

```js
const dotenvExtended = require('dotenv-extended');
const dotenvMustache = require('dotenv-mustache');
const dotenvParseVariables = require('dotenv-parse-variables');

let env = dotenvExtended.load({
  silent: false,
  errorOnMissing: true,
  errorOnExtra: true
});
env = dotenvMustache(env);
env = dotenvParseVariables(env);

console.log(env);
```

If you don't want to use this package to parse variable types, you could also use [getenv][getenv] (but it requires more work).


## Options

A second argument can be provided to `dotenvParseVariables` with an object of options.

The defaults are listed below:

* `assignToProcessEnv` (Boolean) - defaults to `true`, whether or not to assign the parsed values to `process.env`
* `overrideProcessEnv` (Boolean) - defaults to `false`, whether or not to override existing values in `process.env`
* `ignoreFunctions` (Boolean) - defaults to `true`, whether or not to ignore functions in the parsed values returned


## Contributors

| Name           | Website                   |
| -------------- | ------------------------- |
| **Nick Baugh** | <http://niftylettuce.com> |


## License

[MIT](LICENSE) © Nick Baugh


##

[lad]: https://lad.js.org

[fe]: https://forwardemail.net

[npm]: https://www.npmjs.com/

[yarn]: https://yarnpkg.com/

[dotenv]: https://github.com/motdotla/dotenv

[dotenv-expand]: https://github.com/motdotla/dotenv-expand

[dotenv-extended]: https://github.com/keithmorris/node-dotenv-extended

[getenv]: https://github.com/ctavan/node-getenv

[@ladjs/env]: https://github.com/ladjs/env

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