env-armor
v0.2.0
Published
Lightweight environment variable validation for Node.js.
Downloads
744
Readme
env-armor
Lightweight environment variable validation for Node.js.
Installation
npm install env-armorFeatures
- Zero dependencies
- Number validation
- String validation
- Boolean validation
- Enum validation
- Optional environment variables
- Default values
- ESM and CommonJS support
Usage
import { validateEnv } from "env-armor";
const env = validateEnv({
PORT: {
type: "number",
default : 3000
},
DEBUG: {
type: "boolean",
},
DATABASE_URL: {
type: "string",
optional : true
},
NODE_ENV: {
enum: ["development", "production", "test"],
},
});
console.log(env.PORT);Supported Types
Number
PORT: {
type: "number";
}String
DATABASE_URL: {
type: "string";
}Boolean
DEBUG: {
type: "boolean";
}Accepted values:
- true
- false
Enum
NODE_ENV: {
enum: ["development", "production", "test"];
}Error Examples
Missing variable
Missing required environment variable: PORTInvalid number
Invalid environment variable: PORT
Expected type: number
Received: helloOptional Variables
const env = validateEnv({
DEBUG: {
type: "boolean",
optional: true
}
});If DEBUG is missing, validation will be skipped.
If DEBUG is present, it will still be validated.
Default Values
const env = validateEnv({
PORT: {
type: "number",
default: 3000
}
});If PORT is missing, the default value will be used.
Environment variables always take precedence over defaults.
License
MIT
