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: 3 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 (over 6 years ago)
- Default Branch: next
- Last Pushed: 2023-03-01T15:58:39.000Z (over 2 years ago)
- Last Synced: 2025-03-24T21:51:10.584Z (4 months ago)
- Topics: environment-variables
- Language: TypeScript
- Homepage:
- Size: 1.61 MB
- Stars: 21
- Watchers: 2
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
@47ng/check-env
[](https://www.npmjs.com/package/@47ng/check-env)
[](https://github.com/47ng/check-env/blob/next/LICENSE)
[](https://github.com/47ng/check-env/actions)
[](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:
## 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.