Neconfig - configuration module for Nestjs.
npm i neconfig
- Reading environment variables
- Reading from multiple .env files (using dotenv)
- Reading plain js objects
- Providing default values
- Providing different config per module
// String
const host: string | undefined = config.getString('host'); // optional
const host2: string = config.getString('host', 'localhost'); // default value
const host3: string = config.getStringOrThrow('host'); // required, throwing error if host didn't provided
const host4: string = config.getStringOrThrow('host', 'Host is required'); // specified error message
// Integer
const port: number | undefined = config.getInt('port');
const port2: number = config.getInt('port', 3000);
const port3: number = config.getIntOrThrow('port');
// Number
const price: number | undefined = config.getNumber('price');
const price2: number = config.getNumber('price', 1.99);
const price3: number = config.getNumberOrThrow('price');
// Boolean
const logsEnabled: boolean | undefined = config.getBoolean('logs_enabled');
const logsEnabled2: boolean = config.getBoolean('logs_enabled', true);
const logsEnabled3: boolean = config.getBooleanOrThrow('logs_enabled');
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
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
NeconfigModule.register({
readers: [
{ name: 'env', file: path.resolve(process.cwd(), '.env') }
]
})
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:
HOST=myhost.com
PORT=3000
$ export PORT=80
$ npm start
> Listening on http://myhost.com:80
NeconfigModule.register({
readers: [
{
name: 'hash',
data: { HOST: 'localhost', PORT: 3000 }
}
]
})
You can register NeconfigModule with several readers:
NeconfigModule.register({
readers: [
{ name: 'hash', data: { HOST: 'localhost', PORT: 3000 } }, // (1)
{ name: 'env', file: path.resolve(process.cwd(), '.env') } // (2)
{ name: 'env', file: path.resolve(process.cwd(), 'dev.env') } // (3)
]
})
APP_NAME = Neconfig
HOST = myhost.com
PORT = 80
HOST = 0.0.0.0
PORT = 4000
$ 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'}
@Module({
imports: [
NeconfigModule.register({
readers: [
{ name: 'env', file: path.resolve(process.cwd(), 'configs', 'app.env') }
]
})
]
})
export class AppModule {}
@Module({
imports: [
NeconfigModule.register({
readers: [
{ name: 'env', file: path.resolve(process.cwd(), 'configs', 'feat.env') }
]
})
]
})
export class FeatModule {}
AppModule
and FeatModule
have different instances of ConfigReader
You are welcome with this project for contributing, just make a PR