# load-config-file

> Load the config file as a plain object. The config file format can be registered.

Latest version **2.1.0** (published 2025-08-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install load-config-file
pnpm add load-config-file
yarn add load-config-file
bun add load-config-file
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2025-08-02 |
| First published | 2015-07-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 53 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | riceball |
| Keywords | load, config, configuration, file, yaml, json, cson, ini, object, fs, abstract |

## Links

- npm: https://www.npmjs.com/package/load-config-file
- Repository: https://github.com/snowyu/load-config-file.js
- Issues: https://github.com/snowyu/load-config-file.js/issues
- npm.io page: https://npm.io/package/load-config-file

## Dependencies (3)

- [path.js](https://npm.io/package/path.js.md) ^2.0.0
- [util-ex](https://npm.io/package/util-ex.md) ^2.0.0
- [promise-sequence](https://npm.io/package/promise-sequence.md) ^2.0.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.1.0 (latest) — 2025-08-02
- 2.0.0-alpha.3 (alpha) — 2024-05-31
- 2.0.0 — 2024-08-29
- 2.0.0-alpha.2 — 2024-04-23
- 2.0.0-alpha.1 — 2024-04-22
- 2.0.0-alpha.0 — 2024-04-22
- 1.0.8 — 2020-09-30
- 1.0.7 — 2020-07-07
- 1.0.6 — 2020-07-04
- 1.0.5 — 2020-07-04
- 1.0.4 — 2020-07-04
- 1.0.3 — 2020-07-03
- 1.0.2 — 2020-07-03
- 1.0.1 — 2020-07-03
- 1.0.0 — 2020-07-03
- … 18 more at https://npm.io/package/load-config-file/versions

## README

## load-config-file [![npm](https://img.shields.io/npm/v/load-config-file.svg)](https://npmjs.org/package/load-config-file)

[![Build Status](https://img.shields.io/travis/snowyu/load-config-file.js/master.svg)](http://travis-ci.org/snowyu/load-config-file.js)
[![Code Climate](https://codeclimate.com/github/snowyu/load-config-file.js/badges/gpa.svg)](https://codeclimate.com/github/snowyu/load-config-file.js)
[![Test Coverage](https://codeclimate.com/github/snowyu/load-config-file.js/badges/coverage.svg)](https://codeclimate.com/github/snowyu/load-config-file.js/coverage)
[![downloads](https://img.shields.io/npm/dm/load-config-file.svg)](https://npmjs.org/package/load-config-file)
[![license](https://img.shields.io/npm/l/load-config-file.svg)](https://npmjs.org/package/load-config-file)

Load the config file as a plain object.

* The config file format can be registered.
* The registered file format order is the search order.
* the virtual file system can be specfied: `loadConfig.setFileSystem(fs)`
  * `fs.path = require('path.js/lib/path').path` for the  virtual POSIX path.
    * you must set the path first before call setFileSystem.

## Usage

```js
import { Config as loadConfig } from 'load-config-file'
import { parse as parseYaml } from 'yaml'
import * as cson from 'cson';

loadConfig.register(['.yaml', '.yml'], parseYaml); //first search.
loadConfig.register('.cson', cson.parseCSONString.bind(cson)); //second search
loadConfig.register('.json', JSON.parse); //third search.

//Synchronously load config from file.
//it will search config.yaml, config.yml, config.cson, config.json in the current folder.
//the first exist file will be loaded.
//the default encoding is "utf8" if no encoding.
//loadConfig('config', {encoding: 'ascii'})
//the non-enumerable "$cfgPath" property added.
console.log(loadConfig('config'));

const result = await loadConfig('config')
//Asynchronously load config from file
loadConfig('config', function(err, result){
  if (err) {
    console.log('error:', err);
  } else {
    console.log(result);
  }
})

```

## API

```js
var config = require('load-config-file');
```

* `config.register(extensionNames, parserFunc)`: register the configuration file format to
  load. return the configurators if successful.
  * `extensionNames` *(Sting|ArrayOf String)*: the configuration file extension name(s)
    with dot.
  * `parserFunc` *Function(context)*: the configuration context parser function:
    * parse the configuration context and return the plain object.
* `config.setFileSystem(fs)`: set your favour file system. defaults to 'fs'.
  * the "file system" must implement `readFile(path[, options], done)` and `readFileSync(path[, options])`
* `load(path, options, done)`: Asynchronously load config from file
  * options
    * `raiseError` *(Boolean)*: raise error if nothing loaded defaults to false.
    * `exclude` *(String|ArrayOf String)*: excludes some files.
    * `encoding` *(String)*: the file encoding name. defaults to 'utf8'.
  * return the plain object and the `$cfgPath` property added if successful.
* `loadSync(path, options)`: Synchronously load config from file
  * return the plain object and the `$cfgPath` property added if successful.

## Changes

### v0.2

+ raiseError option to load function asynchronously.
+ add the `$cfgPath`*(String)* non-enumerable property to the result.
+ add object usage supports:

    ```js
    var cfgObj = new Config(aPath, aOptions) //create a configuration object.
    result = cfgObj.loadSync()
    ```

## TODO

* ! how to use the specified promise library via [any-promise](https://github.com/kevinbeaty/any-promise)?
  * currently use the bluebird by default.


## License

MIT

---
_Source: https://npm.io/package/load-config-file · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
