Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/47ng/check-env
Check that required environment variables are set for your app
https://github.com/47ng/check-env
environment-variables
Last synced: 2 months ago
JSON representation
Check that required environment variables are set for your app
- Host: GitHub
- URL: https://github.com/47ng/check-env
- Owner: 47ng
- License: mit
- Created: 2018-11-10T21:01:52.000Z (about 6 years ago)
- Default Branch: next
- Last Pushed: 2023-03-01T15:58:39.000Z (almost 2 years ago)
- Last Synced: 2024-09-26T07:38:48.277Z (4 months ago)
- Topics: environment-variables
- Language: TypeScript
- Homepage:
- Size: 1.61 MB
- Stars: 20
- Watchers: 3
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
@47ng/check-env
[![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)
Check that the critical environment variables are set for your app,
and that you did not leave dangerous development overrides in production.## 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.