# unreadconfig

Latest version **0.1.0** (published 2024-09-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install unreadconfig
pnpm add unreadconfig
yarn add unreadconfig
bun add unreadconfig
```

## Health

**Score 30/100 (F)** — status: abandoned.

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2024-09-26 |
| First published | 2023-09-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 7 |
| Unpacked size | 24.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | hunghg255 |
| Maintainers | hunghg255 |
| Keywords | rc, config, loader, load, configuration, cjs, commonjs, esm, es module, yml-config, js-config, ts-config, toml-config, yml, js, ts, toml, typescript, config-loader, auto-config-loader |

## Links

- npm: https://www.npmjs.com/package/unreadconfig
- Repository: https://github.com/hunghg255/unreadconfig
- Homepage: https://github.com/hunghg255/unreadconfig.git
- Issues: https://github.com/hunghg255/unreadconfig/issues
- npm.io page: https://npm.io/package/unreadconfig

## Dependencies (7)

- [ini](https://npm.io/package/ini.md) ^4.1.3
- [jiti](https://npm.io/package/jiti.md) ^2.0.0
- [sucrase](https://npm.io/package/sucrase.md) ^3.35.0
- [lodash.merge](https://npm.io/package/lodash.merge.md) ^4.6.2
- [toml-eslint-parser](https://npm.io/package/toml-eslint-parser.md) ^0.6.1
- [yaml-eslint-parser](https://npm.io/package/yaml-eslint-parser.md) ^1.2.3
- [jsonc-eslint-parser](https://npm.io/package/jsonc-eslint-parser.md) ^2.4.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

- 0.1.0 (latest) — 2024-09-26
- 0.0.13 — 2023-12-20
- 0.0.12 — 2023-10-09
- 0.0.11 — 2023-10-09
- 0.0.10 — 2023-09-02
- 0.0.9 — 2023-09-02
- 0.0.8 — 2023-09-02
- 0.0.7 — 2023-09-02
- 0.0.6 — 2023-09-02
- 0.0.5 — 2023-09-01
- 0.0.4 — 2023-09-01
- 0.0.3 — 2023-09-01
- 0.0.2 — 2023-09-01
- 0.0.1 — 2023-09-01

## README

<p align="center">
<a href="https://www.npmjs.com/package/unreadconfig" target="_blank" rel="noopener noreferrer">
<img src="https://api.iconify.design/eos-icons:configuration-file-outlined.svg?color=%2380ffb7" alt="logo" width='100'/></a>
</p>

<p align="center">
  A tiny JavaScript library read config file
</p>

<p align="center">
  <a href="https://www.npmjs.com/package/unreadconfig" target="_blank" rel="noopener noreferrer"><img src="https://badge.fury.io/js/csvs-parsers.svg" alt="NPM Version" /></a>
  <a href="https://www.npmjs.com/package/unreadconfig" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/npm/dt/csvs-parsers.svg?logo=npm" alt="NPM Downloads" /></a>
  <a href="https://bundlephobia.com/result?p=unreadconfig" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/bundlephobia/minzip/unreadconfig" alt="Minizip" /></a>
  <a href="https://github.com/hunghg255/unreadconfig/graphs/contributors" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/badge/all_contributors-1-orange.svg" alt="Contributors" /></a>
  <a href="https://github.com/hunghg255/unreadconfig/blob/main/LICENSE" target="_blank" rel="noopener noreferrer"><img src="https://badgen.net/github/license/hunghg255/unreadconfig" alt="License" /></a>
</p>


Find and load configuration from a `package.json` property, `rc` file, or `CommonJS` module. It has smart default based on traditional expectations in the JavaScript ecosystem. But it's also flexible enough to search anywhere you want and load whatever you want.

## Features

- Support [JSON](https://www.json.org), [JSONC](https://github.com/microsoft/node-jsonc-parser), [JSON5](https://json5.org/), [YAML](https://yaml.org/), [TOML](https://toml.io), [INI](https://en.wikipedia.org/wiki/INI_file), [CJS](http://www.commonjs.org), [Typescript](https://www.typescriptlang.org/), and ESM config load.
- Reads config from the nearest `package.json` file

## Install

```bash
$ npm i unreadconfig
```

## Quick start

```js
const { readConfig } = require('unreadconfig');

import { readConfig } from 'unreadconfig';

// will look for:
// process.cwd() + '.namespacerc'
// process.cwd() + '.namespacerc.js'
// process.cwd() + '.namespacerc.ts'
// process.cwd() + '.namespacerc.mjs'
// process.cwd() + '.namespacerc.cjs'
// process.cwd() + '.namespacerc.json'
// process.cwd() + '.namespacerc.json5'
// process.cwd() + '.namespacerc.jsonc'
// process.cwd() + '.namespacerc.yaml'
// process.cwd() + '.namespacerc.yml'
// process.cwd() + '.namespacerc.toml'
// process.cwd() + 'namespace.config.mjs'
// process.cwd() + 'namespace.config.cjs'
// process.cwd() + 'namespace.config.js'
// ........
const data = readConfig('namespace', {
  default: {
    testItem2: 'some value'
  }
});
```

## Load JS

Load the JS file and return the result, support `.js`, `.cjs`, `.mjs`, `.ts`.

```js
// => ./app/app.config.js
export default {
  name: 'app'
}
```

```ts
import { loadConf } from 'unreadconfig/load-conf';

interface Config {
  name: string;
}

const result = loadConf<Config>(path.resolve(process.cwd(), 'app/app.config.js'));
// => { name: 'app' }
```

## Option

```ts
import { LoadConfOption } from 'unreadconfig';
export type LoaderFunc<T> = (filepath: string, content: string, jsOption?: LoadConfOption) => T;
export type Loader<T> = Record<string, LoaderFunc<T>>;
export interface readConfigOption<T> {
  searchPlaces?: string[];
  /** An object that maps extensions to the loader functions responsible for loading and parsing files with those extensions. */
  loaders?: Loader<T>;
  /** Specify default configuration. It has the lowest priority and is applied after extending config. */
  defaluts?: T;
  /** Resolve configuration from this working directory. The default is `process.cwd()` */
  cwd?: string;
  /** Default transform js configuration */
  jsOption?: LoadConfOption;
  /** @deprecated use `mustExist` instead */
  ignoreLog?: boolean;
  mustExist?: boolean;
}
/**
 * Find and load configuration from a `package.json` property, `rc` file, or `CommonJS` module.
 * @param namespace {string} Configuration base name. The default is `readConfig`.
 * @param option
 */
export default function readConfig<T>(namespace?: string, option?: readConfigOption<T>): {} & T;
```

Discover configurations in the specified directory order. When configuring a tool, you can use multiple file formats and put these in multiple places. Usually, a tool would mention this in its own README file, but by default, these are the following places, where `${moduleName}` represents the name of the tool:

**Default `searchPlaces`:**

```js
[
  'package.json',
  `.${moduleName}rc`,
  `.${moduleName}rc.json`,
  `.${moduleName}rc.json5`,
  `.${moduleName}rc.jsonc`,
  `.${moduleName}rc.yaml`,
  `.${moduleName}rc.yml`,
  `.${moduleName}rc.toml`,
  `.${moduleName}rc.ini`,
  `.${moduleName}rc.js`,
  `.${moduleName}rc.ts`,
  `.${moduleName}rc.cjs`,
  `.${moduleName}rc.mjs`,
  `.config/${moduleName}rc`,
  `.config/${moduleName}rc.json`,
  `.config/${moduleName}rc.json5`,
  `.config/${moduleName}rc.jsonc`,
  `.config/${moduleName}rc.yaml`,
  `.config/${moduleName}rc.yml`,
  `.config/${moduleName}rc.toml`,
  `.config/${moduleName}rc.ini`,
  `.config/${moduleName}rc.js`,
  `.config/${moduleName}rc.ts`,
  `.config/${moduleName}rc.cjs`,
  `.config/${moduleName}rc.mjs`,
  `${moduleName}.config.js`,
  `${moduleName}.config.ts`,
  `${moduleName}.config.cjs`,
  `${moduleName}.config.mjs`,
]
```

Configurations are loaded sequentially, and the configuration file search is terminated when a configuration file exists.


The content of these files is defined by the tool. For example, you can add a `semi` configuration value to `false` using a file called `.config/readConfig.yml`:

```yml
semi: true
```

Additionally, you have the option to put a property named after the tool in your `package.json` file, with the contents of that property being the same as the file contents. To use the same example as above:

```js
{
  "name": "your-project",
  "readConfig": {
    "semi": true
  }
}
```

This has the advantage that you can put the configuration of all tools (at least the ones that use `unreadconfig`) in one file.

## loader

### `.js`,`.ts`,`.cjs`,`.mjs`

```ts
import type { JITIOptions } from 'jiti/dist/types';
import { Options } from 'sucrase';
export interface LoadConfOption {
  jiti?: boolean;
  jitiOptions?: JITIOptions;
  transformOption?: Options;
}
export declare function loadConf<T>(path: string, option?: LoadConfOption): T;
export declare function jsLoader<T>(filepath: string, content: string, option?: LoadConfOption): T;
```

Modify default `.js`,`.ts`,`.cjs`,`.mjs` loader parameters.

```js
import { readConfig, jsLoader } from 'unreadconfig';

function loadJS(filepath, content) {
  return jsLoader(filepath, content, {
    // change option...
  });
}

const data = readConfig('namespace', {
  loaders: {
    '.js': loadJS,
    '.ts': loadJS,
    '.cjs': loadJS,
    '.mjs': loadJS,
  },
  default: {
    testItem2: 'some value'
  }
});
```

example:

```ts
import { jsLoader } from 'unreadconfig';

const data = jsLoader('/path/to/file/name.js')
```

### `.ini`

```ts
export declare function iniLoader<T>(_: string, content: string): T;
```

example:

```ts
import { iniLoader } from 'unreadconfig';

const data = iniLoader(undefined, `...`)
```

### `.json`,`.jsonc`, `json5`

```ts
export declare function jsonLoader<T>(_: string, content: string): T;
```

example:

```ts
import { jsonLoader } from 'unreadconfig';

const data = jsonLoader(undefined, `{ "a": 123 }`)
```

### `.toml`

```ts
export declare function tomlLoader<T>(_: string, content: string): T;
```

example:

```ts
import { tomlLoader } from 'unreadconfig';

const data = tomlLoader(undefined, `...`)
```

### `.yaml`

```ts
export declare function yamlLoader<T>(_: string, content: string): T;
```

example:

```ts
import { yamlLoader } from 'unreadconfig';

const data = yamlLoader(undefined, `...`)
```

## Custom `Yaml` loader

This is an example, the default `yaml`/`yml` does not require a loader.

```js
import { readConfig } from 'unreadconfig';
import yaml from 'yaml';

function loadYaml(filepath, content) {
  return yaml.parse(content);
}

const data = readConfig('namespace', {
  searchPlaces: [
    '.namespacerc.yaml',
    '.namespacerc.yml',
  ],
  loaders: {
    '.yaml': loadYaml,
    '.yml': loadYaml,
  },
  default: {
    testItem2: 'some value'
  }
});
```

## utils

### merge

```ts
export declare const merge: {
  <TObject, TSource>(object: TObject, source: TSource): TObject & TSource;
  <TObject_1, TSource1, TSource2>(object: TObject_1, source1: TSource1, source2: TSource2): TObject_1 & TSource1 & TSource2;
  <TObject_2, TSource1_1, TSource2_1, TSource3>(object: TObject_2, source1: TSource1_1, source2: TSource2_1, source3: TSource3): TObject_2 & TSource1_1 & TSource2_1 & TSource3;
  <TObject_3, TSource1_2, TSource2_2, TSource3_1, TSource4>(object: TObject_3, source1: TSource1_2, source2: TSource2_2, source3: TSource3_1, source4: TSource4): TObject_3 & TSource1_2 & TSource2_2 & TSource3_1 & TSource4;
  (object: any, ...otherArgs: any[]): any;
};
```

### findConfigFile

```ts
export declare function findConfigFile(moduleName: string, root: string, searchPlaces?: string[]): string;
```

### getConfigPath

```ts
export declare const getConfigPath: () => string;
```

Example:

```ts
import { readConfig, getConfigPath } from 'unreadconfig';

const data = readConfig<Config>('idoc');
const configPath = getConfigPath();
// => /.readConfigrc.js
```

## Related

- [cosmiconfig](https://github.com/cosmiconfig/cosmiconfig) Find and load configuration from a package.json property, rc file, or CommonJS module
- [cjson](https://www.npmjs.com/package/cjson) Comments enabled json loader (Commented JavaScript Object Notation)
- [Config Loader](https://www.npmjs.com/package/@web/config-loader) Load user config files for node js projects.
- [Lilconfig](https://www.npmjs.com/package/lilconfig) Zero-dependency nodejs config seeker.
- [proload](https://github.com/natemoo-re/proload) Searches for and loads your tool's JavaScript configuration files with full support for CJS, ESM, TypeScript and more.
- [rc](https://github.com/dominictarr/rc) The non-configurable configuration loader for lazy people.

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