What is rc-config-loader?
The rc-config-loader package is a utility for loading configuration files in various formats (like JSON, YAML, or JavaScript) from a project's root directory. It is particularly useful for applications that need to support user-defined configurations.
What are rc-config-loader's main functionalities?
Load Configuration
This feature allows you to load configuration files for a given application name. The loader will search for configuration files in the project's root directory and return the configuration object.
const rcConfigLoader = require('rc-config-loader');
const config = rcConfigLoader('myapp');
console.log(config);
Support for Multiple File Formats
The rc-config-loader supports multiple file formats such as JSON, YAML, and JavaScript. It will automatically detect and parse the configuration file based on its extension.
const rcConfigLoader = require('rc-config-loader');
const config = rcConfigLoader('myapp');
console.log(config);
Custom Configuration Path
You can specify a custom configuration file name or path if the default naming convention does not suit your needs.
const rcConfigLoader = require('rc-config-loader');
const config = rcConfigLoader('myapp', { configFileName: 'custom-config' });
console.log(config);
Other packages similar to rc-config-loader
cosmiconfig
Cosmiconfig is a versatile configuration loader that searches for configuration files in various formats (JSON, YAML, JS, etc.) and locations. It offers more flexibility and customization options compared to rc-config-loader.
config
The config package provides a more structured approach to configuration management, supporting environment-specific configurations and hierarchical settings. It is more feature-rich but also more complex than rc-config-loader.
dotenv
Dotenv is a simpler package focused on loading environment variables from a .env file into process.env. While it doesn't support multiple file formats or complex configurations, it is very useful for managing environment-specific settings.
rc-config-loader
Load config from .{product}rc.{json,yml,js}
file.
It is a Node.js library for loading .textlintrc
, .eslintrc
, .stylelintrc
etc...
Features
Find and load a configuration object from:
- a
package.json
property if it is needed - a JSON or YAML, JS "rc file"
.<product>rc
or .<product>rc.json
or .<product>rc.js
or.<product>rc.yml
, .<product>rc.yaml
- TypeScript support
Difference
- Safe API
rc
contains shabang in .js
file
- Enhance Error message
Install
Install with npm:
npm install rc-config-loader
Usage
API
export interface rcConfigLoaderOption {
packageJSON?: boolean | {
fieldName: string;
};
configFileName?: string;
defaultExtension?: string | string[];
cwd?: string;
}
export default function rcConfigLoader(packageName: string, options?: rcConfigLoaderOption): Object;
rcConfigLoader
return { config, filePath }
object.
config
: it is config objectfilePath
: absolute path to config file
If not found config file, return undefined
.
Example
"use strict";
const rcfile = require("rc-config-loader");
console.log(rcfile("eslint"));
console.log(rcfile("eslint", {
configFileName: `${__dirname}/test/fixtures/.eslintrc`
}));
console.log(rcfile("rc-config-loader", {
packageJSON: {
fieldName: "directories"
}
}));
console.log(rcfile("eslint", {
cwd: `${__dirname}/test/fixtures`
}));
console.log(rcfile("travis", {configFileName: ".travis"}));
console.log(rcfile("bar", {
configFileName: `${__dirname}/test/fixtures/.barrc`,
defaultExtension: [".json", ".yml", ".js"]
}));
try {
rcfile("unknown", {
configFileName: `${__dirname}/test/fixtures/.unknownrc`,
defaultExtension: ".json"
})
} catch (error) {
console.log(error);
}
Users
Changelog
See Releases page.
Running tests
Install devDependencies and Run npm test
:
npm i -d && npm test
Contributing
Pull requests and stars are always welcome.
For bugs and feature requests, please create an issue.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
Author
License
MIT © azu
Acknowledgement
Difference
- support multiple
defaultExtension