A flexible configuration loader for Node.js projects, supporting both JavaScript and TypeScript configuration files. This package allows you to easily manage configuration settings in your application with a simple API.
- Table of Contents
- Installation
- Usage
- API
- ConfigLoader Interface
- Example
- License
- Contributing
- Acknowledgements
To install the package, use npm:
npm install config-js
Or using yarn:
yarn add config-js
First, you need to initialize the configuration loader with the directory where your configuration files are located. You can do this using the initializeConfigLoader
method.
import { config } from '@teikun-86/config-js';
import * as path from 'path';
// Initialize the configuration loader with the directory path
config.initializeConfigLoader(path.resolve(__dirname, 'config'));
Once the loader is initialized, you can use the config
function to retrieve configuration values. The config
function takes a key (in dot notation) and an optional default value.
const appName = config<string>('app.name');
const appEnv = config<string>('app.env');
const dbHost = config<string>('database.host');
const dbPort = config<number>('database.port');
console.log(`App Name: ${appName}`);
console.log(`App Environment: ${appEnv}`);
console.log(`Database Host: ${dbHost}`);
console.log(`Database Port: ${dbPort}`);
If you need to change the configuration directory at runtime, you can use the setConfigDir
method provided by the loader.
const loader = config.getConfigLoader();
loader.setConfigDir(path.resolve(__dirname, 'new-config'));
// Now, config values will be loaded from the new directory
const newAppName = config<string>('app.name');
console.log(`New App Name: ${newAppName}`);
Your configuration files can be in either JavaScript or TypeScript or JSON format. The loader will handle both formats seamlessly.
JavaScript Configuration File (config/app.js
):
module.exports = {
name: 'MyApp',
env: 'development',
};
TypeScript Configuration File (config/database.ts
):
export default {
host: 'localhost',
port: 3306,
};
JSON Configuration File (config/app.json
):
{
"name": "MyApp",
"env": "development"
}
Retrieve the configuration value for the specified key. If the key does not exist, the optional defaultValue
will be returned.
Initialize the configuration loader with the directory where your configuration files are located.
Retrieve the singleton instance of the configuration loader. The ConfigLoader
interface provides methods to get configuration values and set the configuration directory.
Set the configuration directory. This will clear any cached configuration values.
Retrieve the configuration value for the specified key. If the key does not exist, the optional defaultValue
will be returned.
Here is a complete example to illustrate how to use the config-js
package:
import { config } from '@teikun-86/config-js';
import * as path from 'path';
// Initialize the configuration loader
config.initializeConfigLoader(path.resolve(__dirname, 'config'));
// Retrieve configuration values
const appName = config<string>('app.name');
const appEnv = config<string>('app.env');
const dbHost = config<string>('database.host');
const dbPort = config<number>('database.port');
console.log(`App Name: ${appName}`);
console.log(`App Environment: ${appEnv}`);
console.log(`Database Host: ${dbHost}`);
console.log(`Database Port: ${dbPort}`);
// Change the configuration directory
const loader = config.getConfigLoader();
loader.setConfigDir(path.resolve(__dirname, 'new-config'));
// Retrieve configuration values from the new directory
const newAppName = config<string>('app.name');
console.log(`New App Name: ${newAppName}`);
This project is licensed under the MIT License. See the LICENSE file for more details.
Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or suggestions.
This package was inspired by the need for a simple and flexible configuration loader for Node.js applications, with support for both JavaScript and TypeScript configuration files.