# @47ng/check-env

> Check that the critical environment variables are set

Latest version **3.0.0** (published 2022-10-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install @47ng/check-env
pnpm add @47ng/check-env
yarn add @47ng/check-env
bun add @47ng/check-env
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2022-10-25 |
| First published | 2018-11-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 23 |
| Author | François Best |
| Maintainers | franky47 |
| Keywords | environment, environment-variables |

## Links

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

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

- 3.0.0 (latest) — 2022-10-25
- 3.0.0-beta.1 (beta) — 2022-10-25
- 2.1.1 — 2022-10-24
- 2.1.0 — 2022-02-10
- 2.0.2 — 2020-12-22
- 2.0.1 — 2020-04-23
- 2.0.0 — 2020-01-08
- 1.5.0 — 2020-01-08
- 1.4.0 — 2019-11-28
- 1.3.1 — 2019-06-13
- 1.3.0 — 2019-06-06
- 1.2.1 — 2018-11-10
- 1.2.0 — 2018-11-10
- 1.1.0 — 2018-11-10
- 1.0.0 — 2018-11-10

## README

<h1 align="center"><code>@47ng/check-env</code></h1>

<div align="center">

[![NPM](https://img.shields.io/npm/v/@47ng/check-env?color=red)](https://www.npmjs.com/package/@47ng/check-env)
[![MIT License](https://img.shields.io/github/license/47ng/check-env.svg?color=blue)](https://github.com/47ng/check-env/blob/next/LICENSE)
[![CI/CD](https://github.com/47ng/check-env/workflows/CI%2FCD/badge.svg?branch=next)](https://github.com/47ng/check-env/actions)
[![Coverage Status](https://coveralls.io/repos/github/47ng/check-env/badge.svg?branch=next)](https://coveralls.io/github/47ng/check-env?branch=next)

</div>

<p align="center">
  Check that the critical environment variables are set for your app,
  and that you did not leave dangerous development overrides in production.
</p>

## Installation

```
yarn add @47ng/check-env
```

## Usage

```js
import { checkEnv } from '@47ng/check-env'

checkEnv({
  // Will log an error and throw if any of these are missing:
  required: [
    'SOME_API_SECRET',
    'PRIVATE_TOKEN',
    'SOME_OTHER_IMPORTANT_THING'
    // ...
  ],

  // Will log an error and throw if any of these are set in production:
  unsafe: [
    'LOCAL_OVERRIDE_DISABLE_HTTPS',
    'INSECURE_COOKIES'
    // ...
  ]
})
```

If some required environment variable are not set, it will tell you and throw
an error at the end:
!["CLI output"](output.png)

## Error handling

You can choose to skip throwing an error with the `noThrow` option:

```js
checkEnv({
  noThrow: true,
  ...
})
```

## Conditional Checks

If you want to require some variables only in production, you can add a condition
before the variable name, any falsy value will be ignored:

```ts
const __PROD__ = process.env.NODE_ENV === 'production'

checkEnv({
  required: [
    'ALWAYS_REQUIRED',
    __PROD__ && 'ONLY_REQUIRED_IN_PRODUCTION',
    !__PROD__ && 'YOU_GET_THE_IDEA'
  ]
})
```

## Logging

By default, `check-env` uses `console.err` with emoji.

You can override the default logging methods with `logMissing` and `logUnsafe`.

Example using [Pino](https://github.com/pinojs/pino):

```js
const logger = require('pino')()

checkEnv({
  logMissing: name => logger.error(`Missing required environment variable ${name}`),
  logUnsafe: name => logger.warn(`Unsafe environment variable ${name} set in production`),
  ...
})
```

## License

[MIT](https://github.com/47ng/check-env/blob/master/LICENSE) - Made with ❤️ by [François Best](https://francoisbest.com)

Using this package at work ? [Sponsor me](https://github.com/sponsors/franky47) to help with support and maintenance.

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