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
Sync loading
Built-in TypeScript support
If you want to async support and customize loader, recommended to use cosmiconfig.
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 declare function rcFile<R extends {}>(pkgName: string, opts?: rcConfigLoaderOption): {
config: R;
filePath: string;
} | undefined;
rcFile
return { config, filePath }
object.
config
: it is config objectfilePath
: absolute path to config file
Note:
rcFile
function return undefined
if the config file is not foundrcFile
throw an Error if the config file content is malformed (causing a parsing error)
Recommenced usage:
import { rcFile } from "rc-config-loader"
function loadRcFile(rcFileName){
try {
const results = rcFile(rcFileName);
if (!results) {
return {};
}
return results.config;
} catch (error) {
return {} ;
}
}
const config = loadRcFile("your-application");
console.log(config);
It will check these files and return config file if found it.
.your-applicationrc.json
.your-applicationrc.yml
.your-applicationrc.yaml
.your-applicationrc.js
- [optional]
package.json
- if
packageJSON
option is enabled
Example
import { rcFile } from "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"]
}));
console.log(rcFile("foorbar"));
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