Conf node module loader
npm install @neo9/n9-node-confConf node module loader.




``bash`
yarn add @neo9/n9-node-conf
or
`bash`
npm install --save @neo9/n9-node-conf
- Drop Node.js V 16 support as it has reached end of life
- Change extendConfig setting from string to object.
Example :
Before (V1) | After (V2) |
|---|---|
|
|
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'),
});
`
#### path
Type: string\
Required \
Path to the folder containing the configuration files. See the structure for more details.
#### extendConfig
Type: object\json
Default: undefined
To describe extension configuration. Extension configuration ca be a , 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\absolute
Required \
To describe where to find extension configuration. One of or relative is required.
###### absolute
Type: string\relative
Required if is not filled \Path.join(__dirname, 'conf/env.json')
Absolute path to the extension configuration.\
Example :
###### relative
Type: string\absolute
Required if is not filled \path
Relative path to the conf folder \'./env.json'
Example :
##### key
###### name
Type: string\package.json
Default the app name from .name\{env}.{app name}
The key to use in configuration extension. The path to load the conf will be
###### format
Type: ExtendConfigKeyFormat\packageJSON.name
Default to undefined.\
The format to apply to the to find the key name. The path to load the conf will be {env}.{format}({app name})
##### mergeStrategy
Type: N9ConfMergeStrategy (v1 or v2)\v2
Default: \
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\undefined
Default: , use npm module app-root-dir to find package.jsonpackage.json
Used to load , 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.
`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'local.js
2. If does not exists, it will be ignored.package.json
3. It will also fetch the of the app to fill its name & version
This module can use a configuration extension, see here for more information.
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,
},
};
To display the logs of the module, you can use DEBUG=n9-node-conf`.