Load config from .{product}rc.{json,yml,js,ts} file.
It is a Node.js library for loading .textlintrc, .eslintrc, .stylelintrc etc...
Find and load a configuration object from:
- a
package.jsonproperty if it is needed - a JSON or YAML, JS, TypeScript "rc file"
.<product>rcor.<product>rc.jsonor.<product>rc.jsor.<product>rc.yml,.<product>rc.yaml.<product>rc.ts.<product>rc.cjs,.<product>rc.mjs,.<product>rc.cts,.<product>rc.mts(only when they are inextensionsoption)
- CommonJS and ES Modules config file
- TypeScript support
- Includes
.d.ts
- Includes
with MoOx/rc-loader
- Safe API
rccontains shabang in.jsfile
- Enhance Error message
with cosmiconfig
Sync loading- cosmiconfig@3+ support
syncoption
- cosmiconfig@3+ support
Built-in TypeScript support- comisconfig@6* is written by TypeScript
If you want to async support and customize loader, recommended to use cosmiconfig.
- Node.js 22.18.0 or later
- TypeScript config file uses Node.js's built-in type stripping
- ES Modules config file uses Node.js's
require(esm)
Install with npm:
npm install rc-config-loader
export interface rcConfigLoaderOption {
// does look for `package.json`
packageJSON?:
| boolean
| {
fieldName: string;
};
// if config file name is not same with packageName, set the name
configFileName?: string;
// where start to load
cwd?: string;
// extensions to search, in order of priority
// A file without extension like `.<product>rc` is tried as the formats of `.json`, `.yaml`/`.yml`, and `.js` in this list
// Default: [".json", ".yaml", ".yml", ".js", ".ts"]
// `.cjs`, `.mjs`, `.cts`, and `.mts` are searched only when they are given
extensions?: string[];
}
/**
* Find and load rcfile, return { config, filePath }
* If not found any rcfile, throw an Error.
* @param {string} pkgName
* @param {rcConfigLoaderOption} [opts]
* @returns {{ config: Object, filePath:string } | undefined}
*/
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:
rcFilefunction returnundefinedif the config file is not foundrcFilethrow 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);
// Not Found
if (!results) {
return {};
}
return results.config;
} catch (error) {
// Found it, but it is parsing error
return {} ; // default value
}
}
// load config
const config = loadRcFile("your-application");
console.log(config); // => rcfile contentIt will check these files and return config file if found it.
.your-applicationrc.json.your-applicationrc.yaml.your-applicationrc.yml.your-applicationrc.js.your-applicationrc.ts.your-applicationrc(tried as JSON, YAML, then JavaScript)
.your-applicationrc.cjs, .your-applicationrc.mjs, .your-applicationrc.cts, and .your-applicationrc.mts are not searched by default.
Add them to extensions option if your tool supports them. See Limit the extensions.
- [optional]
package.json- if
packageJSONoption is enabled
- if
loadConfigFile loads exactly one config file.
Use it when a user specifies the config file path by a CLI option like --config <path>.
export interface loadConfigFileOption {
// the base directory to resolve a relative file path. Default: `process.cwd()`
cwd?: string;
// extensions allowed to load. Default: all supported extensions
extensions?: string[];
}
export declare function loadConfigFile<R extends {}>(filePath: string, opts?: loadConfigFileOption): {
config: R;
filePath: string;
};filePathis resolved fromopts.cwd- It does not search parent directories, does not append any extension, and does not read
package.json - The loader is selected by the extension of
filePath.json→ JSON(JSON5).yamlor.yml→ YAML.js,.cjs, or.mjs→ JavaScript.ts,.cts, or.mts→ TypeScript- Other names like
.your-applicationrcare tried as JSON, YAML, then JavaScript
- It returns
{ config, filePath }, andfilePathis the absolute path to the config file
Note:
loadConfigFilethrows an Error that hascode: "ENOENT"if the config file is not foundloadConfigFilethrows an Error if the config file content is malformed (causing a parsing error)
import { loadConfigFile } from "rc-config-loader"
function loadConfigFromOption(configFilePath) {
try {
return loadConfigFile(configFilePath).config;
} catch (error) {
if (error.code === "ENOENT") {
throw new Error(`Config file not found: ${configFilePath}`);
}
// Found it, but it is parsing error
throw error;
}
}
// load ./config/your-application.json, not ../config/your-application.json
const config = loadConfigFromOption("config/your-application.json");JavaScript and TypeScript config files are loaded by Node.js's require().
- CommonJS:
module.exportsis the config - ES Modules:
export defaultis the config - TypeScript: Node.js strips the types, and then loads it as CommonJS or ES Modules
// .your-applicationrc.ts
type Config = {
rules: Record<string, boolean>;
};
const config: Config = {
rules: {
a: true
}
};
export default config;Limitations:
- ES Modules config file can not use top-level
await, becauserequire(esm)loads only synchronous modules - TypeScript config file can use only erasable syntax. For example,
enumis not supported, andtsconfig.jsonis not read - Node.js caches ES Modules. If an ES Modules config file is changed in the same process, the changed content is not loaded
Both rcFile and loadConfigFile accept extensions option to limit the extensions of config files.
Use it when your tool does not want to run JavaScript or TypeScript config files.
Default extensions |
|
|---|---|
rcFile |
[".json", ".yaml", ".yml", ".js", ".ts"] |
loadConfigFile |
[".json", ".yaml", ".yml", ".js", ".cjs", ".mjs", ".ts", ".cts", ".mts"] |
rcFile searches fewer extensions by default, because each extension adds an existence check in every directory up to the root.
loadConfigFile reads only the given file, so it allows every supported extension by default.
import { rcFile, loadConfigFile } from "rc-config-loader"
// search only .your-applicationrc.json, .your-applicationrc.yaml, and .your-applicationrc.yml
// The order of extensions is the priority of the search
rcFile("your-application", {
extensions: [".json", ".yaml", ".yml"]
});
// also search .your-applicationrc.cjs and .your-applicationrc.mjs
rcFile("your-application", {
extensions: [".json", ".yaml", ".yml", ".js", ".cjs", ".mjs", ".ts"]
});
// throws an Error that has `code: "ERR_RC_CONFIG_EXTENSION_NOT_ALLOWED"` for `config.js`
loadConfigFile("config.js", {
extensions: [".json", ".yaml", ".yml"]
});- A file without extension like
.your-applicationrcis tried only as JSON(.json), YAML(.yamlor.yml), and JavaScript(.js) that are inextensions, in order ofextensions rcFiledoes not try a file without extension ifextensionshas none of.json,.yaml,.yml, and.js.loadConfigFilethrows theERR_RC_CONFIG_EXTENSION_NOT_ALLOWEDerror in that case- An empty array or an unsupported extension throws an Error
defaultExtensionoption was removed. Useextensionsinstead. PassingdefaultExtensionthrows an Error
import { rcFile } from "rc-config-loader"
// load .eslintrc from current dir
console.log(rcFile("eslint"));
// load .eslintrc from specific path
console.log(rcFile("eslint", {
configFileName: `${__dirname}/test/fixtures/.eslintrc`
}));
/*
config: { extends: 'standard',
rules:
{ 'comma-dangle': [ 2, 'always-multiline' ],
'arrow-parens': [ 2, 'as-needed' ] } }
filePath: ${__dirname}/test/fixtures/.eslintrc
*/
// load property from package.json
console.log(rcFile("rc-config-loader", {
packageJSON: {
fieldName: "directories"
}
}));
/*
config: { test: 'test' }
filePath: /path/to/package.json
*/
// load .eslintrc from specific dir
console.log(rcFile("eslint", {
cwd: `${__dirname}/test/fixtures`
}));
// load specific filename from current dir
console.log(rcFile("travis", {configFileName: ".travis"}));
/*
config: { sudo: false, language: 'node_js', node_js: 'stable' }
filePath: /path/to/.travis
*/
// try to load as .json, .yml, js
console.log(rcFile("bar", {
configFileName: `${__dirname}/test/fixtures/.barrc`,
extensions: [".json", ".yml", ".js"]
}));
// try to load as foobar, but .foobarrc is not found
console.log(rcFile("foorbar")); // => undefined
// try to load as .json, but it is not json
// throw SyntaxError
try {
rcFile("unknown", {
// This is not json
configFileName: `${__dirname}/test/fixtures/.unknownrc`,
extensions: [".json"]
})
} catch (error) {
console.log(error);
/*
SyntaxError: Cannot read config file: /test/fixtures/.unknownrc
*/
}See Releases page.
Install devDependencies and Run npm test:
npm i -d && npm test
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
MIT © azu
Difference
- support multiple formats for a file without extension