
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
A type-safe utility that eyeballs your environment variables so you don't have to.
envball – eyeballs your env vars so you don't have to
A lightweight, type-safe utility for managing environment variables in Node.js applications.
It simplifies the process of defining, validating and parsing environment variables with automatic type inference and validation.
import envball from 'envball';
// Basic usage - all variables are strings by default
const env = envball(['PORT', 'NODE_ENV']);
env.PORT; // string (process.env.PORT)
env.NODE_ENV; // string (process.env.NODE_ENV)
// Add defaults for optional variables and type inference
const env = envball(['PORT', 'DEBUG', 'APP_NAME'], {
defaults: {
PORT: 3000, // inferred as number
DEBUG: false, // inferred as boolean
APP_NAME: 'app', // inferred as string
},
});
npm install envball
yarn add envball
pnpm add envball
bun add envball
// Automatic number conversion based on default type
process.env.PORT = '3000';
const env = envball(['PORT'], {
defaults: { PORT: 8080 }, // number type inferred from default
});
env.PORT; // 3000 (number)
// Without defaults, values remain as strings
const env = envball(['PORT']);
env.PORT; // '3000' (string)
// Supports decimals
process.env.RATIO = '3.14';
const env = envball(['RATIO'], {
defaults: { RATIO: 1.0 },
});
env.RATIO; // 3.14 (number)
// Boolean parsing with type inference
process.env.DEBUG = 'true'; // or '1' or 'yes'
const env = envball(['DEBUG'], {
defaults: { DEBUG: false },
});
env.DEBUG; // true (boolean)
// Supported values
const truthyValues = ['true', '1', 'yes', 'TRUE', 'YES'];
const falsyValues = ['false', '0', 'no', 'FALSE', 'NO'];
// String arrays (default)
process.env.HOSTS = 'localhost,127.0.0.1';
const env = envball(['HOSTS'], {
defaults: { HOSTS: [] as string[] },
});
env.HOSTS; // ['localhost', '127.0.0.1']
// Number arrays with type inference from default
process.env.PORTS = '3000,4000,5000';
const env = envball(['PORTS'], {
defaults: { PORTS: [8080] }, // Type inferred from default
});
env.PORTS; // [3000, 4000, 5000] (number[])
// Custom delimiter for all array parsing
const env = envball(['LIST'], {
defaults: { LIST: [] as string[] },
delimiter: ';',
});
// Explicit array types for empty arrays
const env = envball(['NUMS', 'FLAGS'], {
defaults: {
NUMS: [], // Empty array
FLAGS: [], // Empty array
},
arrayTypes: {
NUMS: 'number',
FLAGS: 'boolean',
},
});
// Type-safe object parsing with validation
type DBConfig = {
db: {
host: string;
port: number;
};
};
// Objects are validated against the default structure
process.env.CONFIG = '{"db":{"host":"localhost","port":5432}}';
const env = envball(['CONFIG'], {
defaults: {
CONFIG: { db: { host: '', port: 0 } } as DBConfig,
},
});
// Missing or invalid properties throw errors
process.env.CONFIG = '{"db":{"host":"localhost"}}';
// Error: Missing required property CONFIG.db.port
process.env.CONFIG = '{"db":{"host":"localhost","port":"5432"}}';
// Error: Invalid type for CONFIG.db.port: expected number, got string
const env = envball(['PORT', 'NODE_ENV'], {
defaults: {
PORT: 8080,
NODE_ENV: 'development',
},
validator: (key, value) => {
switch (key) {
case 'PORT': {
if (typeof value === 'number' && (value < 1024 || value > 65535)) {
throw new Error('Port must be between 1024 and 65535');
}
break;
}
case 'NODE_ENV': {
if (!['development', 'production', 'test'].includes(value as string)) {
throw new Error('Invalid NODE_ENV value');
}
break;
}
}
},
});
envball implements the Standard Schema interface, making it compatible with any tool that accepts Standard Schema validators.
import { envballSchema } from 'envball';
// Create a schema instance
const schema = new envballSchema(['PORT', 'NODE_ENV'], {
defaults: {
PORT: 3000,
NODE_ENV: 'development',
},
});
// Use with any Standard Schema compatible tool
const result = schema['~standard'].validate({
PORT: '8080',
NODE_ENV: 'production',
});
if ('issues' in result) {
console.error('Validation failed:', result.issues);
} else {
console.log('Validated value:', result.value);
}
// Missing required variables
envball(['REQUIRED_VAR']);
// Error: Missing environment variable: REQUIRED_VAR
// Invalid type conversion
process.env.PORT = 'not-a-number';
envball(['PORT'], { defaults: { PORT: 3000 } });
// Error: Failed to parse PORT as number: not-a-number
// Invalid object shape (missing property)
process.env.CONFIG = '{"db":{"host":"localhost"}}';
envball(['CONFIG'], {
defaults: { CONFIG: { db: { host: '', port: 0 } } },
});
// Error: Missing required property CONFIG.db.port
// Invalid object shape (wrong type)
process.env.CONFIG = '{"db":{"host":123,"port":5432}}';
// Error: Invalid type for CONFIG.db.host: expected string, got number
// Invalid JSON
process.env.CONFIG = 'invalid-json';
envball(['CONFIG'], { defaults: { CONFIG: {} } });
// Error: Failed to parse CONFIG as JSON: Unexpected token 'i', "invalid-json" is not valid JSON
"") and whitespace-only strings are treated as undefinedenvball(keys, options?)keys: Array of environment variable namesoptions: Optional configuration object
defaults: Object with default values that also define typesvalidator: Function to validate values (key, value) => voiddelimiter: String to split array values (defaults to ',')arrayTypes: Specify types for empty arrays { key: 'string' | 'number' | 'boolean' }A frozen object containing the environment variables with inferred types.
envballSchemaA class that implements the Standard Schema interface. Use this if you need to integrate with tools that accept Standard Schema validators.
Same as envball function.
~standard.validate(value: unknown): Validates an object against the schema~standard.types: Type information for TypeScript integrationWe welcome contributions! Please open an issue or submit a pull request on GitHub.
envball is licensed under the MIT Licence. See LICENCE for details.
FAQs
A type-safe utility that eyeballs your environment variables so you don't have to.
The npm package envball receives a total of 1 weekly downloads. As such, envball popularity was classified as not popular.
We found that envball demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.