env 1.0.0
Install from the command line:
Learn more about npm packages
$ npm install @kldzj/env@1.0.0
Install via package.json:
"@kldzj/env": "1.0.0"
About this version
A environment variable parser with type safety and validation out of the box.
Using yarn:
$ yarn add @kldzj/env
Using npm:
$ npm i -S @kldzj/env
import { parseEnv } from '@kldzj/env';
const env = parseEnv({
PORT: {
type: 'number',
default: 3000,
},
NODE_ENV: {
type: 'string',
default: 'development',
},
DB_URL: {
type: 'string',
required: true,
},
});
console.log(env.PORT); // type number -> 3000
console.log(env.NODE_ENV); // type string -> 'development'
// ...
- Type safety
- Custom parsers
- Optional (and default) values
- Passing a custom environment object
The parser is a function that takes a string and returns a value of any type. It can be used to parse environment variables that are not strings.
If a parser is present, the type of the variable is ignored and instead the return type of the parser is used.
import { parseEnv } from '@kldzj/env';
const env = parseEnv({
DATE: {
parser: (value) => new Date(value),
},
});
console.log(env.DATE); // type Date -> Date object
An object that contains the environment variables. If not provided, process.env
is used.
If true
, an error is thrown if a variable type is number and is parsed as NaN
. Defaults to false
.
Allows you to override the default parser. The default parser is used when a variable does not have a custom parser.