cosmiconfig: One Config Loader for package.json, rc Files and TypeScript Modules
Find and load configuration from a package.json property, rc file, TypeScript module, and more!
At a glance
- What is it?
- cosmiconfig is a library for tooling authors who need to find configuration wherever users put it. It handles the search order, the parsing and the caching, but it also makes choices about scope and module loading that are worth understanding before you depend on it.
- Who is it for?
- Adopt cosmiconfig if you are writing a tool that must read configuration from wherever users already keep it, and you want the conventional search order handled for you rather than reimplemented. Do not adopt it if you need a single, explicit config path, or if your config format is something other than JSON, YAML or a loadable JS or TS module.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 2 days ago.
- What is it written in?
- Mainly TypeScript, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 23, 2026, and from our analysis. They are not legal advice.
Star history
- cosmiconfig/cosmiconfig4,172 ★ · +6
| Repository | 2026-09-23 | 2026-11-23 |
|---|---|---|
| cosmiconfig/cosmiconfig | 4.2k | 4.2k |
6 Stars in the last 61 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
What cosmiconfig solves for tooling authors
Every CLI tool that takes configuration eventually faces the same question: where do users expect to put the file? The JavaScript ecosystem answered that question inconsistently for years. Some tools read a property out of package.json. Others read a dotfile. Others want a .config.js. If you are writing the tool, you either pick one and disappoint everyone else, or you write the search logic yourself.
cosmiconfig is that search logic, packaged. It is aimed at tooling developers, and the README says so explicitly: if you are an end user of a tool that uses cosmiconfig, such as prettier or stylelint, you can skip the developer section entirely. The library is a dependency of tools, not something an application author installs to read their own settings.
The value proposition is the conventional search order. Given a module name, cosmiconfig knows the shapes people expect: a property in package.json, an extensionless rc file, an rc file with one of a long list of extensions, the same names inside a .config subdirectory, and a .config.js or .config.ts style file. Writing that list by hand is tedious and easy to get subtly wrong.
The search order and how a result is produced
You create an explorer by calling cosmiconfig with a module name. That name drives everything else: it generates the default search places and the package.json property to look for. The README's example uses a module called goldengrahams, which produces a search for a goldengrahams property in package.json, then .goldengrahamsrc, then .goldengrahamsrc.json and the rest of the extension list, then the same names inside .config, then goldengrahams.config.js and its TypeScript and ESM variants.
Search starts from process.cwd(), or from a directory you pass to search(). By default it checks those places in the current directory only. You can opt into walking up the directory tree with a search strategy, checking each place in each directory until something acceptable turns up or the home directory is reached. That upward walk is the behaviour most people picture when they hear "cosmiconfig", and it is not the default.
Both search() and load() resolve to a result object with three fields. config holds the parsed configuration, and is undefined if the file is empty. filepath is the path that was found. isEmpty is true when there was nothing to parse, and the README notes the property is absent when the file is not empty. Checking isEmpty matters, because a config file that exists but parses to nothing is a different situation from no config file at all, and search() resolves to null in the second case.
Installing cosmiconfig and running a first search
The package installs from npm. The README states it is tested in Node 14 and above, so check your runtime before adding it to a project pinned to an older Node line.
npm install cosmiconfigA minimal asynchronous explorer takes a module name and calls search(). The README gives this shape, where result.config is the parsed object, result.filepath is where it came from, and result.isEmpty flags an empty file.
const { cosmiconfig } = require('cosmiconfig');
const explorer = cosmiconfig('myapp');
explorer.search()
.then((result) => {
// result.config is the parsed configuration object.
// result.filepath is the path to the config file that was found.
// result.isEmpty is true if there was nothing to parse.
})
.catch((error) => {
// Do something constructive.
});If you already know where the file lives, load() skips the search and returns the same result shape. There is a synchronous twin, cosmiconfigSync, which exposes search() and load() without promises. The README shows the synchronous path returning values directly rather than promises.
const { cosmiconfigSync } = require('cosmiconfig');
const explorerSync = cosmiconfigSync('myapp');
const searchedFor = explorerSync.search();
const loaded = explorerSync.load(pathToConfig);One constraint from the README is easy to miss. Because the default search places include files named after your module, the module name must consist of characters allowed in filenames. The README warns specifically against copying scoped package names such as @my-org/my-package straight into moduleName.
Caching, and the clearCaches calls you will eventually need
The explorer initializes caches when you create it. Both the async and sync explorers expose clearLoadCache(), clearSearchCache() and clearCaches(), which tells you there are two separate caches to reason about: one for loaded and parsed files, one for search results.
That design is a good fit for a CLI that reads configuration once and exits. It is a poor fit for a long-running process that expects configuration changes to be picked up. If you are writing a language server, a dev server or a watch mode, you need to call the appropriate clear method when a file on disk changes, and you need to know which cache is stale. The README documents the methods but does not walk through a watch-mode recipe, so the responsibility for wiring invalidation sits with you.
There is also a cache option in cosmiconfigOptions, which the README lists but does not expand on. Treat the caching behaviour as something to verify against your own use case rather than assume.
Loading JS and TS modules is where the sharp edges are
cosmiconfig does not only parse data files. It can load .js, .ts, .mjs, .cjs, .mts and .cts configs. The README has a dedicated section on loading JS modules, which is a signal that this is not a trivial code path.
Executing a config file is a real decision with consequences. A JSON or YAML config is data. A JavaScript config is code, and loading it means running it in your process, with whatever that implies for the trust boundary between the tool and the repository it is reading. Tools that load configs from a cloned repository are executing code from that repository. cosmiconfig gives you the capability; deciding whether to enable module loaders is your call, and it is the kind of thing worth making explicit in your own documentation rather than inheriting silently.
The extension list is broad. Supporting .mts and .cts alongside .js and .cjs means the loader has to cope with the various module systems in circulation, and the README points readers at the dedicated section to learn how JS files are loaded rather than explaining it inline. If your tool supports only one module system, you may want to narrow searchPlaces rather than accept the full default list.
When cosmiconfig is the wrong tool
The clearest case against it is a project that wants exactly one configuration location. If your tool reads ./myapp.config.js and nothing else, cosmiconfig adds a dependency, a search order and a set of precedence rules you now have to explain to users. The README's own description of the default behaviour, searching five families of locations, is a feature only if you want that flexibility.
The second case is a config format outside the supported set. The loaders cover JSON, YAML and loadable JS and TS modules. If your configuration is TOML or an INI file, the default loaders will not help, and you would be writing a custom loader to get value out of the library.
The third case is performance-sensitive startup where you want to know exactly which filesystem calls happen. cosmiconfig checks a list of candidate paths in order, and with a search strategy enabled it repeats that list per directory up the tree. The README does not publish figures for this, and the caching exists precisely because repeated searches are not free. If your tool starts on every shell prompt, measure the search cost in your own environment before committing.
How it differs from rc and from lilconfig
The README includes a section titled "Differences from rc", which is the most direct comparison available: cosmiconfig shares ancestry with the older rc package but diverges in behaviour. Reading that section is the fastest way to understand what a migration would change, and the README treats it as a first-class topic rather than a footnote.
lilconfig is the other name that comes up in searches about cosmiconfig alternatives, and the difference in approach is one of scope. lilconfig is a smaller package with a narrower feature set. cosmiconfig carries env-paths and js-yaml as dependencies according to its package.json, and it supports the full extension matrix including TypeScript config files. If you need .ts and .mts config loading, or the .config subdirectory conventions, that breadth is the reason to pick cosmiconfig. If you need a compact loader for JSON and JS configs and nothing more, the smaller package is the honest choice, and you should read its documentation rather than assume feature parity.
The trade-off is dependency surface. A library that parses YAML and resolves platform paths brings more code into your tree than one that does not. That is not a criticism, it is the cost of the feature list, and it is worth weighing when the alternative is a few dozen lines of your own search code.
Licence, maintenance and upgrade cost
cosmiconfig is MIT licensed, which is permissive and imposes no copyleft obligation on the tools that depend on it. Its own dependencies, env-paths and js-yaml among them, carry their own licences, and you should check those separately if your organisation audits the full dependency tree. Nothing here is legal advice.
The repository is not archived, and the last push was on 2026-09-01. The most recent release is v10.0.1 from 2026-08-30, following v10.0.0 the same day, with v9.0.1 in March 2026. A major version bump is the upgrade signal to watch: v10 is a breaking change from v9, and the CHANGELOG.md at the repository root is where the project records what moved. The package.json shows a prepublishOnly script running the full test, lint and format check before a build, so releases go out through a gate rather than straight from a working tree.
Upgrade cost for consumers is mostly the major-version boundary. Because cosmiconfig is a transitive dependency for many tools, the practical question is usually when the tools you use move to v10, not when you do. If you depend on it directly, pin the major version and read CHANGELOG.md before crossing it.
Editorial conclusion
Adopt cosmiconfig if you are writing a tool that must read configuration from wherever users already keep it, and you want the conventional search order handled for you rather than reimplemented. Do not adopt it if you need a single, explicit config path, or if your config format is something other than JSON, YAML or a loadable JS or TS module. Before shipping, verify how your own module name behaves when it is a scoped package name, since the README warns against copying scoped names directly into moduleName, and confirm whether you want search() walking up the directory tree or load() against a known path.
Frequently asked questions
What is config in coding?
In cosmiconfig's terms, config is the parsed configuration object returned in the result of search() or load(). It is undefined if the file is empty, and the result's isEmpty property flags that case.
What does cosmiconfig look for by default?
The README lists a package.json property, a JSON or YAML extensionless rc file, an rc file with one of the supported extensions, the same names inside a .config subdirectory, and a .config.js or .config.ts style file.
Does cosmiconfig walk up the directory tree on its own?
No. By default it checks the search places in the current directory, and you have to opt into the upward walk with a search strategy, which stops at the home directory.
How do I install cosmiconfig?
The README gives a single install command, npm install cosmiconfig, and states the package is tested in Node 14 and above.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/cosmiconfig-cosmiconfig)Star history chart for your README
[](https://hysenlabs.com/projects/cosmiconfig-cosmiconfig)Was this useful?