https://github.com/andybarron/env
Zero-dependency environment variable parsing in Node, Deno, and Bun
https://github.com/andybarron/env
bun configuration deno environment-variables nodejs zero-dependency
Last synced: about 2 months ago
JSON representation
Zero-dependency environment variable parsing in Node, Deno, and Bun
- Host: GitHub
- URL: https://github.com/andybarron/env
- Owner: andybarron
- License: mit-0
- Created: 2024-06-29T07:59:35.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-04T21:31:17.000Z (almost 2 years ago)
- Last Synced: 2024-07-05T16:27:37.659Z (almost 2 years ago)
- Topics: bun, configuration, deno, environment-variables, nodejs, zero-dependency
- Language: TypeScript
- Homepage: https://jsr.io/@andyb/env/doc
- Size: 55.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# [`@andyb/env`](https://github.com/andybarron/env)
_Zero-dependency environment variable parsing in Node, Deno, and Bun_
[Documentation](https://jsr.io/@andyb/env/doc) •
[Source](https://github.com/andybarron/env)
[](https://jsr.io/@andyb/env)
[](https://github.com/andybarron/env/actions?query=branch%3Amain)
[](https://app.codecov.io/github/andybarron/env)
[](https://jsr.io/@andyb/env/dependencies)
## Installation
| Tool | Install command |
| ---------------------------- | ----------------------------- |
| [Bun](https://bun.sh) | `bunx jsr add @andyb/env` |
| [Deno](https://deno.com) | `deno add @andyb/env` |
| [NPM](https://www.npmjs.com) | `npx jsr add @andyb/env` |
| [PNPM](https://pnpm.io) | `pnpm dlx jsr add @andyb/env` |
| [Yarn](https://yarnpkg.com) | `yarn dlx jsr add @andyb/env` |
## Quickstart
```ts
import * as env from "@andyb/env";
// parse() is compatible with process.env in Node and Deno.env in Deno.
// On failure, the thrown error will report every variable that failed to parse.
const config = parse(process.env, {
// Specify expected type and environment variable name.
favoriteNumber: env.integer().variable("FAVORITE_NUMBER"),
// Mark some environment variables as optional. They will only be parsed if present.
nickname: env.string().variable("NICKNAME").optional(),
// Default values can be provided. This makes the variable optional as well.
lovesDeno: env.boolean().variable("LOVES_DENO").default(true),
// If no variable is specified, the property name will be used.
TZ: env.string(),
});
// config will have its type inferred correctly:
type InferredType = {
favoriteNumber: number;
nickname: string | undefined;
lovesDeno: boolean;
TZ: string;
};
```
## Custom parsers
```ts
import * as env from "@andyb/env";
import ms from "ms";
// To parse custom types, provide a description and a parser function.
function duration() {
return env.custom(
'must be a duration e.g. "10 seconds"',
(value: string): number => ms(value),
);
}
const config = parse(process.env, {
// Custom parsers have the same chainable configuration methods as the
// built-in parsers.
timeoutMs: duration().variable("TIMEOUT").optional(),
});
// Type inference works for custom and async parsers as well:
type InferredType = {
timeoutMs: number;
healthCheck: number;
};
```
## Built-in parser types
```ts
import * as env from "@andyb/env";
const config = env.parse(process.env, {
BOOLEAN: env.boolean(), // only accepts "true" and "false"
INTEGER: env.integer(),
JSON: env.json(), // accepts any valid JSON value
NUMBER: env.number(),
PORT: env.port(), // accepts integers from 0 to 65545
STRING: env.string(),
});
type InferredType = {
BOOLEAN: boolean;
INTEGER: number;
JSON: env.JsonValue;
NUMBER: number;
PORT: number;
STRING: string;
};
```