@dynejs/config
@dynejs/config is a simple configuration library for Node.js. It provides and easy way to use configuration and environment variables inside your application.
Install
npm i @dynejs/config
Usage
Create a config.js file in your project root:
// config.js
module.exports = {
my: {
key: 'A'
}
}
Application code:
// app.js
const { Config } = require('@dynejs/config')
const config = createConfig()
config.get('my.key')
// Or with default value
config.get('my.key', 'Default value')
Config has a few optional parameter to configure initialization:
export type Cfg = {
root?: string
file?: string
}
-
root
is the path where the configuration file can be found. Default isprocess.cwd()
-
file
filename of the config file, default isconfig.js
env
in your config
Using Dyne config loads an optional .env file also. Environment file variables helps you
to manage different environment settings for development or testing.
.env
file will be loaded from the same directory, where the config.js
file can be found.
It won't be merged in the configuration, it's up to you how you use in your config file.
Create your .env
file in your project:
# .env file:
MY_KEY=Hello!
When you are planning to use env
in your configuration, than return a function
from your config file instead of object:
// config.js
module.exports = (env) => ({
my: {
key: env('MY_KEY'),
// With default value
other: env('OTHER', 'With default value')
}
})
In case when the environment variable
NODE_ENV
is set to test, it will load.env.test
file which helps you to use different settings.