# @poppinss/env

> Environment variable manager for Node.js

Latest version **1.0.6** (published 2019-08-29) · MIT license · 0 weekly downloads

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

## Install

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

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2019-08-29 |
| First published | 2019-05-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 11.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 48 |
| Author | virk,poppinss |
| Maintainers | virk |
| Keywords | env, adonisjs |

## Links

- npm: https://www.npmjs.com/package/@poppinss/env
- Repository: https://github.com/poppinss/env
- Homepage: https://github.com/poppinss/env#readme
- Issues: https://github.com/poppinss/env/issues
- npm.io page: https://npm.io/package/@poppinss/env

## Dependencies (2)

- [dotenv](https://npm.io/package/dotenv.md) ^8.1.0
- [@poppinss/utils](https://npm.io/package/@poppinss/utils.md) ^1.0.5

## 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

- 1.0.6 (latest) — 2019-08-29
- 1.0.5 — 2019-05-29
- 1.0.4 — 2019-05-26
- 1.0.3 — 2019-05-20
- 1.0.2 — 2019-05-19
- 1.0.1 — 2019-05-17
- 1.0.0 — 2019-05-14

## README

<div align="center">
  <img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1557762307/poppinss_iftxlt.jpg" width="600px">
</div>

# Env
[![circleci-image]][circleci-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url]

Environment variables parser and reader for Node.js. This module parses the raw string in dotfile format and set `process.env` variables from it. Also, you can make use of bash like syntax for [variable interpolation](#variable-interpolation).

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
## Table of contents

- [Usage](#usage)
- [Casting values](#casting-values)
- [Variable interpolation](#variable-interpolation)
  - [Following will fail](#following-will-fail)
  - [Do this instead](#do-this-instead)
  - [Escape characters](#escape-characters)
- [API](#api)
- [Maintainers](#maintainers)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Usage
Install the package from npm as follows:

```sh
npm i @poppinss/env

# yarn
yarn add @poppinss/env
```

and then use it as follows

```ts
import { env } from '@poppinss/env'

env.process(`
  PORT=3333
  HOST=127.0.0.1
`)
```

and then read the values as follows

```ts
env.get('PORT')

// or
process.env.PORT
```

The `Env.process` method **will not overwrite** the existing environment variables (that is how it should be). However, if you want to overwrite existing values, maybe inside testing environment, then you can call the `process` method as follows:

```ts
env.process('PORT=3333', true)
```

## Casting values
The values saved as environment variables are always string. However, this module does the values casting for you, when you read them using `Env.get`.

```ts
env.process(`
  CACHE_VIEWS=false
`)

process.env.CACHE_VIEWS // string
env.get('CACHE_VIEWS') // boolean
```

Following values are casted

| .env Value | Casted value |
|------------|--------------|
| `'null'` | `null` |
| `'true'` | `true` |
| `'1'` | `true` |
| `'false'` | `false` |
| `'0'` | `false` |

## Variable interpolation
You can also reference environment variables by using bash like interpolation syntax.

```ts
env.process(`
  HOST=localhost
  PORT=3333
  URL=$HOST:$PORT
`)
```

All `letter`, `numbers` and `_` after the dollar sign will be considered as a variable reference. Any other characters apart from the above mentioned aren't allowed. However, you can wrap your variable substitution inside curly braces `{}` when using characters other than the whitelisted one's.

### Following will fail
The following reference to `$REDIS-USER` will fail, since the parser will stop at `-` and consider `USER` as a static string.

```ts
env.process(`
  REDIS-USER=foo
  REDIS-URL=localhost@$REDIS-USER
`)
```

### Do this instead
The usage of curly braces `{}` tells the parser to parse until the closing brace.

```ts
env.process(`
  REDIS-USER=foo
  REDIS-URL=localhost@${REDIS-USER}
`)
```

### Escape characters
Quite often, you will have strings where you want the `$` dollar character to be considered as a literal value. In that case you can escape the character as follows.

```ts
env.process(`
  PASSWORD=pa\\$\\$word
`)

process.env.PASSWORD // pa$$word
```

> **DO NOTE**: The usage of double escape `\\` is required when you are typing the string directly in Javascript, since the first escape is swallowed by JS. However, if you are writing the enviornment variables inside the `.env` file, then a single escape `\` is required.

## API
Following are the autogenerated files via Typedoc

* [API](docs/README.md)

## Maintainers
[Harminder virk](https://github.com/thetutlage)

[circleci-image]: https://img.shields.io/circleci/project/github/poppinss/env/master.svg?style=for-the-badge&logo=circleci
[circleci-url]: https://circleci.com/gh/poppinss/env "circleci"

[npm-image]: https://img.shields.io/npm/v/@poppinss/env.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/@poppinss/env "npm"

[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript

[license-url]: LICENSE.md
[license-image]: https://img.shields.io/aur/license/pac.svg?style=for-the-badge

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