truthenv
v1.0.0
Published
Tiny zero-dependency helper for safely reading boolean environment flags.
Maintainers
Readme
truthenv
Tiny zero-dependency helper for safely reading boolean environment flags.
import envFlag from 'truthenv';
if (envFlag('FEATURE_AI_SEARCH')) {
console.log('enabled');
}Why? Because this common JavaScript pattern is buggy:
if (process.env.FEATURE_AI_SEARCH) {
// This also runs when FEATURE_AI_SEARCH="false"
}truthenv understands the values developers actually write in .env, CI and hosting dashboards:
| true | false |
| --- | --- |
| true | false |
| 1 | 0 |
| yes | no |
| on | off |
| enabled | disabled |
| active | inactive |
| ok | nope |
Install
npm install truthenvUsage
import { envFlag, toFlag } from 'truthenv';
const debug = envFlag('DEBUG');
const billingEnabled = envFlag('BILLING_ENABLED', { default: true });
const strictFeature = envFlag('FEATURE_X', { strict: true });
console.log(toFlag('off')); // false
console.log(toFlag('YES')); // trueCommonJS also works:
const envFlag = require('truthenv');
if (envFlag('DEBUG')) {
console.log('debug mode');
}API
envFlag(name, options?)
Reads a value from process.env and returns a real boolean.
Options:
default: value returned when the env var is missing or unknown. Default:false.strict: whentrue, unknown values throw aTypeError.env: custom env object for tests, edge runtimes or non-Node environments.
toFlag(value, fallback?)
Converts a raw value to a boolean without reading process.env.
License
MIT
