Neconfig
Description
Neconfig - configuration module for Nestjs.
Example
Installation
npm i neconfig
Features
- Reading environment variables
- Reading from multiple .env files (using dotenv)
- Reading plain js objects
- Providing default values
- Providing different config per module
// String ; // optional ; // default value ; // required, throwing error if host didn't provided ; // specified error message // Integer ; ; ; // Number ; ; ; // Boolean ; ; ;
Types
getString
reads any thing that have toString() method, returns undefined otherwisegetInt
parses value usingparseInt(value, 10)
, returns undefined if NaNgetNumber
parses value usingparseFloat(value)
, returns undefined if NaNgetBoolean
parses value with checks
# Truthy values for boolean true, 'true', 'yes', 'y', '1', 1 # Falsy values for boolean false, 'false', 'no', 'n', '0', 0
Readers
There are few readers:
env
reader, that allows to read from environment variables and if required, from .env fileshash
reader, that allows to read from plain js objects
Env reader
NeconfigModule.register
Here, file
is optional, allowing to read config from files (using dotenv)
If the file option provided and file exists, Neconfig will parse that file content with dotenv and merge with process.env
Variables from file will be overridden by environment variables. Example:
.env file
HOST=myhost.comPORT=3000
Setting environment variable and run
$ export PORT=80$ npm start> Listening on http://myhost.com:80
Hash reader
NeconfigModule.register
Overriding config
You can register NeconfigModule with several readers:
NeconfigModule.register
.env file
APP_NAME = NeconfigHOST = myhost.comPORT = 80
dev.env file
HOST = 0.0.0.0PORT = 4000
Run
$ npm start> config: {HOST: '0.0.0.0', PORT: 4000, APP_NAME: 'Neconfig'}
- First reader
(1)
setsHOST
tolocalhost
andPORT
to3000
- Second reader
(2)
overridesHOST
tomyhost.com
,PORT
to80
and adds new keyAPP_NAME
- Third reader
(3)
overridesHOST
to0.0.0.0
andPORT
to4000
If you remove dev.env
file you will see:
$ npm start> config: {HOST: 'myhost.com', PORT: 80, APP_NAME: 'Neconfig'}
Provide different config per modules
app.module.ts
feat.module.ts
AppModule
and FeatModule
have different instances of ConfigReader
Contributing
You are welcome with this project for contributing, just make a PR