0.1.6 • Published 1 year ago

auto-reload-config v0.1.6

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Auto-reload-config is a config loader that automatically reloads the config file(.js,.cjs,.json) without restarting the application when it is modified. It is designed to load different configuration files based on the value of an environment variable, and it can override fields in the configuration file with the same name suffixed with "cover" file.

Installation

npm install auto-reload-config

Usage

Create config/config.js file with the following content in workdir:

module.exports = {
  key: "your key",
};
import autoReloadConfig from 'auto-reload-config');
autoReloadConfig.get('key', 'default key');
//your key
autoReloadConfig.get('key2', 'default key');
//default key

use custom path

import autoReloadConfig from 'auto-reload-config');
autoReloadConfig.setup({ configDir: () => path.join('..', "customConfig") });
autoReloadConfig.get('key', 'default key');
//your key

use env to load different config

It will load config.{NODE_ENV}.js. Create config/config.dev.js file with the following content in workdir:

module.exports = {
  key: "your key",
};
import autoReloadConfig from 'auto-reload-config');
autoReloadConfig.get('key', 'default key');
//your key
NODE_ENV=dev node app.js

##Options

envVar

The name of the environment variable that determines which configuration file to load. Default is NODE_ENV.

envMap

The mapping of environment variables to configuration files. Default doest not map, the suffiex of configuration file uses the value of NODE_ENV.

disableReload

Disable auto reload config file. Default is false.

configDir

The path to the directory where the configuration files are located. Default is {workdir}/config/.

fileType

The file type of the configuration file. Default is js.