How to use the cosmiconfig function from cosmiconfig

Find comprehensive JavaScript cosmiconfig.cosmiconfig code examples handpicked from public code repositorys.

cosmiconfig.cosmiconfig is a utility function that loads configuration files and options from a variety of sources, including JSON, YAML, and JavaScript.

107
108
109
110
111
112
113
114
115

### cosmiconfig()

```js
const { cosmiconfig } = require('cosmiconfig');
const explorer = cosmiconfig(moduleName[, cosmiconfigOptions])
```

Creates a cosmiconfig instance ("explorer") configured according to the arguments, and initializes its caches.
fork icon160
star icon1
watch icon1

+ 7 other calls in file

14
15
16
17
18
19
20
21
22
const cwd = options.cwd || process.cwd();

return {
	_options: { ...options, cwd },

	_extendExplorer: cosmiconfig('', {
		transform: augmentConfig.augmentConfigExtended(cwd),
		stopDir: STOP_DIR,
	}),
fork icon967
star icon0
watch icon91

+ 13 other calls in file

How does cosmiconfig.cosmiconfig work?

cosmiconfig.cosmiconfig works by searching for a configuration file in various locations and formats, loading it into memory, and returning an object containing the parsed configuration options. The function takes a module name and an options object as arguments, which can include the name of the configuration file, the search path, the extension to search for, and more. cosmiconfig.cosmiconfig then searches for the configuration file in the specified path and loads it using one of several supported parsers, depending on the file format. The resulting configuration object is then returned to the calling code. cosmiconfig.cosmiconfig is useful for loading configuration options for applications or libraries that support multiple configuration file formats, and can be used with a variety of file types, including JSON, YAML, JavaScript, and more. Note that cosmiconfig.cosmiconfig is part of the cosmiconfig library, which is a configuration file loader utility for Node.js applications.

101
102
103
104
105
106
107
108
109
110

- **config:** The parsed configuration object. `undefined` if the file is empty.
- **filepath:** The path to the configuration file that was found.
- **isEmpty:** `true` if the configuration file is empty. This property will not be present if the configuration file is not empty.

## cosmiconfig()

```js
const explorer = cosmiconfig(moduleName[, cosmiconfigOptions])
```
fork icon160
star icon0
watch icon1

+ 7 other calls in file

339
340
341
342
343
344
345
346
347
348
  ext,
  dir,
  base
} = path__default["default"].parse(id);
const searchPath = config.path ? path__default["default"].resolve(config.path) : dir;
const found = await cosmiconfig.cosmiconfig("postcss").search(searchPath);
if (!found || found.isEmpty) return {
  plugins: [],
  options: {}
};
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const cosmiconfig = require("cosmiconfig");

async function loadConfig() {
  const explorer = cosmiconfig("myApp");
  const result = await explorer.search();

  if (!result) {
    throw new Error("Configuration not found");
  }

  console.log(`Loaded configuration from ${result.filepath}`);
  console.log(result.config);
}

loadConfig();

In this example, we use cosmiconfig.cosmiconfig to load configuration options for an application named myApp. We create a new cosmiconfig explorer object with the name of the application, and call the search method to search for a configuration file. If a configuration file is found, the search method returns an object with the parsed configuration options, which are logged to the console. If no configuration file is found, an error is thrown. Note that cosmiconfig.cosmiconfig supports a variety of configuration file formats, including JSON, YAML, and JavaScript, and can be customized with options such as the file extension to search for, the search path, and more.

148
149
150
151
152
153
154
155
156
157

Searches for a configuration file. Returns a Promise that resolves with a [result](https://github.com/cosmiconfig/cosmiconfig#result) or with `null`, if no configuration file is found.

You can do the same thing synchronously with [`explorerSync.search()`](https://github.com/cosmiconfig/cosmiconfig#explorersyncsearch).

Let's say your module name is `goldengrahams` so you initialized with `const explorer = cosmiconfig('goldengrahams');`. Here's how your default [`search()`](https://github.com/cosmiconfig/cosmiconfig#explorersearch) will work:

-   Starting from `process.cwd()` (or some other directory defined by the `searchFrom` argument to [`search()`](https://github.com/cosmiconfig/cosmiconfig#explorersearch)), look for configuration objects in the following places:
    1.  A `goldengrahams` property in a `package.json` file.
    2.  A `.goldengrahamsrc` file with JSON or YAML syntax.
fork icon0
star icon0
watch icon1

+ 7 other calls in file