# @neo9/n9-node-conf

> Conf node module loader

Latest version **2.0.0** (published 2023-11-14) · GPL-3.0-or-later license · 0 weekly downloads

## Install

```sh
npm install @neo9/n9-node-conf
pnpm add @neo9/n9-node-conf
yarn add @neo9/n9-node-conf
bun add @neo9/n9-node-conf
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2023-11-14 |
| First published | 2017-08-30 |
| Weekly downloads | 0 |
| License | GPL-3.0-or-later |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 18 |
| Dependencies | 4 |
| Unpacked size | 61.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Benjamin DANIEL |
| Maintainers | cribeiron9, n9-dsabatier, fmarion, benjd90, cballesta, mrskyce |
| Keywords | node conf, conf env, conf, load conf, configuration, extension, yaml, json |

## Links

- npm: https://www.npmjs.com/package/@neo9/n9-node-conf
- Repository: https://github.com/neo9/n9-node-conf
- Homepage: https://github.com/neo9/n9-node-conf#readme
- Issues: https://github.com/neo9/n9-node-conf/issues
- npm.io page: https://npm.io/package/@neo9/n9-node-conf

## Dependencies (4)

- [debug](https://npm.io/package/debug.md) ^4.3.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [js-yaml](https://npm.io/package/js-yaml.md) ^4.1.0
- [app-root-dir](https://npm.io/package/app-root-dir.md) ^1.0.2

## 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.0.0 (latest) — 2023-11-14
- 2.0.0-rc.3 (rc) — 2023-10-27
- 2.0.0-rc.2 — 2023-10-13
- 2.0.0-rc.1 — 2023-10-13
- 2.0.0-rc.0 — 2023-10-13
- 1.4.1 — 2022-03-21
- 1.4.0 — 2021-05-18
- 1.3.2 — 2020-03-24
- 1.3.1 — 2020-03-24
- 1.3.0 — 2020-03-23
- 1.2.3 — 2020-03-23
- 1.2.2 — 2020-03-20
- 1.2.1 — 2020-03-20
- 1.2.0 — 2020-03-20
- 1.1.1 — 2018-04-17
- … 1 more at https://npm.io/package/@neo9/n9-node-conf/versions

## README

# n9-node-conf

Conf node module loader.

[![npm version](https://img.shields.io/npm/v/@neo9/n9-node-conf.svg)](https://www.npmjs.com/package/@neo9/n9-node-conf)
[![Travis](https://img.shields.io/travis/neo9/n9-node-conf/master.svg)](https://travis-ci.org/neo9/n9-node-conf)
[![Coverage](https://img.shields.io/codecov/c/github/neo9/n9-node-conf/master.svg)](https://codecov.io/gh/neo9/n9-node-conf)
[![license](https://img.shields.io/github/license/neo9/n9-node-conf.svg)](https://github.com/neo9/n9-node-conf/blob/master/LICENSE)

## Installation

```bash
yarn add @neo9/n9-node-conf
```

or

```bash
npm install --save @neo9/n9-node-conf
```

## V2 Upgrade

- Drop Node.js V 16 support as it has reached end of life
- Change extendConfig setting from `string` to `object`.
  Example :

<table>
<tr>
<th>
Before (V1)
</th>
<th>
After (V2)
</th>
</tr>
<tr>
<td>
<pre>

```ts
{
	...,
	extendConfig: {
		key: 'appName'
	}
}
```

</pre>
</td>
<td>
<pre>

```ts
{
	...,
	extendConfig: {
		key: {
			name: 'appName'
		}
	}
}
```

</pre>
</td>
</tr>
</table>

## Usage

`n9NodeConf([options])`

Options:

- path: `String`, default: `process.env.NODE_CONF_PATH || './conf/'`

Example:

```typescript
import n9NodeConf from '@neo9/n9-node-conf';
import { join } from 'path';

const conf = n9NodeConf({
	path: join(__dirname, 'conf'),
});
```

### Options :

[N9ConfOptions](./src/index.ts#L8) :

#### path

Type: `string`\
Required \
Path to the folder containing the configuration files. See the [structure](#structure) for more details.

#### extendConfig

Type: `object`\
Default: undefined
To describe extension configuration. Extension configuration ca be a `json`, `yaml` or `yml` file. \
In the order, it will try to load the path given, then the same file changing the extension to another supported.

| given | 2nd try | 3rd try |
| :---: | :-----: | :-----: |
| json  |  yaml   |   yml   |
| yaml  |   yml   |  json   |
|  yml  |  json   |  yaml   |

##### path

Type: `object`\
Required \
To describe where to find extension configuration. One of `absolute` or `relative` is required.

###### absolute

Type: `string`\
Required if `relative` is not filled \
Absolute path to the extension configuration.\
Example : `Path.join(__dirname, 'conf/env.json')`

###### relative

Type: `string`\
Required if `absolute` is not filled \
Relative path to the conf folder `path` \
Example : `'./env.json'`

##### key

###### name

Type: `string`\
Default the app name from `package.json`.`name`\
The key to use in configuration extension. The path to load the conf will be `{env}.{app name}`

###### format

Type: `ExtendConfigKeyFormat`\
Default to undefined.\
The format to apply to the `packageJSON.name` to find the key name. The path to load the conf will be `{env}.{format}({app name})`

##### mergeStrategy

Type: `N9ConfMergeStrategy` (`v1` or `v2`)\
Default: `v2`\
The merge strategy to use to merge extension configuration with the other.

- v1 : Use lodash merge function. Mainly, merge deeper in arrays\
  [a, b] + [c, d] → [merge(a, c), merge(b, d)]
- v2 : Use built in mechanism. It replace array is any\
  [a, b] + [c, d] → [c, d]

#### overridePackageJsonDirPath

Type: `string`\
Default: `undefined`, use npm module [app-root-dir](https://www.npmjs.com/package/app-root-dir) to find `package.json`
Used to load `package.json`, to find app name, app version and with app name to build the path to load the conf extension.

#### override

Type: `object`\
Default undefined, no override
Override the conf at the end of loading.

##### value

Type: `object`\
Default: undefined, not applied\
Value to override the conf at the end of loading. Merge strategy used is defined bellow. Useful for tests.

##### mergeStrategy

Type: `N9ConfMergeStrategy`\
Default : N9ConfMergeStrategy.V2\
Merge strategy to use to merge override.

## Structure

```bash
conf/
  application.ts
  development.ts
  integration.ts
  local.ts # should be in .gitignore
  preproduction.ts
  production.ts
  staging.ts
package.json
```

The module will load these files, every file overwrites the one before:

`application.js + ${process.env.NODE_ENV}.js + local.js`

1. If `process.env.NODE_ENV` is not defined, default to `'development'`
2. If `local.js` does not exists, it will be ignored.
3. It will also fetch the `package.json` of the app to fill its `name` & `version`

This module can use a configuration extension, see [here](./documentation/extendable-configuration.md) for more information.

## Example

`package.json`

```json
{
	"name": "my-app",
	"version": "0.1.2"
}
```

`conf/application.ts`

```js
export default {
	http: {
		port: 6686,
	},
};
```

`conf/development.ts`

```js
export default {};
```

`conf/production.ts`

```js
export default {
	http: {
		port: 80,
	},
};
```

`loadConf.ts`

```js
import n9NodeConf from '@neo9/n9-node-conf';

const conf = n9NodeConf();
console.log('const conf =', conf);
```

`node loadConf.ts`

```typescript
const conf = {
	name: 'my-app',
	version: '0.1.2',
	env: 'development',
	http: {
		port: 5000,
	},
};
```

`NODE_ENV=production node loadConf.ts`

```typescript
const conf = {
	name: 'my-app',
	version: '0.1.2',
	env: 'production',
	http: {
		port: 80,
	},
};
```

## Logs

To display the logs of the module, you can use `DEBUG=n9-node-conf`.

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