https://github.com/superchupudev/neon-env
A typed environment variable parser with support for choices, custom parsers, and more.
https://github.com/superchupudev/neon-env
Last synced: over 1 year ago
JSON representation
A typed environment variable parser with support for choices, custom parsers, and more.
- Host: GitHub
- URL: https://github.com/superchupudev/neon-env
- Owner: SuperchupuDev
- License: mit
- Created: 2022-10-23T16:53:54.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-11-04T04:36:57.000Z (over 1 year ago)
- Last Synced: 2025-03-27T14:07:06.642Z (over 1 year ago)
- Language: TypeScript
- Homepage: https://npmjs.com/package/neon-env
- Size: 248 KB
- Stars: 60
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# neon-env
> Basically [typed-env](https://www.npmjs.com/package/typed-env), but with support for choices and maintained.
A typed environment variable parser with support for choices, custom parsers, and more.
## Installation
> Node.js ^14.18.0, v16 or newer is required. [See issue #1](https://github.com/SuperchupuDev/neon-env/issues/1#issuecomment-1296366710).
```sh-session
npm i neon-env
```
## Usage
```ts
import { createEnv } from 'neon-env';
const env = createEnv({
PORT: { type: 'number', default: 80 }
});
env.PORT; // number
```
## Features
- Strongly typed
- Supports [custom parsers](#parser)
- Supports optional environment variables
- Supports limiting the possible values (see [Choices](#choices))
- Supports passing custom environments (see [Options](#options))
### Choices
```ts
import { createEnv } from 'neon-env';
const env = createEnv({
NODE_ENV: {
type: 'string',
choices: ['development', 'production']
}
});
env.NODE_ENV; // 'development' | 'production'
```
As of 0.2.0, you no longer need to add `as const` to the choices array to get the best type safety.
### Parser
You can pass a `parser` function to return your own custom type
```ts
import { createEnv } from 'neon-env';
const env = createEnv({
HOMEPAGE: { parser: url => new URL(url) }
});
env.HOMEPAGE; // URL
```
### Options
If you want to use a custom env, pass `env` in the `options` parameter, otherwise it will load from `process.env`
```ts
interface Options {
env?: Record | NodeJS.ProcessEnv;
}
```