cosmiconfig
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
is-directory | 0.3.1 | 2.42 kB | MIT | prod | |
js-yaml | 3.14.1 | 75.07 kB | MIT | prod | |
parse-json | 4.0.0 | 1.98 kB | MIT | prod |
Cosmiconfig is a powerful tool in JavaScript ecosystem designed to search for and load configuration for your application. It brings smart default configurations while offering flexibility to customize as per your needs. It loads configuration from multiple sources such as package.json properties, JSON or YAML files, TypeScript modules, and more. Its search functionality starts looking at a specified point and moves up the directory tree, searching for acceptable configurations. If no configuration options are discovered, the program reaches up to the home directory.
To use cosmiconfig, you first need to install it with npm:
npm install cosmiconfig
Then, you can import it into your JavaScript or TypeScript file and start using its functionalities. Given below is an example of how to use cosmiconfig for loading and searching configuration:
const { cosmiconfig, cosmiconfigSync } = require('cosmiconfig');
const explorer = cosmiconfig('yourModuleName');
// Search for a configuration:
explorer.search()
.then(result => {
// The result object contains these properties:
// result.config is the parsed configuration.
// result.filepath is the configuration file path.
// result.isEmpty indicates if the file was empty.
})
.catch((error) => {
// Handle errors
});
// Load a configuration:
explorer.load('pathToConfig').then(result => { /*...*/ });
// For synchronous operations, you can use the cosmiconfigSync() function.
In code, 'yourModuleName' and 'pathToConfig' should be replaced with the name of your module and the desired path you want to load configuration from respectively.
Cosmiconfig provides detailed documentation and comprehensive APIs for all its features that can be accessed through the Cosmiconfig Git Repository. The main sections of interest within the docs are: Installation, APIs for tooling developers, Explaining result of searching and loading, Asynchronous and Synchronous APIs, and Customizing cosmiconfig options.